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)
 SQL

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2007-02-28 : 08:04:46
Maryam writes "Hi
Could I set a paramat in front of from cluse in select?

for instance:

Declare @tbl sysname
Declare @Str Nvarchar(400)

Set @tbl = pub.dbo.Athur'
Set @str = 'SELECT * FROM ' + @tbl

Select * from @str

OR

SELECT *
FROM OPENROWSET('SQLOLEDB','192.168.0.1';'sa';'sa', @str)"

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2007-02-28 : 10:49:09
You need to use dynamic SQL. Here's how to do it (but note that dynamic SQL is usually not necessary and you should avoid it unless absolutely necessary, there are lots of posts about dynamic SQL on these forums).

Declare @tbl sysname
Declare @Str Nvarchar(400)

Set @tbl = 'pubs.dbo.Authors'
Set @str = 'SELECT * FROM ' + @tbl
EXEC (@str)
--OR
EXEC sp_executesql @str
Go to Top of Page
   

- Advertisement -