如果嵌套查询没有返回结果,则将变量设置为 NULL

发布于 2024-11-06 19:27:54 字数 1303 浏览 5 评论 0原文

假设您有以下查询。如果嵌套查询返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我偏爱纯白色 2024-11-13 19:27:55

尝试

SELECT @sampleid = [sampleid]
FROM [sample]
WHERE [identifyingnumber] = @sample_identifyingnumber

try

SELECT @sampleid = [sampleid]
FROM [sample]
WHERE [identifyingnumber] = @sample_identifyingnumber
り繁华旳梦境 2024-11-13 19:27:55

您将sample_table中不存在的记录设置为NULL,其中identifyingnumber等于sample_identifyingnumber:

UPDATE my_table
   SET sampleid = NULL
 WHERE NOT EXISTS
          (SELECT 'X'
             FROM sample_table
            WHERE identifyingnumber = sample_identifyingnumber);

您将sample_table记录中存在的字段sampleid设置为MIN(DISTINCT Sampleid),其中identifying_number = sample_identifyingnumber:

UPDATE my_table
   SET sampleid =
          (SELECT MIN (DISTINCT sampleid)
             FROM sample_table
            WHERE identifyingnumber = sample_identifyingnumber)
 WHERE EXISTS
          (SELECT 'X'
             FROM sample_table
            WHERE 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:

UPDATE my_table
   SET sampleid = NULL
 WHERE NOT EXISTS
          (SELECT 'X'
             FROM sample_table
            WHERE identifyingnumber = sample_identifyingnumber);

You set to MIN (DISTINCT sampleid) the field sampleid for which exist in sample_table records where identifying_number = sample_identifyingnumber:

UPDATE my_table
   SET sampleid =
          (SELECT MIN (DISTINCT sampleid)
             FROM sample_table
            WHERE identifyingnumber = sample_identifyingnumber)
 WHERE EXISTS
          (SELECT 'X'
             FROM sample_table
            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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文