过程中逻辑表(DELETED、INSERTED)的一些问题
问题描述:
我尝试在程序中使用逻辑表,但出现错误:
I try use logical tables in procedure but I have error:
无效的对象名称已删除"
"Invalid object name DELETED"
那么第一个问题我可以在程序中使用逻辑表吗?如果是,我该怎么做?
So first question can I use logical tables in procedure? If yes how can I do this?
这是一个代码
USE Operator
GO
CREATE PROCEDURE generuj ( @nazwaTabeli VARCHAR(20) )
AS
BEGIN
SET NOCOUNT ON;
--
-- Check if this is an INSERT, UPDATE or DELETE Action.
--
DECLARE @action as char(1);
SET @action = 'I'; -- Set Action to Insert by default.
IF EXISTS(SELECT * FROM DELETED) --Invalid object name DELETED
BEGIN
SET @action =
CASE
WHEN EXISTS(SELECT * FROM INSERTED) -- Invalid object name INSERTED
THEN 'U' -- Set Action to Updated.
ELSE 'D' -- Set Action to Deleted.
END
END
INSERT INTO TBLLOGI (UZYTKOWNIK,NAZWATABELI,DATAOPERACJI,TYPOPERACJI)
VALUES (SYSTEM_USER,@nazwaTabeli,SYSDATETIME(),@action)
END
我从这个主题中得到它点击一>我想在看起来像这样的触发器上使用这个过程
I have it from this topic Click I want use this procedure on trigger which looks like this
CREATE TRIGGER trgLogi
ON TBLABONENCI
AFTER INSERT, UPDATE, DELETE
AS
BEGIN
EXEC generuj TBLABONENCI
END;
有人可以帮我吗?
答
inserted
和 deleted
虚拟表仅在触发器中或作为 OUTPUT 子句的一部分可用.触发器正在调用一个过程,您应该先将它们保存到表中,以便在过程内部使用它们.
inserted
and deleted
virtual tables are available only in triggers or as part of OUTPUT clause.If your trigger is calling a procedure, you should save them to tables first to use them from inside the procedure.