Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Executing the results of a query

Status
Not open for further replies.

dimbulbz

Technical User
Joined
May 23, 2001
Messages
5
Location
US
I need to be able to execute the result set of a query. I have used xp_cmdshell, with an ISQL statement, but all I really want to do is call a new SQL widow from within SQL and execute, without all ISQL /U /P /Q"yadayada..." These are simple, one line queries that are loaded into a variable using a cursor:
*************************example*****************
declare @adduser as char (150)
declare crsr cursor for
select
'sp_adduser '''+rtrim(name)+''''+','''+rtrim(name)+''''+',''public'''
from tempuser
for read only
open crsr
while (0 = 0)
begin
fetch next from crsr into @adduser
if (@@fetch_status <> 0 ) break
exec master..xp_cmdshell @adduser
select @adduser
end
close crsr
deallocate crsr

********************** end example *****************

I want to replace the line

exec master..xp_cmdshell @adduser

with something that just opens a SQL session and executes the contents of @adduser.

Its probably really simple, but I'm stumped. Any help out there?



Jim Johnston
dimbulbz@hotmail.com
 
EXECUTE can be used to execute a T-SQL string held in a variable, so you could rewrite the proc something like this;

Code:
declare @adduser as [red]VARCHAR(150)[/red]
declare crsr  cursor [red]LOCAL FAST_FORWARD[/red] for 
select  
   'sp_adduser '''+rtrim(name)+''''+','''+rtrim(name)+''''+',''public'''
    from tempuser

open crsr 
[red]fetch next from crsr into @adduser[/red]
while [red]@@Fetch_Status = 0[/red]
 begin 
   [red]exec (@adduser)[/red]
   select @adduser
   fetch next from crsr into @adduser
 end
close crsr
deallocate crsr

Nathan

[yinyang]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top