子查询返回超过 1 个值。当子查询跟随 = 或子查询用作表达式时,这是不允许的

Posted

技术标签:

【中文标题】子查询返回超过 1 个值。当子查询跟随 = 或子查询用作表达式时,这是不允许的【英文标题】:Subquery returned more than 1 value. this is not permitted when the subquery follows = or when the subquery is used as an expression 【发布时间】:2019-02-22 12:49:44 【问题描述】:

我想用这个查询更新我的“MakinelerVeParcalar”表;

UPDATE MakinelerVeParcalar SET Durum = 'Montaj' WHERE ID = 161

我得到了这个错误;

消息 512,级别 16,状态 1,过程 trgSureUpdate,第 31 行 [批处理开始第 0 行] 子查询返回超过 1 个值。当子查询跟随 =、!=、、>= 或子查询用作表达式时,这是不允许的。

我的 trgSureUpdate(触发器)

ALTER TRIGGER [dbo].[trgSureUpdate]
ON [dbo].[MakinelerVeParcalar]
AFTER UPDATE
AS 
BEGIN

    DECLARE @count INT
    DECLARE @idinserted INT
    DECLARE @duruminserted VARCHAR(50)
    DECLARE @max INT
    DECLARE @iddeleted INT
    DECLARE @durumdeleted VARCHAR(50)
    DECLARE @datediff INT
    DECLARE @durumbilgisi varchar(50)
    DECLARE @sureinserted INT
    DECLARE @suredeleted INT
    DECLARE @diffdate INT

    SELECT @idinserted = ID from inserted  <-- Line 31
    SELECT @duruminserted = Durum from inserted
    SELECT @iddeleted = ID from deleted
    SELECT @durumdeleted = Durum from deleted

    SET @count = (SELECT count(*) FROM Sure WHERE ID = @idinserted and Asama = @duruminserted)
    SET @max = (SELECT max(SiraNo) FROM Sure WHERE ID = @idinserted)
    SET @durumbilgisi = (SELECT DurumBilgisi FROM DurumBilgisi WHERE ID = @idinserted)
    SET @sureinserted = (SELECT Sure FROM Sure WHERE ID = @idinserted and Asama = @duruminserted)
    SET @suredeleted = (SELECT Sure FROM Sure WHERE ID = @iddeleted and Asama = @durumdeleted)

    IF @duruminserted != @durumdeleted
    BEGIN


        IF @durumbilgisi != 'Bitti'
        BEGIN
            UPDATE Sure Set Cikis = GETDATE() WHERE Asama = @durumdeleted and ID = @idinserted and SiraNo = @max
            SET @diffdate = DATEDIFF (SECOND,(SELECT Giris FROM Sure WHERE ID = @idinserted and SiraNo = @max),(SELECT Cikis FROM Sure WHERE ID = @idinserted and SiraNo = @max))
            UPDATE Sure SET Sure = (@diffdate) WHERE ID = @idinserted and SiraNo = @max
        END
        INSERT INTO Sure (ID,Asama,Giris,Cikis,Sure,SiraNo) VALUES(@idinserted,@duruminserted,GETDATE(),NULL,0,(@max+1))
        UPDATE DurumBilgisi SET DurumBilgisi = 'Devam Ediyor' WHERE ID = @idinserted
    END
END

【问题讨论】:

inserteddeleted 是(虚拟)表。您没有考虑到它们可能有多行,因此您的触发代码只是伪造的。 你能给我样品吗? Microsoft 文档中充满了示例。 . . docs.microsoft.com/en-us/sql/t-sql/statements/…。网络上还有无数的例子。 无法解决:( 【参考方案1】:

我认为您可能对行号感到困惑。您已将此标记为第 31 行,但即使 INSERTED 上有多行,它也不会抛出您的错误:

SELECT @idinserted = ID from inserted

但是,如果 SELECT 返回超过 1 行,这将引发您的错误:

SET @durumbilgisi = (SELECT DurumBilgisi FROM DurumBilgisi WHERE ID = @idinserted)

select @var = col from table 语法和set @var = (select col from table) 语法之间存在差异。您需要检查 SET 查询。

【讨论】:

以上是关于子查询返回超过 1 个值。当子查询跟随 = 或子查询用作表达式时,这是不允许的的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server 触发器:子查询返回超过 1 个值

子查询返回的值不止一个.当子查询跟随在 =,!=,<,<=,>,>= 之后,或子查询用作

SQL Server 子查询返回超过 1 个值。子查询遵循 =, !=, <, <= , >, >=

如何使用 SQL 中的 Case 语句将数据插入临时表

子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,不允许?

SQL问题,子查询返回的值不止一个!