触发器如何工作?

发布于 2024-08-06 03:05:20 字数 1937 浏览 4 评论 0原文

使用 SQL Server 2000

我在新数据库中创建一个触发器

CREATE TRIGGER [CARDEVENTSAVING] ON [dbo].[T_CARDEVENT] 
AFTER INSERT 
AS

DECLARE @EDATE VARCHAR(50)
DECLARE @ETIME VARCHAR(50) 
DECLARE @EDOOR NVARCHAR(50)
DECLARE @EFLOOR NVARCHAR(50)
DECLARE @EMPID NVARCHAR(50)
DECLARE @TAG NVARCHAR(50)
DECLARE @ENAME NVARCHAR(50)
DECLARE @ELNAME NVARCHAR(50)
DECLARE @EPART  NVARCHAR(50)
DECLARE @EDEP NVARCHAR(50)
DECLARE @EFUNCTION NVARCHAR(50)
DECLARE  @STATUS NVARCHAR(50)
DECLARE @EDATE2 DATETIME


SELECT @EDATE =(SELECT CARDEVENTDATE FROM INSERTED)
SELECT @ETIME =(SELECT CARDEVENTTIME FROM INSERTED)
SELECT @TAG = (SELECT CARDNO FROM INSERTED)

IF LEN(@TAG) = 4
 SELECT @TAG = '0000'+@TAG  
ELSE IF LEN(@TAG) = 5
  SELECT @TAG ='000'+@TAG
ELSE IF LEN(@TAG) = 6
  SELECT @TAG = '00' + @TAG
ELSE IF LEN(@TAG) = 7
  SELECT @TAG = '0' + @TAG

SELECT @EDOOR = (SELECT DOOR FROM T_PERSONALTRACKING WHERE CARDNO = @TAG)
SELECT @EFLOOR = (SELECT FLOOR FROM T_PERSONALTRACKING WHERE CARDNO = @TAG)
SELECT @EMPID = (SELECT SABUN FROM DATALINEDB.DBO.PERSON WHERE TAGNO = @TAG)
SELECT @ENAME = (SELECT NAME FROM DATALINEDB.DBO.PERSON WHERE TAGNO = @TAG)
SELECT @ELNAME = (SELECT LastName FROM DATALINEDB.DBO.PERSON WHERE TAGNO = @TAG)
SELECT @EPART = (SELECT PART FROM DATALINEDB.DBO.PERSON WHERE TAGNO = @TAG)
SELECT @EDEP = (SELECT MBUSO FROM DATALINEDB.DBO.PERSON WHERE TAGNO = @TAG)
SELECT @EFUNCTION = (SELECT FUNCTIONKEY FROM INSERTED)
SELECT @EDATE2 =(SELECT CARDEVENTDATE FROM INSERTED)

INSERT INTO 
  FINGERPRINTDB.DBO.HISTORY(EDATE, ETIME, EDOOR, EFLOOR, ESABUN, ETAG, 
                            ENAME, ELNAME, EPART, EDEP, ESTATUS, EFUNCTION, 
                            EINOUT, EDATE2) 
VALUES
  (@EDATE, @ETIME, @EDOOR, @EFLOOR, @EMPID, @TAG, 
   @ENAME, @ELNAME, @EPART, @EDEP, NULL, @EFUNCTION, 
   NULL, @EDATE2)

上述查询是否成功执行?但旧数据库中的行不受影响。

我在新数据库中插入一个新行,然后检查旧数据库,没有任何影响吗?

如何从其他数据库获取数据?

Using SQL Server 2000

I make a trigger in new database

CREATE TRIGGER [CARDEVENTSAVING] ON [dbo].[T_CARDEVENT] 
AFTER INSERT 
AS

DECLARE @EDATE VARCHAR(50)
DECLARE @ETIME VARCHAR(50) 
DECLARE @EDOOR NVARCHAR(50)
DECLARE @EFLOOR NVARCHAR(50)
DECLARE @EMPID NVARCHAR(50)
DECLARE @TAG NVARCHAR(50)
DECLARE @ENAME NVARCHAR(50)
DECLARE @ELNAME NVARCHAR(50)
DECLARE @EPART  NVARCHAR(50)
DECLARE @EDEP NVARCHAR(50)
DECLARE @EFUNCTION NVARCHAR(50)
DECLARE  @STATUS NVARCHAR(50)
DECLARE @EDATE2 DATETIME


SELECT @EDATE =(SELECT CARDEVENTDATE FROM INSERTED)
SELECT @ETIME =(SELECT CARDEVENTTIME FROM INSERTED)
SELECT @TAG = (SELECT CARDNO FROM INSERTED)

IF LEN(@TAG) = 4
 SELECT @TAG = '0000'+@TAG  
ELSE IF LEN(@TAG) = 5
  SELECT @TAG ='000'+@TAG
ELSE IF LEN(@TAG) = 6
  SELECT @TAG = '00' + @TAG
ELSE IF LEN(@TAG) = 7
  SELECT @TAG = '0' + @TAG

SELECT @EDOOR = (SELECT DOOR FROM T_PERSONALTRACKING WHERE CARDNO = @TAG)
SELECT @EFLOOR = (SELECT FLOOR FROM T_PERSONALTRACKING WHERE CARDNO = @TAG)
SELECT @EMPID = (SELECT SABUN FROM DATALINEDB.DBO.PERSON WHERE TAGNO = @TAG)
SELECT @ENAME = (SELECT NAME FROM DATALINEDB.DBO.PERSON WHERE TAGNO = @TAG)
SELECT @ELNAME = (SELECT LastName FROM DATALINEDB.DBO.PERSON WHERE TAGNO = @TAG)
SELECT @EPART = (SELECT PART FROM DATALINEDB.DBO.PERSON WHERE TAGNO = @TAG)
SELECT @EDEP = (SELECT MBUSO FROM DATALINEDB.DBO.PERSON WHERE TAGNO = @TAG)
SELECT @EFUNCTION = (SELECT FUNCTIONKEY FROM INSERTED)
SELECT @EDATE2 =(SELECT CARDEVENTDATE FROM INSERTED)

INSERT INTO 
  FINGERPRINTDB.DBO.HISTORY(EDATE, ETIME, EDOOR, EFLOOR, ESABUN, ETAG, 
                            ENAME, ELNAME, EPART, EDEP, ESTATUS, EFUNCTION, 
                            EINOUT, EDATE2) 
VALUES
  (@EDATE, @ETIME, @EDOOR, @EFLOOR, @EMPID, @TAG, 
   @ENAME, @ELNAME, @EPART, @EDEP, NULL, @EFUNCTION, 
   NULL, @EDATE2)

The above query is executing successfully? But rows was not affected in the old database.

I insert a new row in new database then i checked in old database, nothing affected?

How to get a data's from other database?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囚我心虐我身 2024-08-13 03:05:20

我认为您需要:

CREATE TRIGGER [CARDEVENTSAVING] ON [dbo].[T_CARDEVENT] 
AFTER INSERT
AS
INSERT oldDB.DBO.HISTORY (id, event, ...)
SELECT id, event, ... 
FROM inserted

插入的“表”包含所有新数据,删除的“表”包含删除的数据。当更新发生时,两个“表”都被填满。

I think you need that:

CREATE TRIGGER [CARDEVENTSAVING] ON [dbo].[T_CARDEVENT] 
AFTER INSERT
AS
INSERT oldDB.DBO.HISTORY (id, event, ...)
SELECT id, event, ... 
FROM inserted

The 'Table' inserted contains all new data, 'Table' deleted containers deleted data. When an update occurs both 'Tables' are filled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文