创建要更新的 Oracle 过程 Table
Creating Oracle Procedure To Update Table
我正在尝试在 Oracle 数据库中创建一个过程,如果任何其他列存在值,该过程将使用 'X' 更新 PMBPCSB。如果所有列都是 NULL 那么我不希望它做任何事情。我不确定如何格式化程序来完成我想要的。感谢任何帮助。
COLUMN_NAME
DATA
PMBPCSB
PMBPCFBB
PMBPCHUB
PMBCITNC
PMBCITNSB
PMBCSDB
PMBCSDVOS
PMBCVOSB
PMBCWOB
这只是一个普通的UPDATE
,不是吗?
SQL> create table pm
2 (pmbpcsb varchar2(1),
3 pmbpcfbb varchar2(10),
4 pmbpchub varchar2(10),
5 pmbcitnc varchar2(10)
6 );
Table created.
SQL> insert into pm values (null, '100', '200', '300');
1 row created.
SQL> insert into pm values (null, '300', null , '500');
1 row created.
SQL> insert into pm values (null, null , null , null );
1 row created.
更新:
SQL> update pm set
2 pmbpcsb = 'X'
3 where coalesce(pmbpcfbb, pmbpchub, pmbcitnc) is not null;
2 rows updated.
结果:
SQL> select * from pm;
PMBPCSB PMBPCFBB PMBPCHUB PMBCITNC
---------- ---------- ---------- ----------
X 100 200 300
X 300 500
--> this is a row whose columns are NULL (all of them)
SQL>
如何转换它为程序?只需:
SQL> create or replace procedure p_pm as
2 begin
3 update pm set
4 pmbpcsb = 'X'
5 where coalesce(pmbpcfbb, pmbpchub, pmbcitnc) is not null;
6 end;
7 /
Procedure created.
SQL>
但是,也许您更愿意使用 CASE
并影响所有行,因为 - 如果 table 内容发生变化怎么办?
SQL> update pm set
2 pmbpcsb = case when coalesce(pmbpcfbb, pmbpchub, pmbcitnc) is not null then 'X'
3 else null
4 end;
3 rows updated.
SQL>
或者,正如人们评论的那样,创建一个视图(使用相同的原理):
SQL> create or replace view v_pm as
2 select case when coalesce(pmbpcfbb, pmbpchub, pmbcitnc) is not null then 'X'
3 else null
4 end pmbpcsb,
5 pmbpcfbb,
6 pmbpchub,
7 pmbcitnc
8 from pm;
View created.
SQL> select * From v_pm;
PMBPCSB PMBPCFBB PMBPCHUB PMBCITNC
---------- ---------- ---------- ----------
X 100 200 300
X 300 500
SQL>
我正在尝试在 Oracle 数据库中创建一个过程,如果任何其他列存在值,该过程将使用 'X' 更新 PMBPCSB。如果所有列都是 NULL 那么我不希望它做任何事情。我不确定如何格式化程序来完成我想要的。感谢任何帮助。
COLUMN_NAME | DATA |
---|---|
PMBPCSB | |
PMBPCFBB | |
PMBPCHUB | |
PMBCITNC | |
PMBCITNSB | |
PMBCSDB | |
PMBCSDVOS | |
PMBCVOSB | |
PMBCWOB |
这只是一个普通的UPDATE
,不是吗?
SQL> create table pm
2 (pmbpcsb varchar2(1),
3 pmbpcfbb varchar2(10),
4 pmbpchub varchar2(10),
5 pmbcitnc varchar2(10)
6 );
Table created.
SQL> insert into pm values (null, '100', '200', '300');
1 row created.
SQL> insert into pm values (null, '300', null , '500');
1 row created.
SQL> insert into pm values (null, null , null , null );
1 row created.
更新:
SQL> update pm set
2 pmbpcsb = 'X'
3 where coalesce(pmbpcfbb, pmbpchub, pmbcitnc) is not null;
2 rows updated.
结果:
SQL> select * from pm;
PMBPCSB PMBPCFBB PMBPCHUB PMBCITNC
---------- ---------- ---------- ----------
X 100 200 300
X 300 500
--> this is a row whose columns are NULL (all of them)
SQL>
如何转换它为程序?只需:
SQL> create or replace procedure p_pm as
2 begin
3 update pm set
4 pmbpcsb = 'X'
5 where coalesce(pmbpcfbb, pmbpchub, pmbcitnc) is not null;
6 end;
7 /
Procedure created.
SQL>
但是,也许您更愿意使用 CASE
并影响所有行,因为 - 如果 table 内容发生变化怎么办?
SQL> update pm set
2 pmbpcsb = case when coalesce(pmbpcfbb, pmbpchub, pmbcitnc) is not null then 'X'
3 else null
4 end;
3 rows updated.
SQL>
或者,正如人们评论的那样,创建一个视图(使用相同的原理):
SQL> create or replace view v_pm as
2 select case when coalesce(pmbpcfbb, pmbpchub, pmbcitnc) is not null then 'X'
3 else null
4 end pmbpcsb,
5 pmbpcfbb,
6 pmbpchub,
7 pmbcitnc
8 from pm;
View created.
SQL> select * From v_pm;
PMBPCSB PMBPCFBB PMBPCHUB PMBCITNC
---------- ---------- ---------- ----------
X 100 200 300
X 300 500
SQL>