我第二次问这个问题,但找不到答案,请帮忙。 我想用此查询更新我的'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
如果您需要表格,我可以发送所有表格。谢谢...
答案 0 :(得分:3)
您需要使用subquery
子句来限制top
中的行:
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 ??
??
排序列,用于指定您的列排序。
答案 1 :(得分:1)
您有两种选择。
首先:-如果返回相同的记录值,则写下来。
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)
第二次选择第一条记录
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)