Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Executing sql query from select t_sql from table1


Executing sql query from select t_sql from table1

Author
Message
xmozart.ryan
xmozart.ryan
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 56
hello guys, can we get recordset from the query that we saved in the recordset ??

CREATE TABLE [dbo].[t_test] (
[t_sql] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
) ON [PRIMARY]
GO




insert into t_tes (t_sql,name) values ('select * from view1','select recordset from view1)



and then i want to call recordset from the query that i saved in t_test with this code :


DECLARE @query varchar(255)
SET @query = (select t_sql from t_test)
select * from (@query)



but i got err. msg "Must declare the variable '@query'"
Bhuvnesh
Bhuvnesh
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2930 Visits: 4076
xmozart.ryan (12/18/2012)

DECLARE @query varchar(255)
SET @query = (select t_sql from t_test)
select * from (@query)



but i got err. msg "Must declare the variable '@query'"
What yiu actually need here the query as result or data of query ?

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
xmozart.ryan
xmozart.ryan
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 56
Bhuvnesh (12/18/2012)
xmozart.ryan (12/18/2012)

DECLARE @query varchar(255)
SET @query = (select t_sql from t_test)
select * from (@query)



but i got err. msg "Must declare the variable '@query'"
What yiu actually need here the query as result or data of query ?


i need the data of query..
Bhuvnesh
Bhuvnesh
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2930 Visits: 4076
xmozart.ryan (12/19/2012)
Bhuvnesh (12/18/2012)
xmozart.ryan (12/18/2012)

DECLARE @query varchar(255)
SET @query = (select t_sql from t_test)
select * from (@query)



but i got err. msg "Must declare the variable '@query'"
What yiu actually need here the query as result or data of query ?


i need the data of query..

this will work

DECLARE @query varchar(255)
SET @query = ''
SET @query = 'select t_sql from t_test'
EXEC (@query)



-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
xmozart.ryan
xmozart.ryan
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 56
Bhuvnesh (12/19/2012)
xmozart.ryan (12/19/2012)
Bhuvnesh (12/18/2012)
xmozart.ryan (12/18/2012)

DECLARE @query varchar(255)
SET @query = (select t_sql from t_test)
select * from (@query)



but i got err. msg "Must declare the variable '@query'"
What yiu actually need here the query as result or data of query ?


i need the data of query..

this will work

DECLARE @query varchar(255)
SET @query = ''
SET @query = 'select t_sql from t_test'
EXEC (@query)


it's not like that.
i want to get record set from the query that i already saved on table t_test field t_sql,
i.e :
i already saved a sql query like "select * from table" into t_test field t_sql
after that i want to get that query and save it to variable , and then i want to get record set from that variable..
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47427 Visits: 44405
If I may suggest...

If this is just for experimentation, that's fine. If this is a design for an app, just don't do this. You're opening yourself to all sorts of complexities, SQL Injection being just one.

As for how you do this:

DECLARE @sSQL NVARCHAR(4000)
SELECT @sSQL = t_sql from t_test WHERE -- whatever condition to get the row you want.
EXEC (@sSQL)



Again, DO NOT go this route/design for a real application.


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


xmozart.ryan
xmozart.ryan
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 56
GilaMonster (12/19/2012)
If I may suggest...

If this is just for experimentation, that's fine. If this is a design for an app, just don't do this. You're opening yourself to all sorts of complexities, SQL Injection being just one.

As for how you do this:

DECLARE @sSQL NVARCHAR(4000)
SELECT @sSQL = t_sql from t_test WHERE -- whatever condition to get the row you want.
EXEC (@sSQL)



Again, DO NOT go this route/design for a real application.


It works,, thanks.. Smile
actually from last hour, i want to create a stored procedure which have a dynamic select.. and have a dynamic where too.. and it's really sick for me to create such a dynamic t-sql script...
so now, i create static stored procedure and it much easier to do.,, thx 4 ur advice man.. Cool
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