日期:2014-05-18  浏览次数:20442 次

动态创建触发器出现了问题 救命
IF   EXISTS   (SELECT   name   FROM   sysobjects
            WHERE   name   =   'tb_record '   AND   type   =   'U ')
      DROP   table   tb_record
go

create   table   tb_record
(mytablename   varchar(20),
information   varchar(400),
controldate   smalldatetime
)

go

IF   EXISTS   (SELECT   name   FROM   sysobjects
            WHERE   name   =   'create_trigger '   AND   type   =   'P ')
      DROP   PROCEDURE   create_trigger

go

create   procedure   create_trigger   as
begin
    declare   @mytablename   varchar(20),@infor_insert   varchar(400),@infor_delete   varchar(400),
                    @infor_update   varchar(400)

    declare   @sql   varchar(1000)
 
    declare   fetch_mytablename   cursor   local   scroll  
    for  
        select   mytablename   from   mytablename
   
    set   @infor_insert= '执行了插入操作 '
    set   @infor_delete= '执行了删除操作 '
    set   @infor_update= '执行了更新操作 '

    if   CURSOR_STATUS( 'local ', 'fetch_mytablename ')=-1

    Open   fetch_mytablename    

    Fetch   next   from   fetch_mytablename   into   @mytablename

    while   @fetch_status=0
    begin

      select   @sql= 'create   trigger   '+@mytablename+ '_insert   on   '+@mytablename+ '   for   insert   as
        begin
            insert   into   tb_record(mytablename,information,controldate)
            values( '+@mytablename+ ', '+@infor_insert+ ', '+ ' '+left(convert(varchar(20),getdate(),120),10)+ ') '+
          '   end '

        exec   @sql
       
        fetch   next   from   fetch_mytablename   into   @mytablename

    end

    close   fetch_mytablename  
    deallocate   fetch_mytablename

end


命令能成功执行     但是   为什么tb_record中没有记录       是不是创建触发器没成功???

怎么办       急急急!!!!!!!

------解决方案--------------------
存储过程需要主动执行:
exec create_trigger

然后你可以查询你的触发器是否创建成功:
select * from sysobjects where type= 'tr ' and parent_obj=object_id(N 'tb_record ')