Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Old Forums
 CLOSED - General SQL Server
 How Can I Use ....

Author  Topic 

tanvirasad
Starting Member

10 Posts

Posted - 2006-11-03 : 04:34:49
I have table where I have stored the SQL Statement when I fire the query on that record it shoul run the statement which is stored in that column.

e.g
INSERT INTO MyTable ('Select MyCol From MySecondTable')

When I Run
"Select * From MyTable"
It Should Run
"Select MyCol From MySecondTable"

Tanvir Asad

madhuotp
Yak Posting Veteran

78 Posts

Posted - 2006-11-03 : 05:40:15
hi,

I think you should explain the scenario....... Here , u r expecting a lot from a single statement..... What i understand is you have to use dynamic sql. sp_executesql kind of stuff

Madhu
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-03 : 06:01:20
http://www.soommarskog.se/dynamic_sql.html

declare @sqlcmd varchar(8000)
select @sqlcmd = mycol
from mytable
where mypk = 2

exec (@sqlcmd)


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-11-03 : 06:46:41
why did you store queries in a column?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-03 : 12:53:45
It is not that uncommon. I have to do it too in one of my projects
I have made some kind of report generator, where the users can select a number of options.
The finished query is saved in clear text, and filed under a user selected named for easy reuse.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-11-04 : 06:29:17
quote:
Originally posted by Peso

It is not that uncommon. I have to do it too in one of my projects
I have made some kind of report generator, where the users can select a number of options.
The finished query is saved in clear text, and filed under a user selected named for easy reuse.


Peter Larsson
Helsingborg, Sweden


I see

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

tanvirasad
Starting Member

10 Posts

Posted - 2006-11-06 : 03:55:30
Hi,

Thanks for the replies but I need some more detailed coz I don't want to change my frontend application

Here I explain The Scenario

Table1
ID Name Addr
1 ABC 1-ABC
2 DEF 2-XYZ
3 GHI 3-DEF

Table2
ID Address2
1 Select Addr From Table1 Where ID =1
2 Select Addr From Table1 Where ID =2
3 Select Addr From Table1 Where ID =3

Now Execution
When I Hit a Query on Table2
Select Address2 From Table2 Where ID = 1

It Should Return The Address From Table1 By Executing The Query




Tanvir Asad
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-06 : 04:37:38
As I wrote in my previous answer...
declare @sqlcmd varchar(8000)
select @sqlcmd = address2
from table2
where id = 2

exec (@sqlcmd)


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-11-06 : 06:29:15
oh yeah i didnt check this, while replying to other post.. :-(

We also have a report writer where we save the query in the database, but then we normally call everything from the front end.



Chirag

http://chirikworld.blogspot.com/
Go to Top of Page
   

- Advertisement -