13,086,120 members (84,264 online)
Rate this:
See more:
```BEGIN
DECLARE @T VARCHAR(20)
DECLARE @QRY NVARCHAR(200)
SET @T='X'
SET @QRY='SELECT * FROM [dbo].[LIBMAST] WHERE CALLNO LIKE %'+@T+'%'
EXEC(@QRY)
END```

error showing

Incorrect syntax near 'X'.
Posted 21-Feb-13 22:28pm
josh-jw17.3K

Rate this:

## Solution 1

You need quotes in your query try:

```BEGIN
DECLARE @T VARCHAR(20)
DECLARE @QRY NVARCHAR(200)
SET @T='X'
SET @QRY='SELECT * FROM [dbo].[LIBMAST] WHERE CALLNO LIKE ''%'+@T+'%''';
EXEC(@QRY)
END```
Rate this:

## Solution 2

Quotes my friend, quotes.
Change:
`SET @QRY='SELECT * FROM [dbo].[LIBMAST] WHERE CALLNO LIKE %'+@T+'%'`
To
`SET @QRY='SELECT * FROM [dbo].[LIBMAST] WHERE CALLNO LIKE ''%'+@T+'%'''`
Rate this:

## Solution 3

Hi Josh,

Try This.....
```BEGIN
DECLARE @T VARCHAR(20)
DECLARE @QRY NVARCHAR(200)
SET @T='X'
SET @QRY='SELECT * FROM [dbo].[LIBMAST] WHERE CALLNO LIKE ''%'+@T+'%'''
PRINT @QRY
EXEC(@QRY)
END```

(or)
```BEGIN
DECLARE @T VARCHAR(20)
DECLARE @QRY NVARCHAR(200)
SET @T='X'
SET @QRY='SELECT * FROM [dbo].[LIBMAST] WHERE CALLNO LIKE '+CHAR(39)+'%'+@T+'%'+CHAR(39)
PRINT @QRY
EXEC(@QRY)
END```

You missed Single Quote in String Comparison.

Regards,
GVPrabu
v2
Rate this:

## Solution 4

Hi Josh

```BEGIN
DECLARE @T VARCHAR(20)
DECLARE @QRY NVARCHAR(200)
SET @T='X'
SET @QRY='SELECT * FROM [dbo].[LIBMAST] WHERE CALLNO LIKE ''%' + @T + '%'''
EXEC(@QRY)
END```

Regards
Willington

Top Experts
Last 24hrsThis month
 Graeme_Grant 170 OriginalGriff 138 Jochen Arndt 115 Kornfeld Eliyahu Peter 90 BillWoodruff 80
 OriginalGriff 3,095 Graeme_Grant 1,519 ProgramFOX 1,367 ppolymorphe 1,217 Jochen Arndt 1,205