如果嵌套查询没有返回结果,则将变量设置为 NULL
假设您有以下查询。如果嵌套查询返回 NULL(0 个结果),存储过程将崩溃并出现下面提到的错误。我发现我可以在下面的替代查询中重写代码,但我想找到一种更简单的语法来编写它。我有大约 10 个这样的查询,其中一些有多个嵌套查询。有没有更简单的方法来编写它们?我不是 SQL 专家,所以我一直在寻找建议!谢谢。
查询(有时会崩溃):
SET @sampleid = (
SELECT
[sampleid]
FROM [sample]
WHERE [identifyingnumber] = @sample_identifyingnumber
查询错误:
Subquery returned more than 1 value. This is not permitted when the subquery
follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
永不崩溃的替代查询:
IF
(
SELECT
COUNT([sampleid])
FROM [sample]
WHERE [identifyingnumber] = @sample_identifyingnumber
) = 0
BEGIN
SET @sampleid = NULL
END
ELSE
BEGIN
SET @sampleid =
(
SELECT
DISTINCT [sampleid]
FROM [sample]
WHERE [identifyingnumber] = @sample_identifyingnumber
)
END
============ ===
更复杂的示例:
SET @testcodeid = (
SELECT
[testcodeid]
FROM [testcode]
WHERE [testcode].[name] = (
SELECT [test_code]
FROM [ws_test_request]
WHERE [client_id] = @clientid
AND [sample_specimen_id] = @sample_identifyingnumber
)
);
Suppose you have the following query. If the nested query returns NULL
(0 results), the stored procedure crashes with the following error mentioned below. I found out I can re-write the code in the Alternative query below, but I'm wanting to find an easier syntax to write it. I have about 10 of these, and some have multiple nested queries. Is there an easier way to write them? I'm not an expert in SQL, so I'm always looking for suggestions! Thanks.
Query (that sometimes crashes):
SET @sampleid = (
SELECT
[sampleid]
FROM [sample]
WHERE [identifyingnumber] = @sample_identifyingnumber
Error from query:
Subquery returned more than 1 value. This is not permitted when the subquery
follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Alternative query that never crashes:
IF
(
SELECT
COUNT([sampleid])
FROM [sample]
WHERE [identifyingnumber] = @sample_identifyingnumber
) = 0
BEGIN
SET @sampleid = NULL
END
ELSE
BEGIN
SET @sampleid =
(
SELECT
DISTINCT [sampleid]
FROM [sample]
WHERE [identifyingnumber] = @sample_identifyingnumber
)
END
===============
Example that's more complex:
SET @testcodeid = (
SELECT
[testcodeid]
FROM [testcode]
WHERE [testcode].[name] = (
SELECT [test_code]
FROM [ws_test_request]
WHERE [client_id] = @clientid
AND [sample_specimen_id] = @sample_identifyingnumber
)
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
try
您将sample_table中不存在的记录设置为NULL,其中identifyingnumber等于sample_identifyingnumber:
您将sample_table记录中存在的字段sampleid设置为MIN(DISTINCT Sampleid),其中identifying_number = sample_identifyingnumber:
我已编写MIN(DISTINCT .. .)如果我们发现更多的样本ID对应于相同的识别号码:在这种不幸的情况下,我取X个(X严格> 1)不同样本ID中的最小值。
我需要两次更新而不是一次。
You set to NULL the records for which do not exist records in sample_table with identifyingnumber equal to sample_identifyingnumber:
You set to MIN (DISTINCT sampleid) the field sampleid for which exist in sample_table records where identifying_number = sample_identifyingnumber:
I have written MIN (DISTINCT ...) in case we find more sampleids corresponding to the same identifyingnumber: in this unlucky case, I take the MINIMUM of the X (with X strictly > 1) different sampleids.
I need two updates instead of one.