小弟初学触发器 碰到个问题 请教下 alter trigger tr_tmptri
on class1
for insert
as
declare @t nvarchar(30)
select @t=姓名 from inserted
if exists( select a.姓名 from class1 a,inserted b where a.姓名=b.姓名) ---这里如何判断数据库里没有同名的信息 就跳出触发器 不需要执行后边的
---像现在这样 if 后边肯定为真,还有 怎么用update 实现 而不用删除 ----重新建立新数据。begin
select a.姓名 from class1 a,inserted b where a.姓名=b.姓名
delete from class1 where class1.姓名=@t
insert into class1 select * from inserted
end
on class1
for insert
as
declare @t nvarchar(30)
select @t=姓名 from inserted
if exists( select a.姓名 from class1 a,inserted b where a.姓名=b.姓名) ---这里如何判断数据库里没有同名的信息 就跳出触发器 不需要执行后边的
---像现在这样 if 后边肯定为真,还有 怎么用update 实现 而不用删除 ----重新建立新数据。begin
select a.姓名 from class1 a,inserted b where a.姓名=b.姓名
delete from class1 where class1.姓名=@t
insert into class1 select * from inserted
end
解决方案 »
- 关于SQL查询
- 如何把一个存贮过程的值放入到一个临时表里。
- 复制一个数据库表中的多条记录到另一个数据库相同的表中问题
- 一个在SQL中取数据的问题
- 请教一个行列转换的问题
- Login failed for user 'sa'. 原因: 密码验证失败,出现基础结构错误。请检查以前的错误。
- 哪位大侠有"LECCO SQL Expert Pro for MS SQL Server" 和注册码?
- 问个替换列内容的问题
- windows优化大师卸载SQLserver时出现“停止服务“Eventlog”失败,请求的控件对此服务无效‘的消息框,该如何处理??
- 触发器里如何调用存储过程
- 帮忙写下查询的sql语句谢谢!
- SQL SERVER 关于结合的问题
抛开例子,你描述一下你想要个什么样的功能的触发器现在假如class1表里有两个字段:姓名,性别
alter trigger tr_tmptri
on class1
for insert
as
if (select count(1) from class1 a,inserted b
where a.姓名=b.姓名 and a.性别=b.性别 group by a.姓名,a.性别)>1
begin
rollback
raiserror ('重复数据插入',16,1)
end
--这里如何判断数据库里没有同名的信息 就跳出触发器 不需要执行后边的
--像现在这样 if 后边肯定为真,还有 怎么用update 实现 而不用删除
--重新建立新数据。
else
begin
delete t from class1 t where exists (select 1 from inserted where t.姓名=姓名)
insert into class1 select * from inserted
end
没有环境,没有测,感觉有点儿别扭
关注,学习!
where a.姓名=b.姓名 and a.性别=b.性别 group by a.姓名,a.性别)>1这个 必须>1才可以是 有重复记录么?
是不是等于=1 就可以了。
如果没有重复记录就是0啊?
我觉得是这样...求教~
where a.姓名=b.姓名 and a.性别=b.性别 难道不是在有相同记录的时候 永远等于1么?求解为什么啊
插入后才触发!