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)
 No title supplied

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-04-03 : 16:22:29
writes "--The following bit of code will set the variable @str.
--@str = 'select * from where CLientNum=@ClientNum'

I was wondering if there is some syntax that allows me to execute that string.

I can print it to the screen then cut and paste it into queryanalyzer but is there some way to automate the execution of the sql statements that are produced?

declare @str varchar(500)

--get the clientNum
declare @Clientnum int
set @ClientNum=1234

declare @currentID int
set @currentID = -1

--begin cursor loop
select top 1 @str = 'select * from ' + O.name + ' where ClientNum = @ClientNum', @currentID = O.ID
from sysColumns C
join sysObjects O on C.ID = O.ID
where C.name like 'ClientNum' and O.ID>@currentID
order by O.ID
--end cursor loop

While (@@rowcount=1)
BEGIN

print @str
--expects a stored procedure when using the execute command
--execute @str ???

--Begin Cursor loop
select top 1 @str = 'select * from ' + O.name + ' where ClientNum = @ClientNum',@currentID=O.ID
from sysColumns C
join sysObjects O on C.ID = O.ID
where C.name like 'ClientNum' and O.ID>@currentID
order by O.ID
--end cursor loop
END"
   

- Advertisement -