我有一个填充表格,其中'Number'为PK
。我想查询一个搜索特定数字的查询,如果找不到,那么它将返回“NULL”而不是no值。
我已经设法做了一次回归:
SELECT (SELECT Risk_Impact FROM [dbo].[RFC] WHERE Number = 'RFC-018345')
但是我想选择多个列,如:
SELECT (SELECT Risk_Impact, Risk_Impact, BI_TestingOutcome FROM [dbo].[RFC] WHERE Number = 'RFC-018345')
然而它给了我一个错误:
“Msg 116,Level 16,State 1,Line 1 当子查询未与EXISTS一起引入时,只能在选择列表中指定一个表达式。“
你能帮忙吗?
提前谢谢
答案 0 :(得分:4)
尝试
select p.* from (select 1 as t ) v
left join (select * from [dbo].[RFC] WHERE Number = 'RFC-018345') p
on 1=1
答案 1 :(得分:0)
在sql server中应该使用像这样的isnull函数
SELECT isnull((SELECT Risk_Impact FROM [dbo].[RFC] WHERE Number = 'RFC-018345'),0)
答案 2 :(得分:-1)
精制@Sagar查询:
更好的缩进可能对人们学习更有帮助。
<击> 例如: 选择 [DBO]。[RFC]。* 从 (SELECT 1 AS Col1)AS V. 交叉加入 [DBO]。[RFC] 数字=&#39; RFC-018345&#39; 击>
编辑:我陷入了删除子查询的过程中。工作查询:
SELECT
[dbo].[RFC].*
FROM
(SELECT 1 AS Col1) AS V
LEFT JOIN
[dbo].[RFC]
ON Number = 'RFC-018345'
(我还提倡不要SELECT *,在生产附近,但要始终明确结果集中的列。例如:
SELECT
[dbo].[RFC].[Risk_Impact],
[dbo].[RFC].[BI_TestingOutcome]
FROM
(SELECT 1 AS Col1) AS V
LEFT JOIN
[dbo].[RFC]
ON Number = 'RFC-018345'
)