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
SSCertifiable
SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)

Group: General Forum Members
Points: 6876 Visits: 4076
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
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2006 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
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1889 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
SSCertifiable
SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)

Group: General Forum Members
Points: 6876 Visits: 4076
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
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2182 Visits: 8117
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
SSCertifiable
SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)

Group: General Forum Members
Points: 6876 Visits: 4076
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
SSC Veteran
SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)

Group: General Forum Members
Points: 229 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
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 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
SSC-Enthusiastic
SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)

Group: General Forum Members
Points: 184 Visits: 1441
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
SSC-Enthusiastic
SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)

Group: General Forum Members
Points: 184 Visits: 1441
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