在触发器中的变量老提示要声明变量的问题 急疯了??
在触发器中的变量老提示要声明变量的问题 急疯了??
完整代码是
CREATE TRIGGER tougao ON dbo.yichangxinwen
instead of insert
as
declare @biaoming nvarchar(200)
if (select quanxian from denglu where yonghu = (select yonghu from inserted)) = '编辑'
begin
set @biaoming = 'adlishi'
end
if (select quanxian from denglu where yonghu = (select yonghu from inserted)) = '通讯员'
begin
set @biaoming = 'txlishi'
end
if (select quanxian from denglu where yonghu = (select yonghu from inserted)) = '本台记者'
begin
set @biaoming = 'bjlishi'
end
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;
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
if not 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;
insert @biaoming(yonghu,nianyue,touzong)
select yonghu,convert(char(7),getdate(),20),1 from inserted
end
结果是老说@biaoming 必须声明 是不是触发器中变量的使用方法不正确????
上面的@biaoming是得到要插入或者更新的表名 变量
在线等!!!!
------解决方案--------------------为什么不用else if ,而全部要用if,这不是浪费吗?????
变量名不能够直接这么用
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 1 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,selec