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

dynamic from clause

Status
Not open for further replies.

alex1312

Programmer
Joined
Jun 17, 2002
Messages
7
Location
US
hi, how do i assign a declare value in from clause, for example

declare @a varchar(20)
set @a = 'some_table_name'
select * from @a

these statements will output an error, but how do i fix it with out using exec command. thanks for any help.
 
>> but how do i fix it with out using exec command >>

You can't, a table name is not an expression. Why this need? Do you have multiple idenntical tables or what?
 
Without an EXEC statement, you would need to do something like this.

If @tbl_name = 'Table1'
Begin
Select * From Table1
Return
End

If @tbl_name = 'Table2'
Begin
Select * From Table2
Return
End

... If statements and tables

If @tbl_name = 'TableN'
Begin
Select * From TableN
Return
End
If you want to get the best answer for your question read faq183-874 and faq183-3179.
Terry L. Broadbent - DBA
SQL Server Page:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top