我正在尝试创建一个脚本,我正在为联系人搜索数百个代理号码,如果没有结果返回,我需要一个空值。
例如,以下脚本将返回
AgentNumber | Description | StartDate
------------------------------------------------
51702 | Terminal | 1900-01-01 00:00:00
10676 | RAMA | 1900-01-01 00:00:00
10585 | RAMA | 1900-01-01 00:00:00
我需要的是显示的结果是表格中没有数据的地方:
AgentNumber | Description | StartDate
------------------------------------------------
51702 | Terminal | 1900-01-01 00:00:00
10676 | RAMA | 1900-01-01 00:00:00
10585 | RAMA | 1900-01-01 00:00:00
10001 | NULL | NULL
SET QUERY_GOVERNOR_COST_LIMIT 990
SELECT AgentNumber,description,startdate
FROM dbsSupport.dbo.ContractsIreland2
WHERE AgentNumber IN (51702,10676,10585,10001)
答案 0 :(得分:0)
据推测,您使用的是基于SET
变量的SQL Server。
问题的解决方案是LEFT JOIN
。你可以这样做:
SELECT v.AgentNumber, ci.description, ci.startdate
FROM (VALUES (51702), (10676), (10585), (10001)
) v(AgentNumber)
dbsSupport.dbo.ContractsIreland2 ci
ON ci.AgentNumber = v.AgentNumber;
答案 1 :(得分:0)
您正在寻找某种look-up
表
select l.AgentNumber, c.description, c.startdate
from ( select 51702 as AgentNumber union
select 10676 as AgentNumber union
select 10585 as AgentNumber union
select 10001 as AgentNumber) l left join dbsSupport.dbo.ContractsIreland2 c
on c.AgentNumber = l.AgentNumber;