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

passing server name as parameter

Status
Not open for further replies.

habneh

Programmer
Mar 21, 2007
55
US
I wrote a query which can be run on different servers

I want to pass the server name and database name as a parameter to my query.

how can I do that

can I do

select * from @servername.@databasename.dbo.tbl

Thanks
 
you have to use dynamic SQL to build a string with your server and database names in it. then execute the string.


- Paul
10qkyfp.gif

- If at first you don't succeed, find out if the loser gets anything.
 
Thank you paul,

can you give me a sample code whcih describes this

Thanks,
 
Here you go.

Code:
declare @server_name  varchar(255),
        @database_name varchar(255),    
        @sql varchar(max)

SELECT @server_name = 'MydbServer',
       @database_name = 'myDb'

SELECT @sql = 'SELECT top 1 * FROM '+ @server_name +'.'+@database_name+'.'+'dbo.Mytable'

exec(@sql)

- Paul
10qkyfp.gif

- If at first you don't succeed, find out if the loser gets anything.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top