Connecting Tech Pros Worldwide Help | Site Map

Open SQL connection using T-SQL

Newbie
 
Join Date: Nov 2009
Posts: 2
#1: 2 Weeks Ago
I would like to connect to a remote sql server using a stored procedure. I had planned on using OPENQUERY( linked_server ,'query' ) to pull data from the remote server but this command does not accept variables for its arguments. Is there another way to open a connection using Transact-SQL?

Thanks for your help!
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#2: 2 Weeks Ago

re: Open SQL connection using T-SQL


Will this work:

Expand|Select|Wrap|Line Numbers
  1.  
  2. exec ('select * from linked_server.' + @dbname + '.dbo.' + @tablename + ' where blah blah order by blah blah'
  3.  
  4.  
That's just a pseudo-code...

Happy Coding!!!


--- CK
Newbie
 
Join Date: Nov 2009
Posts: 2
#3: 2 Weeks Ago

re: Open SQL connection using T-SQL


I guess I just needed to treat it like any other query but include the linked server name in the string.

I'm still pretty wet behind the ears. Thanks so much for your help ck9663. This was my first visit to this sight. I appreciate you taking the time to reply.

- Muddler
Reply

Tags
connection string, openquery, sql connection, t-sql