表table1中有msg字段和flag字段,在表table1建一触发器,目的:当插入的记录flag标识的内容为"是"时,将该记录插入到另一个表table2中,如果msg字段的内容超过20个字符时,将这条记录分成两条记录插入到table2中;我写的触发器如下:create trigger InsSendMsg ON dbo.accession   for insert as  if len(INSERTED.msg)>20
     begin
        insert into table2(a,b,c,msg1) select ('OK',b1,c1,substring(INSERTED.msg,1,20)) from INSERTED where flag='是' 
        insert into table2(a,b,c,msg1) select ('OK',b1,c1,substring(INSERTED.msg,21,len(INSERTED.msg)-20)) from INSERTED where flag='是'
else
   insert into table2(a,b,c,msg1) select ('OK',b1,c1,msg) from INSERTED where flag='是' 
出错提示:
服务器: 消息 107,级别 16,状态 1,过程 InsSendMsg,行 10
列前缀 'INSERTED' 与查询中所用的表名或别名不匹配。帮帮忙?

解决方案 »

  1.   

    --这样试试.没经过验证create trigger InsSendMsg ON dbo.accession
    for insert as
    declare @b1 varchar(50)
    declare @c1 varchar(50)
    declare @str varchar(8000)
    declare @str1 varchar(20)
    declare @i int
    set @i = 1
    if exists(select 1 from inserted where len(msg)>20)
    declare cur_tmp cursor for 
    select b1,c1,msg from inserted where len(msg)>20 and flag='是' 
    open cur_tmp
    fetch next from cur_tmp into @b1,@c1,@str
    while @@fetch_status = 0
    begin
    while @i <= len(@str)
    begin
    select @str1 = substring(@str,@i,20)
    insert into table2(a,b,c,msg1)
    select 'OK',@b1,@c1,@str1
    select @i = @i + 20
    end
    fetch next from cur_tmp into @b1,@c1,@str
    end
    else
    insert into table2(a,b,c,msg1) select ('OK',b1,c1,msg) from INSERTED where flag='是' 
      

  2.   

    CLOSE cur_tmp
    DEALLOCATE cur_tmp