Microsoft
Software
Hardware
Network
Question : Result Set in a dynamic query
Hey experts,
I have the following dynamic sql query in a larger stored procedure:
Set @SQlQuery = 'SELECT GLAccountID, (ACOpening + ACBalance) AS ACSum
FROM ' + @OldDBName + '.dbo.tbGLAccount
WHERE (Detail = 1) AND (Code LIKE ''1%'' OR Code LIKE ''2%'' OR Code LIKE ''3%''
OR Code LIKE ''4%'' OR Code LIKE ''5%'')'
It returns several rows, and I want all the result set of this query to be returned to me (by some kind of common cursor between the dynamic query & my SP), so I would be using the values of this result set in my SP...any help on how to do that??
P.S.: I was provided in a similar question to this one the follwoing link:
http://support.microsoft.c
om/default
.aspx?scid
=KB;EN-
US;
q262499
, but it doesn't help in this case
Answer : Result Set in a dynamic query
--in case you plan to use it as parameters for you sp
declare @SQlQuery varchar(1000),@OldDBName sysname
set @OldDBName='pubs'
Set @SQlQuery = 'SELECT top 10 au_fname FROM ' + @OldDBName + '.dbo.authors'
create table #t (fname varchar(50))
insert into #t
exec(@SQlQuery)
-- Declare the variables to store the values returned by FETCH.
DECLARE @au_fname varchar(20)
DECLARE authors_cursor CURSOR FOR
select * from #t
OPEN authors_cursor
-- Perform the first fetch and store the values in variables.
-- Note: The variables are in the same order as the columns
-- in the SELECT statement.
FETCH NEXT FROM authors_cursor
INTO @au_fname
-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
-- Concatenate and display the current values in the variables.
PRINT 'Author: ' + @au_fname
-- This is executed as long as the previous fetch succeeds.
FETCH NEXT FROM authors_cursor
INTO @au_fname
END
CLOSE authors_cursor
DEALLOCATE authors_cursor
GO
Random Solutions
options for repairing unstable laptop - low level re-format?
Query to Dump entire mySQL database?
Can't boot XP: recycles, Safe mode fails
I need to install multiple network printers using AD or a startup batch file.
exchange MTA, all sent mail is NDR'd
Citrix Client in locked PCs
outlook "You have new mail" notification pop up
priv1.stm size, exchange 16 gb limit
Informix : Subtract minutes from datetime
Can't find drivers for PCI Parrallel.