create proc proc_editType
ascreate trigger tr_update 
on TargetType 
for update 
as 
begin 
    if update(type) 
    begin 
        update t2 
        set type = i.type 
        from TargetItem t2 
        right join inserted i on t2.code = i.code 
        update t3 
        set type = i.type 
        from Item t3 
        right join inserted i on t3.Icode = i.code 
    end 
end 
go错误提示:
Server: Msg 156, Level 15, State 1, Procedure proc_editType, Line 4
Incorrect syntax near the keyword 'trigger'.
Server: Msg 140, Level 15, State 1, Procedure proc_editType, Line 9
Can only use IF UPDATE within a CREATE TRIGGER statement.正确的该怎么放呀?注:触发器没问题!

解决方案 »

  1.   

    伺服器: 訊息 156,層級 15,狀態 1,程序 proc_editType,行 3
    關鍵字 'trigger' 附近的語法不正確。
    伺服器: 訊息 140,層級 15,狀態 1,程序 proc_editType,行 7
    只能在 CREATE TRIGGER 陳述式中使用 IF UPDATE。
      

  2.   

    把创建触发器的SQL放到字符串里面就行了,存储过程里面显然是不能有 Create trigger语句的。create proc proc_editType 
    as 
    declare @SQL varchar(4000)
    set @SQL ='
    create trigger tr_update 
    on TargetType 
    for update 
    as 
    begin 
    if update(type) 
    begin 
    update t2 
    set type = i.type 
    from TargetItem t2 
    right join inserted i on t2.code = i.code 
    update t3 
    set type = i.type 
    from Item t3 
    right join inserted i on t3.Icode = i.code 
    end 
    end '
    execute(@SQL);

    go 
      

  3.   

    在存储过程里面执行DDL语句(也就是创建、修改、删除数据库对象的语句),都用这种拼SQL字符串的方法。