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.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2007-02-28 : 08:04:46
|
| Maryam writes "HiCould I set a paramat in front of from cluse in select?for instance:Declare @tbl sysnameDeclare @Str Nvarchar(400)Set @tbl = pub.dbo.Athur'Set @str = 'SELECT * FROM ' + @tbl Select * from @str ORSELECT *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 sysnameDeclare @Str Nvarchar(400)Set @tbl = 'pubs.dbo.Authors'Set @str = 'SELECT * FROM ' + @tbl EXEC (@str)--OREXEC sp_executesql @str |
 |
|
|
|
|
|