Creating Server String | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

Creating Server String

Hello Everyone, I am facing one typical issue. I want to execute one SELECT query but i am getting variable not declare error. Usually i am getting my servername in local variable like @servername and now i am joining servername with DB and table name as below declare @servername varchar(50) set @servername =’databaseserver’
Select * from @servername.dbname.dbo.Tablename I am sure why i am getting variable error because after FROM system aspects table data type but i am having varchar data type. Please let me know how to fix this issue Thanks and Regards Ravi K
You need to execute this dynamically via EXEC() or sp_ExecuteSQL. Search the forum. Many, many threads about dynamic sql. —
Frank Kalis
Microsoft SQL Server MVP
http://www.insidesql.de
Ich unterstütze PASS Deutschland e.V. http://www.sqlpass.de)

Read this also
http://www.sommarskog.se/dynamic_sql.html Madhivanan Failing to plan is Planning to fail
]]>