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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Variable in the FROM Clause 1

Status
Not open for further replies.

Andel

Programmer
Feb 15, 2001
366
US
Is there anyone who can tell me how to use variable in the FROM clause? Ex.

declare @tblname varchar(50)
set @tblname = mytable
select * from @tblname

The myTable has some records but this query doesn't give any result. Can somebody tell me the trick? Thanks!
 
You have to build the string and then run it using exec (ie. dynamically).

select @dynamicQuery = 'select * from ' + @tblname
exec @dynamicQuery

Give this a shot.

JB
 
JB, it works... Thank you!

Maingel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top