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

触发器还是有问题 不能执行插入和更新用做变量表名得到的表
还是有问题 不能执行插入和更新用做变量表名得到的表

提示为:第 1 行: 'adlishi' 附近有语法错误。


代码如下
CREATE TRIGGER tougao ON yichangxinwen  
instead of insert 
as 
declare @biaoming varchar(200)  

if (select quanxian from denglu where yonghu = (select yonghu from inserted)) = '编辑' 
begin 
set @biaoming = 'adlishi' 
end 

else if (select quanxian from denglu where yonghu = (select yonghu from inserted)) = '通讯员' 
begin 
set @biaoming = 'txlishi' 
end 

else if (select quanxian from denglu where yonghu = (select yonghu from inserted)) = '本台记者' 
begin 
set @biaoming = 'bjlishi' 
end 

else if (select quanxian from denglu where yonghu = (select yonghu from inserted)) = '集体记者' 
begin 
set @biaoming = 'jjlishi' 
end 


if exists(select * from yichangxinwen where yonghu = (select yonghu from inserted) and nianyue = (select convert(char(7),getdate(),20)) ) 
begin 

  insert yichangxinwen(biaoti,wenzhang,select1,yonghu,select2,yonghu2,shijian,nianyue,shenghe,danwei,riqi,canshu) 
  select biaoti,wenzhang,select1,yonghu,select2,yonghu2,shijian,nianyue,shenghe,danwei,riqi,canshu from inserted; 
  exec('update'''+ @biaoming+ '''set touzong=touzong+1 
  from '''+@biaoming+''' inner join inserted yichangxinwen on '''+@biaoming+'''.yonghu =  
  yichangxinwen.yonghu and '''+@biaoming+'''.nianyue = (select convert(char(7),getdate(),20)) 
  ') 
end 

else 
begin 
   
  insert yichangxinwen(biaoti,wenzhang,select1,yonghu,select2,yonghu2,shijian,nianyue,shenghe,danwei,riqi,canshu) 
  select biaoti,wenzhang,select1,yonghu,select2,yonghu2,shijian,nianyue,shenghe,danwei,riqi,canshu from inserted; 
  exec('insert '''+@biaoming+'''(yonghu,nianyue,touzong) 
  select yonghu,convert(char(7),getdate(),20),1 from inserted') 
end 

请大家帮忙看看吧!!!!!


------解决方案--------------------
SQL code

--这个问题贴了两次。且很多人都指出了错误处,楼主仍然犯这个错。无语。。。。。

CREATE TRIGGER tougao  ON yichangxinwen   
instead   of  insert  
as  
declare  @biaoming  varchar(200)   
declare @t table(yonghu varchar(200) ,nianyue varchar(200) ,touzong varchar(200))
if (select quanxian from denglu where yonghu = (select yonghu  from inserted)) = '编辑'  
begin  
set @biaoming = 'adlishi'  
end  

else if (select quanxian from denglu where yonghu = (select yonghu  from inserted)) = '通讯员'  
begin  
set @biaoming = 'txlishi'  
end  

else if (select quanxian from denglu where yonghu = (select yonghu  from inserted)) = '本台记者'  
begin  
set @biaoming = 'bjlishi'  
end  

else if (select quanxian from denglu where yonghu = (select yonghu  from insertedfro