Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Assigning Query to the variable

Author  Topic 

girishkardam
Starting Member

13 Posts

Posted - 2008-09-19 : 07:41:44
Hi I had created procedure and passing tablename & columnname as parameter. I am using passed tablename & columnname in below format

set @sql = 'select count(*) from '+ @tablename + ' where '+ @columnname + ' Like '+ '%Script%'

exec(@sql)

but procedure is not working properly and showing error for '%Script%' while executing it bcz while executing the query '%Script%' is read as %Script% by sql server. Kindly guide me how i can handle this condition.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-19 : 07:50:39
do like below:-
set @sql = 'select count(*) from '+ @tablename + ' where '+ @columnname + ' Like ''%Script%'''
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-19 : 07:51:47
also read below to understand about single quotes
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/02/19/understanding-single-quotes.aspx
Go to Top of Page

girishkardam
Starting Member

13 Posts

Posted - 2008-09-19 : 08:07:27
Hi visakh thanks it working fine
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-19 : 09:44:40
quote:
Originally posted by girishkardam

Hi visakh thanks it working fine


welcome
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-09-19 : 10:40:57
Better yet, read up on sp_executeSQL -- using parameters is always the best way to write stuff like this.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -