无效的对象名称[dbo]。[TriggerName]

时间:2014-11-26 18:06:34

标签: sql-server object triggers alter

我创建了一个名为UPDATE_MERCHANDISE的触发器。查询完成时出现一个错误,一个简单的拼写错误。我修正了拼写错误并将代码更改为ALTER TRIGGER,但现在当我尝试执行它时,我收到错误

  

无效的对象名称[dbo]。[UPDATE_MERCHANDISE]

为什么我创建它时会有效,但是当我尝试改变它时却没有?任何和所有的帮助/想法将不胜感激。

感谢。

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 个答案:

答案 0 :(得分:1)

如果你有一个拼写错误,很可能,触发器永远不会被创建

Try CREATE TRIGGER instead of ALTER TRIGGER

顺便说一句,一般来说,你删除触发器并使用类似的东西重新创建。

IF Object_id('TRIGGER_NAME') is not null
    DROP TRIGGER Trigger Name
GO
CREATE TRIGGER TriggerName
AS