按A和B跟插入的记录比较作为比较条件,找到D的值,会找到三条记录的
----------------------------------------------------------------------
比较的规则?
----------------------------------------------------------------------
比较的规则?
解决方案 »
- sqlserver2005 的安装目录问题
- SQL Server2005找不到Database Engine Tuning Advisor
- 执行查询的问题
- 大家帮帮我!(急,在线等)
- mysql条件 筛选表 符合条件的居前显示 不符合条件的在后面显示 居前和居后的都有自己的排序
- Sql高手請進
- 高手考虑了这个有味的问题了吗?
- 关于 sql server 的 SQLSERVERAGENT
- SQL中关于随机数问题
- 我怎么在TempDB里面加入临时表,然后我用程序怎么访问这些临时表呢?
- 分布式数据库环境中,外键约束的问题??
- 一个文件夹下面有多个dbf文件,怎样一次导入到新建的SQL Server中啊?
字段A,B,C,D
当插入数据时,C的值 <-1 时,如"-3"
将这条数据拆分为三条记录插入到AAA,将三条记录中C的值都修改为-1
INSERT tb SELECT 1,1,1,1
UNION ALL SELECT 1,1,1,1
UNION ALL SELECT 1,1,1,1
UNION ALL SELECT 1,1,1,1
go
if exists(select 1 from sysobjects where id=object_id(N'dbo.t_tb') and xtype='TF')
drop trigger t_tb
go
create trigger t_tb on tb
instead of insert
as
declare @i int
select @i= d from inserted
if @i>=0 insert tb select * from inserted
if @i<-1
begin
set @i=abs(@i)
while @i>0
begin
insert tb select i.a,i.b,i.c,-1 from inserted i
set @i=@i-1
end
end
goinsert tb select 114,114,114,-2
select * from tb--删除测试数据
drop trigger t_tb
drop table tb
接上面的代码:
使用:
select d from tb where a = i.a And b=i.b
会得到二条记录...
现在要将这二条记录中的d的值取出来.
insert tb select i.a,i.b,-1,刚才查询到的D的值 from inserted i
已经插进去了。
举个例子:(改变一下字段,将C与D互换,来符合楼主的表结构)insert tb select 114,114,-2,114
这里D=114的,在触发器里D的值都是114啊,还要再取吗?是不是我理解有错??
查询出来的已经存在表中的记录
instead of insert
as
declare @i int,@d int
select @i= c from inserted
select @d=d from tb a,inserted i where a.a = i.a And a.b=i.b
if @i>=0 insert tb select * from inserted
if @i<-1
begin
set @i=abs(@i)
while @i>0
begin
insert tb select i.a,i.b,-1,@d from inserted i
set @i=@i-1
end
end
go