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
 Transact-SQL (2000)
 [[Solved]] Dynamic Query in Store Procedure

Author  Topic 

jas2005
Starting Member

11 Posts

Posted - 2005-02-15 : 03:27:22
Dear All,
I have a dynamic query as shown below
[ Sample Code ]
declare @strKey1 varchar(30), @strKey2 varchar(30), @mysql varchar(200)
set @strKey1 = '[Type]'
set @strKey2 = 'business'
set @mysql = 'select title_id, type, pub_id from titles Where ' + @strKey1 + '=' + @strKey2
Print @mysql
exec (@mysql)

How do i make this code workable ???

PLease advise / HELP !!!

Thanks

~j~A~s

AndyB13
Aged Yak Warrior

583 Posts

Posted - 2005-02-15 : 03:32:55
You need to enclose you variable @strKey2 in ''
When you use PRINT, copy the text from the results and paste into the QA window and see if what you are generating works

Where ' + @strKey1 + '=' + '''' + @strKey2 + ''''

Andy

Beauty is in the eyes of the beerholder
Go to Top of Page

jas2005
Starting Member

11 Posts

Posted - 2005-02-15 : 03:43:57
Thanks...Andy :D

~j~A~s
Go to Top of Page
   

- Advertisement -