Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

let a inputvariable deside which table is queried...

Status
Not open for further replies.

ruupy

Programmer
Jan 12, 2001
29
NL

Hello,
A short SP problem this time.
What do you do when you want the table on which the query is run, to be dependant on an SP-input-variable?

an SP-user executes the SP in the following way :
**************
exec sp_test @whichtable = 'table_01'
**************


As part of the SP, the following...
**************
....
select * from @whichtable
...
**************
........is not accepted.

Does anybody have a slik idea /workaround?????


ThenX
RuupY
 
You have to use "dynamic SQL".

declare @sql varchar(1000)
set @sql = "select * from " + @whichtable
exec (@sql)
 

ThenX a lot.

Short
Sweet
'n
Simple

...worx like charm!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top