trigger - Incorrect syntax near 'INSTEAD' error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • asf93555
    New Member
    • Jul 2008
    • 1

    #1

    trigger - Incorrect syntax near 'INSTEAD' error

    Running under SQL2000 I can not get an INSTEAD trigger to function. I've even copied the example directl from books online - no joy . . .

    Server: Msg 170, Level 15, State 1, Procedure IO_Trig_INS_Emp loyee, Line 2
    Line 2: Incorrect syntax near 'INSTEAD'.

    CREATE TABLE Person
    (
    SSN char(11) PRIMARY KEY,
    Name nvarchar(100),
    Address nvarchar(100),
    Birthdate datetime
    )

    CREATE TABLE EmployeeTable
    (
    EmployeeID int PRIMARY KEY,
    SSN char(11) UNIQUE,
    Department nvarchar(10),
    Salary money,
    CONSTRAINT FKEmpPer FOREIGN KEY (SSN)
    REFERENCES Person (SSN)
    )

    CREATE VIEW Employee AS
    SELECT P.SSN as SSN, Name, Address,
    Birthdate, EmployeeID, Department, Salary
    FROM Person P, EmployeeTable E
    WHERE P.SSN = E.SSN

    CREATE TABLE PersonDuplicate s
    (
    SSN char(11),
    Name nvarchar(100),
    Address nvarchar(100),
    Birthdate datetime,
    InsertSNAME nchar(100),
    WhenInserted datetime
    )

    CREATE TRIGGER IO_Trig_INS_Emp loyee ON Employee
    INSTEAD OF INSERT
    AS
    BEGIN
    SET NOCOUNT ON
    -- Check for duplicate Person. If no duplicate, do an insert.
    IF (NOT EXISTS (SELECT P.SSN
    FROM Person P, inserted I
    WHERE P.SSN = I.SSN))
    INSERT INTO Person
    SELECT SSN,Name,Addres s,Birthdate,Com ment
    FROM inserted
    ELSE
    -- Log attempt to insert duplicate Person row in PersonDuplicate s table.
    INSERT INTO PersonDuplicate s
    SELECT SSN,Name,Addres s,Birthdate,SUS ER_SNAME(),GETD ATE()
    FROM inserted
    -- Check for duplicate Employee. If no duplicate, do an insert.
    IF (NOT EXISTS (SELECT E.SSN
    FROM EmployeeTable E, inserted
    WHERE E.SSN = inserted.SSN))
    INSERT INTO EmployeeTable
    SELECT EmployeeID,SSN, Department, Salary,Comment
    FROM inserted
    ELSE
    --If duplicate, change to UPDATE so that there will not
    --be a duplicate key violation error.
    UPDATE EmployeeTable
    SET EmployeeID = I.EmployeeID,
    Department = I.Department,
    Salary = I.Salary,
    Comment = I.Comment
    FROM EmployeeTable E, inserted I
    WHERE E.SSN = I.SSN
    END
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Here's the error if your run it in sql2005

    Msg 207, Level 16, State 1, Procedure IO_Trig_INS_Emp loyee, Line 12
    Invalid column name 'Comment'.
    Msg 213, Level 16, State 1, Procedure IO_Trig_INS_Emp loyee, Line 12
    Insert Error: Column name or number of supplied values does not match table definition.
    -- CK

    Comment

    Working...