Linked server 如何为OPENDATASOURCE传递参数

Linked server 如何为OPENDATASOURCE传递参数,linked-server,parameter-passing,Linked Server,Parameter Passing,我可以通过以下方式连接到链接服务器: 选择testNo、soruTuruId、soruNo、cevap、degerlendirenTcNo、degerlendirilenTcNo 来自OPENDATASOURCE('SQLOLEDB','Data Source=192.168.150.42;User ID=readerUser;Password=1')。akreditasyon.dbo.tblPerfCevap 但我必须将密码作为参数传递。我试着这样做: SET@connectionString

我可以通过以下方式连接到链接服务器:

选择testNo、soruTuruId、soruNo、cevap、degerlendirenTcNo、degerlendirilenTcNo 来自OPENDATASOURCE('SQLOLEDB','Data Source=192.168.150.42;User ID=readerUser;Password=1')。akreditasyon.dbo.tblPerfCevap

但我必须将密码作为参数传递。我试着这样做:

SET@connectionString='数据源=192.168.150.42;用户ID=读卡器;密码='+@pw

选择testNo、soruTuruId、soruNo、cevap、degerlendirenTcNo、degerlendirilenTcNo 来自OPENDATASOURCE('SQLOLEDB',@connectionString.).akreditasyon.dbo.tblPerfCevap

选择testNo、soruTuruId、soruNo、cevap、degerlendirenTcNo、degerlendirilenTcNo 从OPENDATASOURCE('SQLOLEDB','Data Source=192.168.150.42;User ID=readerUser;Password='+@pw)。akreditasyon.dbo.tblPerfCevap

但是没有起作用:S


有人有想法吗?

使用exec()函数运行查询。将查询包装成有效字符串。

以下是我对Ojulari答案的工作实现。我希望从我们的Prod、QA等运行相同的存储过程。。环境中,每个服务器都需要访问不同的BizTalk Server数据库

declare @DataSource varchar(100)
set @DataSource = 
    case 
        when dbo.GetEnvironment() = 'PROD' then 'Data Source=ProdBizTalkServer;UID=test;PWD=test'
        when dbo.GetEnvironment() = 'QA'   then 'Data Source=QABizTaklServer;UID=test;PWD=test'
        ELSE null
    end       
declare @myExec varchar(max) 
set @myExec =   'select 
                    nvcMessageType,
                    COUNT(*) 
                    from OpenDataSource(''SQLNCLI10'',''' + @DataSource + ''').BizTalkMsgBoxDb.dbo.Spool
                    where nvcMessageType like ''%#FlightMessageReceivedEvent''
                    group BY nvcMessageType
                    order by nvcMessageType 
            '  
print '@myExec=' + IsNull(@myExec,'null') 

EXEC(@myExec)