I want to write an select query using OPENQUERY in SQL server where i have to select the records from linked server but in where clause i have to compare the dates but the date should be from a table present in current DB not in linked server DB. For example: Select * from OPENQUERY(Linked_Server,'select * from db.table_name where Edate>(select edate from CurrentDB)')
我想在SQL服务器中使用OPENQUERY编写一个选择查询,我必须从链接服务器中选择记录,但在where子句中我必须比较日期,但日期应该来自当前数据库中不存在于链接服务器数据库中的表。例如:从OPENQUERY中选择*(Linked_Server,'select * from db.table_name where Edate>(从CurrentDB中选择edate)')
so please help me how in can pass the parameters from current Database server to linked Database server
所以请帮我如何将参数从当前数据库服务器传递到链接数据库服务器
2 个解决方案
#1
0
Hiii its not necessary to use OPENQUERY you can query in this way
Hiii没必要使用OPENQUERY,你可以用这种方式查询
select * from "linkservername"."DB NAME"."Tablename" where Edate > (select edate from CurrentDB)
try this.
尝试这个。
#2
0
Here you can find different ways to do it: https://support.microsoft.com/en-us/help/314520/how-to-pass-a-variable-to-a-linked-server-query
在这里您可以找到不同的方法:https://support.microsoft.com/en-us/help/314520/how-to-pass-a-variable-to-a-linked-server-query
So, for example, in your case it can be smth like this:
所以,例如,在你的情况下,它可以像这样:
declare @dt char(8) = (select convert(char(8), edate, 112) from CurrentDB) -- this should return 1 value only of date type
declare @sql varchar(4000) =
'select *
from openquery(MyLinkedServer, ''select * from db.table_name where Edate>''''' + @dt + ''''''')';
exec(@sql);
#1
0
Hiii its not necessary to use OPENQUERY you can query in this way
Hiii没必要使用OPENQUERY,你可以用这种方式查询
select * from "linkservername"."DB NAME"."Tablename" where Edate > (select edate from CurrentDB)
try this.
尝试这个。
#2
0
Here you can find different ways to do it: https://support.microsoft.com/en-us/help/314520/how-to-pass-a-variable-to-a-linked-server-query
在这里您可以找到不同的方法:https://support.microsoft.com/en-us/help/314520/how-to-pass-a-variable-to-a-linked-server-query
So, for example, in your case it can be smth like this:
所以,例如,在你的情况下,它可以像这样:
declare @dt char(8) = (select convert(char(8), edate, 112) from CurrentDB) -- this should return 1 value only of date type
declare @sql varchar(4000) =
'select *
from openquery(MyLinkedServer, ''select * from db.table_name where Edate>''''' + @dt + ''''''')';
exec(@sql);