|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, February 14, 2013 11:01 PM
Points: 20,
Visits: 48
|
|
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'"
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Tuesday, March 26, 2013 8:41 AM
Points: 2,562,
Visits: 3,451
|
|
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---------- While 1 = 1 (Learning SQL....) Click to get fast response of your post
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, February 14, 2013 11:01 PM
Points: 20,
Visits: 48
|
|
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..
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Tuesday, March 26, 2013 8:41 AM
Points: 2,562,
Visits: 3,451
|
|
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---------- While 1 = 1 (Learning SQL....) Click to get fast response of your post
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, February 14, 2013 11:01 PM
Points: 20,
Visits: 48
|
|
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..
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 4:15 PM
Points: 37,651,
Visits: 29,903
|
|
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 2008, MVP 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
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, February 14, 2013 11:01 PM
Points: 20,
Visits: 48
|
|
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.. :) 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..
|
|
|
|