触发三个操作(更新,删除,插入)转换错误

时间:2015-09-19 11:39:51

标签: sql-server

我试图在 customers 表上创建一个用于插入,删除和更新的触发器。
触发器已成功创建,但是,我确定它包含许多错误 这是我的触发器代码

go
create trigger Onetrig
on customers
after update,insert,delete
as 
declare @log varchar(100),@name varchar(20),@activity varchar(20)
begin
if exists(select * from deleted) and exists(select * from deleted)
set @activity = 'Update'
set @name = (select c_name from inserted)
set @log = 'Record Updated in database '+@name
insert into logs
values(@activity,@log,GETDATE())

if exists(select * from inserted) and not exists(select * from deleted)
set @activity = 'Insert'
set @name = (select c_name from inserted)
set @log = 'Record Inserted in database '+@name
insert into logs
values(@activity,@log,GETDATE())

if exists(select * from deleted) and not exists(select * from inserted)
set @activity = 'Insert'
set @name = (select c_name from deleted)
set @log = 'Record Deleted from database '+@name
insert into logs
values(@activity,@log,GETDATE())
end

go create trigger Onetrig on customers after update,insert,delete as declare @log varchar(100),@name varchar(20),@activity varchar(20) begin if exists(select * from deleted) and exists(select * from deleted) set @activity = 'Update' set @name = (select c_name from inserted) set @log = 'Record Updated in database '+@name insert into logs values(@activity,@log,GETDATE()) if exists(select * from inserted) and not exists(select * from deleted) set @activity = 'Insert' set @name = (select c_name from inserted) set @log = 'Record Inserted in database '+@name insert into logs values(@activity,@log,GETDATE()) if exists(select * from deleted) and not exists(select * from inserted) set @activity = 'Insert' set @name = (select c_name from deleted) set @log = 'Record Deleted from database '+@name insert into logs values(@activity,@log,GETDATE()) end

我的任务是使用这三个操作的活动填充 log 表。当我执行这三个操作中的任何一个时,它会抛出某种转换的错误

customers 表有一些记录 这是我的客户&的代码。 log 表:
Msg 8152, Level 16, State 14, Procedure Onetrig, Line 11 String or binary data would be truncated. The statement has been terminated.

create table logs
(activity varchar(20),report varchar(20),Time datetime)

创建表客户

create table logs
(activity varchar(20),report varchar(20),Time datetime)

我错过了什么转换?

2 个答案:

答案 0 :(得分:1)

您已在report中声明logs为20个字符。但是,您正在将字符串'Record Updated in database '+@name插入其中。这至少包含27个字符。

建议:

  • 修复varchar字段的长度。你也可以让它们更长,甚至多达8000个字符。
  • 进行插入时,始终明确包含列列表。
  • 修复第一个if语句以引用inserteddeleted,因此逻辑合理。
  • 在每个陈述的末尾加上分号。

答案 1 :(得分:0)

Gordon Linoff的回答非常好。

我想在触发的情况下添加以下行

set @name = (select c_name from inserted)
set @name = (select c_name from deleted)

假设始终只插入/删除/更新了一行。这是错误的,因为DML触发器在语句级别而不是在行级别。 因此我会改变(对于I / U / D):

if exists ...
begin
set @activity = 'U/I/D'
insert into dbo.logs (... columns ...) -- <-- You should use the schema (default schema is dbo)
select @activity, 'Record Updated in database ' + x.c_name, GETDATE()
--                                      ^
--                                      |
--          ----------------------------------------------------------
--          The maximum length of dbo.logs.c_name (!?) column should be 
--          LEN('Record Updated in database') + 1 + MaxLength of dbo.customers.c_name column
from inserted as x -- or deleted
end