子查询返回的值超过1。当子查询后跟=或将子查询用作表达式时,不允许这样做

时间:2019-02-22 12:49:44

标签: sql-server tsql sql-update subquery

我要使用此查询在“ MakinelerVeParcalar”表上进行更新;

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

我得到了这个错误;

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

我的trgSureUpdate(Trigger)

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

1 个答案:

答案 0 :(得分:0)

我认为您可能对行号感到困惑。您已将此标记为第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查询。