Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subquery returned more than 1 value. Cannot resolve

Tags:

sql

sql-server

I'm asking this problem for the second time, but can't find the answer please help. I want to update on my 'MakinelerVeParcalar' table with this query ;

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

And I got this error;

Msg 512, Level 16, State 1, Procedure trgSureUpdate, Line 31 [Batch Start Line 0] Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

My 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

If you need tables I can send all the tables. Thanks...

like image 708
Hasan Kaan TURAN Avatar asked Sep 13 '26 16:09

Hasan Kaan TURAN


2 Answers

You need to limit the rows in subquery by using top clause :

 SELECT TOP (1) @durumbilgisi = DurumBilgisi 
 FROM DurumBilgisi 
 WHERE ID = @idinserted
 ORDER BY ??

 SELECT TOP (1) @sureinserted = Sure 
 FROM Sure 
 WHERE ID = @idinserted and Asama = @duruminserted
 ORDER BY ??

 SELECT TOP (1) @suredeleted = Sure 
 FROM Sure 
 WHERE ID = @iddeleted and Asama = @durumdeleted
 ORDER BY ??

?? ordering column that specify your column ordering.

like image 190
Yogesh Sharma Avatar answered Sep 15 '26 04:09

Yogesh Sharma


You have two choices.

First:- if returns same records value then write down.

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

Second select first records

 SET @durumbilgisi = (SELECT TOP 1 DurumBilgisi FROM DurumBilgisi WHERE ID = @idinserted)
 SET @sureinserted = (SELECT TOP 1 Sure FROM Sure WHERE ID = @idinserted and Asama = @duruminserted)
 SET @suredeleted = (SELECT TOP 1 Sure FROM Sure WHERE ID = @iddeleted and Asama = @durumdeleted)
like image 32
Hemang A Avatar answered Sep 15 '26 05:09

Hemang A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!