案例声明内部触发器

时间:2013-02-21 06:34:12

标签: asp.net sql sql-server-2005

create trigger Insert_emp on dbo.emp
after insert
as
begin

declare @tablename varchar(50)
declare @rdatetime varchar(20)
declare @month int

set @rdatetime = (select Bdate from inserted)
set @month = (select datepart(mm,@rdatetime))
case when @month = 2 then set @tablename = 'FEP_EMP' end
print @tablename 
end

触发器将在执行后执行,在emp表中插入记录。它将获取插入的日期。从该日期起将计算月份。我想要设置表名的值。

1 个答案:

答案 0 :(得分:1)

SQL Server中的CASE只能用于获取标量值 - 不能用于决定执行哪个代码块或语句。要处理条件代码执行,请使用常规IF / ELSE语句。

将您的触发器更改为:

create trigger Insert_emp on dbo.emp
after insert
as
begin

   declare @tablename varchar(50)
   declare @rdatetime varchar(20)
   declare @month int

   set @rdatetime = (select Bdate from inserted)
   set @month = (select datepart(mm,@rdatetime))

   IF @month = 2 
      set @tablename = 'FEP_EMP' 

   print @tablename 
end