赞
踩
sql server提供了update的from 子句,可以将要更新的表与其它的数据源连接起来。虽然只能对一个表进行更新,但是通过将要更新的表与其它的数据源连接起来,就可以在update的表达式 中引用要更新的表以外的其它数据。
一般形式:
update A SET 字段1=B表字段表达式, 字段2=B表字段表达式 from B WHERE 逻辑表达式
例如:
UPDATE dbo.Table2
SET dbo.Table2.ColB = dbo.Table2.ColB + dbo.Table1.ColB
FROM dbo.Table2
INNER JOIN dbo.Table1
ON (dbo.Table2.ColA = dbo.Table1.ColA);
实际更新的操作是在要更新的表上进行的,而不是在from子句所形成的新的结果集上进行的
Oracle没有update from语法,可以通过两种实现方式:
1、利用子查询:
update A
SET 字段1=(select 字段表达式 from B WHERE …),
字段2=(select 字段表达式 from B WHERE …)
WHERE 逻辑表达式
UPDATE多个字段两种写法:
写法一:
UPDATE table_1 a
SET col_x1 = (SELECT b.col_y1, b.col_y2 FROM table_2 b WHERE b.col_n = a.col_m),
col_x2 = (SELECT b.col_y2 FROM table_2 b WHERE b.col_n = a.col_m)
WHERE EXISTS (SELECT * FROM table_2 b WHERE b.col_n = a.col_m)
或
UPDATE table_1 a
SET col_x1 = (SELECT b.col_y1, b.col_y2 FROM table_2 b WHERE b.col_n = a.col_m),
col_x2 = (SELECT b.col_y2 FROM table_2 b WHERE b.col_n = a.col_m)
WHERE a.col_m=(SELECT b.col_n FROM table_2 b WHERE b.col_n = a.col_m)
写法二:
UPDATE table_1 a
SET (col_x1, col_x2) = (SELECT b.col_y1, b.col_y2 FROM table_2 b WHERE b.col_n = a.col_m)
WHERE EXISTS (SELECT * FROM table_2 b WHERE b.col_n = a.col_m);
或
UPDATE table_1 a
SET (col_x1, col_x2) = (SELECT b.col_y1, b.col_y2 FROM table_2 b WHERE b.col_n = a.col_m)
WHERE a.col_m=(SELECT b.col_n FROM table_2 b WHERE b.col_n = a.col_m)
2、利用视图:
UPDATE (SELECT A.NAME ANAME,B.NAME BNAME FROM A,B WHERE A.ID=B.ID)
SET ANAME=BNAME;
在oracle中通常如果视图的数据源来自单表则该视图可以进行更新。而如果视图数据源来自两个以上表时这个视图是不可更新的。但有时候为了操作的方便我们更希望能够对多表视图也进行更新。
这时候我们可以通过建立更新触发器来替代该视图原有更新以达到多表更新的效果
例如:
3.1 创建测试数据表
--===================================================
--创建测试表
--===================================================
Drop Table t1;
Drop Table t2;
create table t1
( t11 numeric(28),t12 varchar2(20));
create table t2
( t11 numeric(28),t22 varchar2(20));
3.2 多表视图范例
--===================================================
--创建测试视图
--===================================================
create Or Replace view t as
select T1.t11 f1 ,T1.t12 f2 ,T2.t22 f3
from T1,T2
Where T1.t11=T2.t11;
3.3 多表视图触发器范例
--===================================================
--创建视图的替代触发器
--===================================================
Create Or Replace Trigger Trg_InsUpdDel_t
Instead Of Insert or update or delete
on t
for each row
Declare
begin
If Inserting Then
Insert Into t1 (t11,t12) Values (:New.f1,:New.f2);
Insert Into t2 (t11,t22) Values (:New.f1,:New.f3);
elsif Updating Then
Update t1 set t11=:New.f1,t12=:New.f2 where t11=:New.f1;
Update t2 set t11=:New.f1,t22=:New.f3 where t11=:New.f1;
elsif Deleting then
Delete from t1 where t11=:Old.f1;
Delete from t2 where t11=:Old.f1;
End if;
end;
如此即实现多表可更新视图的定义工作 。
但要注意当视图进行重新编译的时候这个触发器会失效需要重建。
个体户有效期至
update HZ_TJCLML set SPRQ = (select to_date('20990101', 'yyyymmdd') from HZ_QYHZNR
WHERE HZ_QYHZNR.QYLX = '4900'
AND 2 = 2
AND HZ_TJCLML.SPRQ IS NULL
AND ( (HZ_QYHZNR.ZT <> 'D' AND HZ_QYHZNR.ZT <> 'C')
OR HZ_QYHZNR.ZT IS NULL)
AND (HZ_QYHZNR.CLRQ >= TO_DATE ('20120101', 'YYYYMMDD')
OR HZ_QYHZNR.HZRQ >= TO_DATE ('20120101', 'YYYYMMDD'))
AND HZ_QYHZNR.NBXH = HZ_TJCLML.NBXH
AND HZ_TJCLML.DJGLYWLX = '1'
and HZ_TJCLML.SPRQ IS NULL)
where exists(select 1 from HZ_QYHZNR
WHERE HZ_QYHZNR.QYLX = '4900'
AND 2 = 2
AND HZ_TJCLML.SPRQ IS NULL
AND ( (HZ_QYHZNR.ZT <> 'D' AND HZ_QYHZNR.ZT <> 'C')
OR HZ_QYHZNR.ZT IS NULL)
AND (HZ_QYHZNR.CLRQ >= TO_DATE ('20120101', 'YYYYMMDD')
OR HZ_QYHZNR.HZRQ >= TO_DATE ('20120101', 'YYYYMMDD'))
AND HZ_QYHZNR.NBXH = HZ_TJCLML.NBXH
AND HZ_TJCLML.DJGLYWLX = '1'
and HZ_TJCLML.SPRQ IS NULL)
;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。