有兩個table.要在table2上作個update時的觸發器.當update table2的數量時,判table2的該產品sum(數量)不能和table1的該產品sum(數量)不相等.
一句話就是,兩個表的各產品總數得一樣.
table1
(
產品名稱 char(20)
數量 int
)table2
(
產品名稱 char(20)
位置 int
數量 int
)
一句話就是,兩個表的各產品總數得一樣.
table1
(
產品名稱 char(20)
數量 int
)table2
(
產品名稱 char(20)
位置 int
數量 int
)
解决方案 »
- 为什么我的visual studio商业智能模板里面没有Analysis Services项目模板
- sql如何判断条件进行不同更新?
- 走投无路一个非常怪异的时间转换的问题
- 中止程序执行的SQL怎么写?
- 你绝对没有见过的SQL语句,可惜我现在通不过,谁帮我解决一下,问题是关于在 order by 后面加 case when语句.
- 几个SQLSERVER的问题请教
- sql server使用addlinkedserver报错
- 关于access(2000)数据库密码的问题。
- 将文本插入数据库时的问题。
- sqlserver2005程序执行存储过程慢/超时
- technet 实况录像 2007年以后为什么不能下载?
- 从文本导入数据去重?
for update
as
if update(数量)
begin
update table1
set 数量=i.数量
from table1,inserted i
where table1.产品名称=i.产品名称
end
如果是,就不用sum了吧
ls正解~
for update
as
if update(数量) and exists(select 1 from inserted i,deleted d where i.产品名称=d.产品名称 and isnull(i.数量,0)<>isnull(d.数量,0))
begin
update table1
set 数量=t.数量
from table1,(select 产品名称,sum(数量) as 数量 from table2 group by 产品名称)t,inserted i,deleted d
where table1.产品名称=i.产品名称 and i.产品名称=d.产品名称 and isnull(i.数量,0)<>isnull(d.数量,0) and t.产品名称=i.产品名称
end
就是在修改table2的數據是判斷是否會和table1總數不同.
for update
as
if update(数量) and exists(select 1 from inserted i,deleted d where i.产品名称=d.产品名称 and isnull(i.数量,0)<>isnull(d.数量,0))
begin
if exists(select 1 from (select 产品名称,sum(isnull(数量,0)) as 数量
from table2 group by 产品名称)t,table1,inserted i,deleted d
where t.产品名称=table1.产品名称
and t.数量<>isnull(table1.数量,0)
and t.产品名称=i.产品名称
and i.产品名称=d.产品名称
and isnull(i.数量,0)<>isnull(d.数量,0))
raiserror('修改的数量和table1中的总数不相等!',16,1)
rollback tran
return
end
table1
產品名稱 數量
1.產品1 50
2.產品2 60table2
產品名稱 位置 數量
1.產品1 100 10
2.產品2 100 60
3.產品1 200 40現在要修改table2的第三條紀錄為 數量50 修改時判斷,table2的總數得和table1一樣,不一樣報錯"數據總數不對,請修改"...改對了,才可以update table2
table1
產品名稱 數量
1.產品1 50
2.產品2 60table2
產品名稱 位置 數量
1.產品1 100 10
2.產品2 100 60
3.產品1 200 30 -----数据改变
修改后
table2
產品名稱 位置 數量
1.產品1 100 10
2.產品2 100 60
3.產品1 200 40
才能提交 更新 ?
drop table table1
drop table table2
create table table1(name varchar(20),shumu int)
insert into table1 values('產品1',50)
insert into table1 values('產品2',60)create table table2(id int,name varchar(20),dizhi varchar(20), shumu int)
insert into table2 values(1,'產品1','a',10)
insert into table2 values(2,'產品2','b',60)
insert into table2 values(3,'產品1','b',30)select * from table1
select * from table2create trigger tr_t2 on table2
for update
as
begin if (select sum(shumu) from table2 where id<> (select id from deleted) and name=(select name from inserted))
+
(select shumu from inserted ) in (select shumu from table1 where name=(select name from inserted))
update table2 set shumu=(select shumu from inserted) where id=(select id from deleted)
else
---不做任何事
end
还没测试 待改正
drop table table2
create table table1(name varchar(20),shumu int)
insert into table1 values('產品1',50)
insert into table1 values('產品2',60)create table table2(id int,name varchar(20),dizhi varchar(20), shumu int)
insert into table2 values(1,'產品1','a',10)
insert into table2 values(2,'產品2','b',60)
insert into table2 values(3,'產品1','b',30)select * from table1
select * from table2drop trigger tr_t2create trigger tr_t2 on table2
for update
as
begin if ((select sum(shumu) from table2 where id<> (select id from deleted) and name=(select name from inserted))
+
(select shumu from inserted )) in (select shumu from table1 where name=(select name from inserted))
update table2 set shumu=(select shumu from inserted) where id=(select id from deleted)else update table2 set shumu=(select shumu from deleted) where id=(select id from deleted)end
update table2 set shumu=20 where id=3
for update
as
begin if ((select sum(shumu) from table2 where id<> (select id from deleted) and name=(select name from inserted))
+
(select shumu from inserted )) in (select shumu from table1 where name=(select name from inserted))
update table2 set shumu=(select shumu from inserted) where id=(select id from deleted)else raiserror('修改的数量和table1中的总数不相等!',16,1)
rollback tran
returnend 结果和在重新更新是一样的吧
是啊
我写的是一条一条的
对触发器多条更新还比较陌生
---------------------------------------------------
不是说你写的触发器是一条一条的,是说更新多条的时候会出错.
试试这条语句会不会报错?
update table2 set shumu=50 where id in(1,3)
把table1做成视图不就得了,
as
select partname,sum(quantity) as AmountQty
from tabel2 group by partname
for update
as
if update(数量) and exists(select 1 from inserted i,deleted d where i.产品名称=d.产品名称 and isnull(i.数量,0)<>isnull(d.数量,0))
begin
if exists(select 1 from (select 产品名称,sum(isnull(数量,0)) as 数量
from table2 group by 产品名称)t,table1,inserted i,deleted d
where t.产品名称=table1.产品名称
and t.数量<>isnull(table1.数量,0)
and t.产品名称=i.产品名称
and i.产品名称=d.产品名称
and isnull(i.数量,0)<>isnull(d.数量,0))
Begin --這裡少了Begin
raiserror('修改的数量和table1中的总数不相等!',16,1)
rollback tran
End --這裡少了End
return
end
GO