0

我正在学习有关触发器的硬件分配,并且在插入和删除 DML ForTriggers 的语法方面遇到问题,如下所示:

CREATE TRIGGER tr_PERSON_ForInsert
ON PERSON
FOR INSERT
AS
BEGIN

    DECLARE @SSN  CHAR(9)
    SELECT  @SSN  = SSN FROM  inserted
    DECLARE @NAME VARCHAR(50)
    SELECT  @NAME = NAME FROM inserted  
    DECLARE @USERNAME VARCHAR(50)
    SELECT  @USERNAME USERNAME FROM INSERTED
    declare @time time(7)
    select  @time = time from inserted

    insert into PERSON_DEL_LOG
    VALUES ('New person with SSN = ' +
    Cast(@SSN as NVARchar(9)) + 
    CAST(@NAME AS NVARCHAR(50)) +
    cast(@username as nvarchar(50)) +
    cast(@time as nvarchar(20))
    )
END

INSERT INTO PERSON('012675543', 'MIKE', '5467896543', 'MUSEUM', 'INTEL', 
'BLUECROSS', '987654321')

CREATE TRIGGER tr_PERSON_ForDELETE
ON PERSON
FOR DELETE
AS
BEGIN

    DECLARE @SSN  CHAR(9)
    SELECT  @SSN  = SSN FROM  DELETED
    DECLARE @NAME VARCHAR(50)
    SELECT  @NAME = NAME FROM DELETED       

    insert into PERSON_DEL_LOG
    VALUES ('New person with SSN = ' +
    Cast(@SSN as nvarchar(9)) +
    Cast(@NAME as VARCHAR(50)) + 'is added at ' + 
    cast(Getdate() as nvarchar(20))
    )
END

DELETE FROM PERSON WHERE NAME = 987654321

SELECT * FROM PERSON_DEL_LOG

我的PERSON_DEL_LOG表结构:

SSN, NAME, USERNAME, TIME

我需要知道我的问题到底出在哪里

4

0 回答 0