当前位置: 代码迷 >> Sql Server >> 触发器还是有有关问题 不能执行插入和更新用做变量表名得到的表
  详细解决方案

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

热度:60   发布时间:2016-04-27 18:44:17.0
触发器还是有问题 不能执行插入和更新用做变量表名得到的表
还是有问题 不能执行插入和更新用做变量表名得到的表

提示为:第 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 [email protected]+''' inner join inserted yichangxinwen on [email protected]+'''.yonghu =  
  yichangxinwen.yonghu and [email protected]+'''.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 [email protected]+'''(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
  相关解决方案