Help needed in sqlservers connection

  • Hi,

    I have two database in different servers and i need to access one db object in another db. for ex, server1 (db1) and server2(db2(tableA,tableB) )

    I will be writing a stored procedure in in db1 and i wanted to access the TableA's data of db2. i am aware of using the linked server. but i am, looking for alternate for linked server because i need to pass credentials at runtime to connect the server and access the objects.

    Ex:

    Create Procedure Test(@server varchar(50),@username varchar(20), @pwd varchar(20))

    as

    Begin

    select * from server2.db2.tableA;

    End

    the alternate i found is OpenQuery. not sure how should i accomplish using openquery and am not sure about the drawback of using this. please suggest me the best way to achieve this and if possible show me some sample.

    Thanks in advance

  • KGJ-Dev (7/7/2014)


    I will be writing a stored procedure in in db1 and i wanted to access the TableA's data of db2. i am aware of using the linked server. but i am, looking for alternate for linked server because i need to pass credentials at runtime to connect the server and access the objects.

    I'd still use the Linked Server. If you're looking to have the linkage impersonate someone, you can do that during linked server setup if it's generic. If you want to setup as a specific user so you can control your impersonation, use the EXECUTE AS clause on a procedure.

    You don't want to set it up in a way where you're going to pass in a name/password for the other server on every call. It's, at best, painful.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Hi Craig,

    Thanks for your reply. I will not be using this mechanism often. also i am informed to use non linked server that's why i started to do the research. Finally i tried with open Rowset and i am able to acheive with concern. My concern is , i tried in my local database and for just returning 4 rows it took 00:00:01 by means 1 second which is bad. because my table has only 4 rows. so to return 4 rows it should not take 1 second. any suggestion please

    My code:

    declare @sql nvarchar(max)

    select @sql = 'select a.* from openrowset(''MSDASQL'', ''Driver={SQL SERVER}; Server=testserver;UID=sa; PWD=loginpwd;'', ''select * from [sample].dbo.Company'') as a'

    exec sp_executeSQL @sql

  • can anyone please help me in this regard

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply