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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Select statement with variable table name

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-07-23 : 07:28:13
Pierre writes "Hi there

Is it possible to use a select statement with a variable table name for eg.

select * from @variable table...

any ideas..?

Pierre"

dsdeming

479 Posts

Posted - 2003-07-23 : 08:28:03
You'll have to use dynamic SQL. See EXECUTE and sp_executesql in BOL for details.

Dennis
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-07-23 : 12:00:16
It's possible but not recommended..

USE Northwind
GO

DECALRE @SQL varchar(8000), @tbname sysname

SELECT @tbname = 'Orders'

SELECT @SQL = 'SELECT * FROM ' + @tbname

EXEC(@SQL)
GO

What are you doing though?

Remeber Dynamic SQL leads to the dark side...

trust your feelings..help you they will



Brett

8-)
Go to Top of Page
   

- Advertisement -