SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Store result in Variable of dynamic query


Store result in Variable of dynamic query

Author
Message
Bhuvnesh
Bhuvnesh
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21502 Visits: 4079
hi


declare @lstr varchar(200)
declare @word varchar(20)
set @lstr = 'select top 1 name from sysobjects'
set @word = exec(@lstr)
select @word



i want ot store result comes from dynamic query into variable @word.(or above code is not working)



Please help

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
winash
winash
SSCarpal Tunnel
SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)

Group: General Forum Members
Points: 4550 Visits: 1883
The problem here is that the variables are declared outside the scope of the dynamic SQL statement...either declare the variable within the dynamic SQL (this might not be of any use) or get the results of the dynamic into a table/temp table (this might be of more use)

declare @lstr varchar(200)
set @lstr = 'declare @word varchar(20) select top 1 @word = name from sysobjects select @word'
exec(@lstr)

or

DECLARE @Results TABLE(result sysname)
INSERT @Results(result)
EXEC('select top 1 name from sysobjects')
SELECT * FRM @Results



Kishore.P
Kishore.P
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3703 Visits: 619
bhuvnesh.dogra (11/11/2008)
hi

declare @lstr varchar(200)
declare @word varchar(20)
set @lstr = 'select top 1 name from sysobjects'
set @word = exec(@lstr)
select @word

i want ot store result comes from dynamic query into variable @word.(or above code is not working)

Please help


you may try like:

declare @word table (tableName varchar (50))
declare @lstr varchar (200)
set @lstr = 'select name from sysobjects where xtype = ''u'''
insert @word
exec (@lstr)
select * from @word



Bhuvnesh
Bhuvnesh
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21502 Visits: 4079
thanks

but i cant take table variable or temp table ? i need to use local variable .


but if atlast i go with local variable will there be performance difference between local variable and table variable ?

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
Ken McKelvey
Ken McKelvey
SSCertifiable
SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)

Group: General Forum Members
Points: 6452 Visits: 8617
Use sp_executesql with an OUTPUT parameter.

DECLARE @word sysname

EXEC sp_executesql
    N'SELECT TOP 1 @DynamicWord = [name] FROM sysobjects'
    ,N'@DynamicWord sysname OUTPUT'
    ,@word OUTPUT

SELECT @word


Bhuvnesh
Bhuvnesh
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21502 Visits: 4079
hi ken,

thanks it worksSmile

but can u explain the whole scenario.

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
venu_ksheerasagaram
venu_ksheerasagaram
SSChasing Mays
SSChasing Mays (615 reputation)SSChasing Mays (615 reputation)SSChasing Mays (615 reputation)SSChasing Mays (615 reputation)SSChasing Mays (615 reputation)SSChasing Mays (615 reputation)SSChasing Mays (615 reputation)SSChasing Mays (615 reputation)

Group: General Forum Members
Points: 615 Visits: 243
Hi All,

Here i am having a doubt regarding the Dynamic Query. My requirement is How to store the Dynamic Query resultset into a TempTable? Could it possibel?

Because i need to fire a Search Query on the result set that was generated by the Dyamic query.

Thank you all,

Venu Gopal.K
Software Engineer
INDIA
pradipcjain
pradipcjain
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 7
with small correction...

declare @lstr varchar(200)
declare @word table (tableName varchar (50))
set @lstr = 'select top 1 name from sysobjects'
insert @word
exec(@lstr)
select * from @word

:-)
Adam Gojdas
Adam Gojdas
Mr or Mrs. 500
Mr or Mrs. 500 (542 reputation)Mr or Mrs. 500 (542 reputation)Mr or Mrs. 500 (542 reputation)Mr or Mrs. 500 (542 reputation)Mr or Mrs. 500 (542 reputation)Mr or Mrs. 500 (542 reputation)Mr or Mrs. 500 (542 reputation)Mr or Mrs. 500 (542 reputation)

Group: General Forum Members
Points: 542 Visits: 1466
Bhuvnesh (11/11/2008)
hi ken,

thanks it worksSmile

but can u explain the whole scenario.



Here this may help a little:

DECLARE @word sysname

EXEC sp_executesql
N'SELECT TOP 1 @DynamicWord = [name] FROM sysobjects' --dynamic sql query to execute
,N'@DynamicWord sysname OUTPUT' --parameter definitions
,@DynamicWord=@word OUTPUT --assigning the caller procs local variable to the dynamic parameter

SELECT @word


Adam Gojdas
Adam Gojdas
Mr or Mrs. 500
Mr or Mrs. 500 (542 reputation)Mr or Mrs. 500 (542 reputation)Mr or Mrs. 500 (542 reputation)Mr or Mrs. 500 (542 reputation)Mr or Mrs. 500 (542 reputation)Mr or Mrs. 500 (542 reputation)Mr or Mrs. 500 (542 reputation)Mr or Mrs. 500 (542 reputation)

Group: General Forum Members
Points: 542 Visits: 1466
venu_ksheerasagaram (11/1/2009)
Hi All,

Here i am having a doubt regarding the Dynamic Query. My requirement is How to store the Dynamic Query resultset into a TempTable? Could it possibel?

Because i need to fire a Search Query on the result set that was generated by the Dyamic query.

Thank you all,


Here is a possible way to do that:


CREATE TABLE #TempLocal (MyName sysname);

EXEC sp_executesql
N'INSERT INTO #TempLocal (MyName) SELECT [name] FROM sysobjects'; --dynamic sql query to execute

SELECT MyName FROM #TempLocal;
DROP TABLE #TempLocal;


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search