gpt4 book ai didi

sql-server-2008 - 有没有办法让失败的 SELECT 不被扔进 TRY/CATCH?

转载 作者:行者123 更新时间:2023-12-01 11:00:21 24 4
gpt4 key购买 nike

例如:

CREATE PROCEDURE dbo.MyProc
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
...
DECLARE @Id INT;

-- I don't want the following line to throw and abort processing.
-- I just want @Id to remain NULL. I can add a nested TRY/CATCH, but is there
-- a better way?
SET @Id=(SELECT Id FROM MyTable WHERE ...);
...
END TRY
BEGIN CATCH
...
END CATCH;
END;

更新:澄清一下,如果返回多行,我希望@Id 保持为 NULL。

最佳答案

我会做这样的事情来避免多行错误:

SELECT top 1 @Id = Id FROM MyTable WHERE ...

或者这个

SELECT @Id = Id FROM MyTable WHERE ...
if @@rowcount > 1
set @id = null

当有多行时,最后一段会将@Id 设置为空。

关于sql-server-2008 - 有没有办法让失败的 SELECT 不被扔进 TRY/CATCH?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11548013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com