问题
I created a trigger named UPDATE_MERCHANDISE
. The query completed with one error, a simple typo. I fixed the typo and changed the code to ALTER TRIGGER
, but now when I try to execute it I receive the error
Invalid object name [dbo].[UPDATE_MERCHANDISE]
Why would it be valid when I created it, but not when I try to alter it? Any and all help/ideas would be appreciated.
Thanks.
USE [CIS31038]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[UPDATE_MERCHANDISE] ON [dbo].[ORDERITEM]
AFTER INSERT, DELETE, UPDATE
AS
BEGIN
DECLARE @ITEMID VARCHAR(2)
DECLARE @TOTAL INT
--INSERT CASE
IF(EXISTS (SELECT * FROM INSERTED) AND NOT EXISTS (SELECT * FROM DELETED))
BEGIN
DECLARE INSERT_CURSOR CURSOR FOR
SELECT ITEMID, SUM(QUANTITY) AS TOTAL
FROM INSERTED
GROUP BY ITEMID
OPEN INSERT_CURSOR
FETCH NEXT FROM INSERT_CURSOR INTO @ITEMID, @TOTAL
WHILE(@@FETCH_STATUS = 0)
BEGIN
UPDATE MERCHANDISE
SET QUANTITYONHAND = QUANTITYONHAND - @TOTAL
WHERE ITEMID = @ITEMID
FETCH NEXT FROM INSERT_CURSOR INTO @ITEMID, @TOTAL
END
CLOSE INSERT_CURSOR
DEALLOCATE INSERT_CURSOR
END
--DELETE CASE
IF(EXISTS (SELECT * FROM DELETED) AND NOT EXISTS (SELECT * FROM INSERTED))
BEGIN
DECLARE DELETE_CURSOR CURSOR FOR
SELECT ITEMID, SUM(QUANTITY) AS TOTAL
FROM DELETED
GROUP BY ITEMID
OPEN DELETE_CURSOR
FETCH NEXT FROM DELETE_CURSOR INTO @ITEMID, @TOTAL
WHILE (@@FETCH_STATUS = 0)
BEGIN
UPDATE MERCHANDISE
SET QUANTITYONHAND = QUANTITYONHAND + @TOTAL
WHERE ITEMID = @ITEMID
FETCH NEXT FROM DELETE_CURSOR INTO @ITEMID, @TOTAL
END
CLOSE DELETE_CURSOR
DEALLOCATE DELETE_CURSOR
END
--UPDATE CASE
IF(EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED))
BEGIN
DECLARE UPDATE_CURSOR CURSOR FOR
SELECT I.ITEMID, SUM(I.QUANTITY - D.QUANTITY) AS TOTAL
FROM INSERTED I INNER JOIN DELETED D ON
I.PONUMBER = D.PONUMBER AND I.ITEMID = D.ITEMID
GROUP BY I.ITEMID
OPEN UPDATE_CURSOR
FETCH NEXT FROM UPDATECURSOR INTO @ITEMID, @TOTAL
WHILE (@@FETCH_STATUS = 0)
BEGIN
UPDATE MERCHANDISE
SET QUANTITYONHAND = QUANTITYONHAND - @TOTAL
WHERE ITEMID = @ITEMID
FETCH NEXT FROM UPDATE_CURSOR INTO @ITEMID, @TOTAL
END
CLOSE UPDATE_CURSOR
DEALLOCATE UPDATE_CURSOR
END
END
回答1:
If you had a typo, most likely, the trigger never got created
Try CREATE TRIGGER instead of ALTER TRIGGER
By the way, generally, you remove the trigger and recreate, using something like.
IF Object_id('TRIGGER_NAME') is not null
DROP TRIGGER Trigger Name
GO
CREATE TRIGGER TriggerName
AS
来源:https://stackoverflow.com/questions/27156053/invalid-object-name-dbo-triggername