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...
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With