12,401,867 members (68,884 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-jw16K

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

Please use below query...

```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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 250 Richard Deeming 223 Karthik Bangalore 180 ppolymorphe 145 F-ES Sitecore 135
 OriginalGriff 7,033 ppolymorphe 3,030 Karthik Bangalore 2,917 F-ES Sitecore 2,187 Richard MacCutchan 2,125

Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 22 Feb 2013