Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
--i have read some article and found that

''' anurag ''' --this in dynamic sql will be treated as 'anurag'

SQL
declare @var varchar(max)
set @var='select * from tbl_user_master where first_name like'+ '''%anurag%'''
print @var
exec (@var)


--i get the result

------------------------------------------------------------------------------
--but why i get error on below query

SQL
declare @var varchar(max)

set @var='select * from tbl_user_master where first_name like '''%anurag%''' '
print @var

exec (@var)


--Msg 207, Level 16, State 1, Line 3
Invalid column name 'anurag'.

-- i m new to it.. kindly guide me.

--i thought both are same and both will give me the same result
Posted
Updated 6-Sep-13 4:56am
v2
Comments
Richard C Bishop 6-Sep-13 10:58am    
You have an extra single quote at the end of the second snippet of SQL.

1 solution

No, they aren't the same!
The in first example:
like' + '''%anurag%'''
    ^   ^^^        ^^^---- Close second string
    |   |||        ||----- 2nd character of a pair of quotes\
    |   |||        |                                         - These create a single ' character
    |   |||        |------ 1st character of a pair of quotes/
    |   |||--------------- 2nd character of a pair of quotes\
    |   ||                                                   - These create a single ' character
    |   ||---------------- 1st character of a pair of quotes/
    |   |----------------- Open second string
    |--------------------- Close first string                       
In the second example:
like '''%anurag%''' '
     |||        ||| |----- Or this...
     |||        |||------- Or this...
     |||        ||-------- Or this...
     |||        |--------- SQL not going to look at this...
     |||                   ERK! SQL NO UNDERSTAND! **REDO FROM START** ***OUT OF CHEESE ERROR***
     |||------------------ End of first string
     ||------------------- 2nd character of a pair of quotes\
     |                                                       - These create a single ' character
     |-------------------- 1st character of a pair of quotes/
 
Share this answer
 
Comments
anurag19289 6-Sep-13 11:34am    
Thats why i always i say you "you are simply awsome". A perfect explanation. This is what i was looking for :) cheers +5
OriginalGriff 6-Sep-13 11:36am    
You're welcome!
Bala Selvanayagam 6-Sep-13 12:06pm    
5ed

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900