Tsql 如果行存在或不存在,则显示布尔值';T

Tsql 如果行存在或不存在,则显示布尔值';T,tsql,Tsql,我有一个表[Contracts],表中有[id]、[number]列。我还有一些字符串格式的数字:“12342”,“23252”,“1256532”。我想得到这样的输出 1535325 | no 12342 | yes 23252 | yes 434574 | no 1256532 | yes 当然,我可以写这篇文章并获取我拥有的行,但是我如何确定行是否不存在并获取上面的输出: SELECT [Id] ,[Number] FROM [Contracts] wher

我有一个表[Contracts],表中有[id]、[number]列。我还有一些字符串格式的数字:“12342”,“23252”,“1256532”。我想得到这样的输出

1535325 | no
12342   | yes
23252   | yes
434574  | no
1256532 | yes
当然,我可以写这篇文章并获取我拥有的行,但是我如何确定行是否不存在并获取上面的输出:

SELECT [Id]
      ,[Number]
  FROM [Contracts]
  where [Number] in 
  ('12342', '23252', '1256532')

您可以将值放入临时表或表变量中,然后执行
左联接

declare @d table (Number varchar(10))
insert into @d values  ('12342'), ('23252'), ('1256532'), ('xxxx') -- last one is not in Contracts

SELECT c.[Id], c.[Number], case when d.Number is NULL then 'no' else 'yes' end [This Number from C is in D also]
FROM [Contracts] c
    left join @d d on d.Number = c.Number
对于“反向”使用
右连接

SELECT c.[Id], d.[Number], case when c.Number is NULL then 'no' else 'yes' end [This Number from D is in C also]
FROM [Contracts] c
    right join @d d on d.Number = c.Number

谢谢,但实际上我必须站在@d表的立场上,换句话说,如果@d表中有[Number]中没有的行,我必须显示No。我根据您的类比更改了脚本,但它不起作用