如何在过程 plsql 更新之前测试一个值
how to test a value before a update in procedure plsql
我有一个程序是什么用另一个 table 的数据更新我的客户。并且一些客户由于其中一个值的失败而没有更新。
这是抛出的错误:ORA - 12899:列“TABLE_EXAMPLE”“BRANCH_CODE”的值太大(当前:3,最大值:2)。
现在是程序:
PROCEDURE p_update_customers AS
CURSOR customer_data IS
SELECT a.code_cust code_cust,
'0' || a.branch_code branch_code,
a.costumer_id,
a.name name,
b.description
FROM customer a, description b
WHERE a.costumer_id = b.costumer_id;
reg_customer_data customer_data%ROWTYPE;
BEGIN
BEGIN
OPEN customer_data;
LOOP
FETCH customer_data
INTO reg_customer_data;
EXIT WHEN customer_data%NOTFOUND;
BEGIN
我认为在这部分我必须使用 IF 或 CASE 语句测试分支代码。我只需要该值的最后两位数。 (在 BEGIN 和 UPDATE 之间)。
UPDATE table_example
SET code_cust = reg_customer_data.code_cust,
branch_code = reg_customer_data.branch_code,
costumer_id = reg_customer_data.costumer_id,
name = reg_customer_data.name,
description = reg_customer_data.description
WHERE code_cust = reg_customer_data.code_cust;
END;
END LOOP;
END;
END p_update_customers;
因此,我需要在更新前测试 branch_code,因为我只需要最后两位数字(09 或 58)。
costumer_id name branch_code
1 jose 09
2 peter 09
3 jhon 09
4 charlie 058
如果branch_code有3个数字,删除第一个并传递2个数字值,稍后继续更新。
注意:我无法更改或删除我在游标中使用的连接:CUSTOMER_DATA 因为 TABLE_EXAMPLE 上的 BRANCH_CODE 必须是两位数。
根据 customer.branch_code
数据类型将 '0' || a.branch_code branch_code
替换为以下内容之一...
如果是数字:
TO_CHAR(a.branch_code, 'FM00') branch_code
如果是varchar/varchar2:
SUBSTR('0' || a.branch_code, -2, 2) branch_code
附录: OP 注意到光标无法更改,因此解决方案需要转移到更新:
UPDATE table_example
SET code_cust = reg_customer_data.code_cust,
branch_code = SUBSTR(reg_customer_data.branch_code, -2, 2),
costumer_id = reg_customer_data.costumer_id,
name = reg_customer_data.name,
description = reg_customer_data.description
WHERE code_cust = reg_customer_data.code_cust;
如果不能更改游标,您仍然可以使用带有 the substr()
function 的负位置值从字符串末尾开始倒数,如 Ed Gibbs 所示,但针对游标结果集:
with t as (
select 1 as costumer_id, 'jose' as name, '09' as branch_code from dual
union all select 2, 'peter ', '09' from dual
union all select 3, 'jhon', '09' from dual
union all select 4, 'charlie', '058' from dual
)
select costumer_id, name, branch_code,
substr(branch_code, -2) as new_branch_code
from t;
| COSTUMER_ID | NAME | BRANCH_CODE | NEW_BRANCH_CODE |
|-------------|---------|-------------|-----------------|
| 1 | jose | 09 | 09 |
| 2 | peter | 09 | 09 |
| 3 | jhon | 09 | 09 |
| 4 | charlie | 058 | 58 |
因此您可以更改更新以使用相同的调用设置值:
UPDATE table_example
SET code_cust = reg_customer_data.code_cust,
branch_code = substr(reg_customer_data.branch_code, -2),
...
我有一个程序是什么用另一个 table 的数据更新我的客户。并且一些客户由于其中一个值的失败而没有更新。
这是抛出的错误:ORA - 12899:列“TABLE_EXAMPLE”“BRANCH_CODE”的值太大(当前:3,最大值:2)。
现在是程序:
PROCEDURE p_update_customers AS
CURSOR customer_data IS
SELECT a.code_cust code_cust,
'0' || a.branch_code branch_code,
a.costumer_id,
a.name name,
b.description
FROM customer a, description b
WHERE a.costumer_id = b.costumer_id;
reg_customer_data customer_data%ROWTYPE;
BEGIN
BEGIN
OPEN customer_data;
LOOP
FETCH customer_data
INTO reg_customer_data;
EXIT WHEN customer_data%NOTFOUND;
BEGIN
我认为在这部分我必须使用 IF 或 CASE 语句测试分支代码。我只需要该值的最后两位数。 (在 BEGIN 和 UPDATE 之间)。
UPDATE table_example
SET code_cust = reg_customer_data.code_cust,
branch_code = reg_customer_data.branch_code,
costumer_id = reg_customer_data.costumer_id,
name = reg_customer_data.name,
description = reg_customer_data.description
WHERE code_cust = reg_customer_data.code_cust;
END;
END LOOP;
END;
END p_update_customers;
因此,我需要在更新前测试 branch_code,因为我只需要最后两位数字(09 或 58)。
costumer_id name branch_code
1 jose 09
2 peter 09
3 jhon 09
4 charlie 058
如果branch_code有3个数字,删除第一个并传递2个数字值,稍后继续更新。
注意:我无法更改或删除我在游标中使用的连接:CUSTOMER_DATA 因为 TABLE_EXAMPLE 上的 BRANCH_CODE 必须是两位数。
根据 customer.branch_code
数据类型将 '0' || a.branch_code branch_code
替换为以下内容之一...
如果是数字:
TO_CHAR(a.branch_code, 'FM00') branch_code
如果是varchar/varchar2:
SUBSTR('0' || a.branch_code, -2, 2) branch_code
附录: OP 注意到光标无法更改,因此解决方案需要转移到更新:
UPDATE table_example
SET code_cust = reg_customer_data.code_cust,
branch_code = SUBSTR(reg_customer_data.branch_code, -2, 2),
costumer_id = reg_customer_data.costumer_id,
name = reg_customer_data.name,
description = reg_customer_data.description
WHERE code_cust = reg_customer_data.code_cust;
如果不能更改游标,您仍然可以使用带有 the substr()
function 的负位置值从字符串末尾开始倒数,如 Ed Gibbs 所示,但针对游标结果集:
with t as (
select 1 as costumer_id, 'jose' as name, '09' as branch_code from dual
union all select 2, 'peter ', '09' from dual
union all select 3, 'jhon', '09' from dual
union all select 4, 'charlie', '058' from dual
)
select costumer_id, name, branch_code,
substr(branch_code, -2) as new_branch_code
from t;
| COSTUMER_ID | NAME | BRANCH_CODE | NEW_BRANCH_CODE |
|-------------|---------|-------------|-----------------|
| 1 | jose | 09 | 09 |
| 2 | peter | 09 | 09 |
| 3 | jhon | 09 | 09 |
| 4 | charlie | 058 | 58 |
因此您可以更改更新以使用相同的调用设置值:
UPDATE table_example
SET code_cust = reg_customer_data.code_cust,
branch_code = substr(reg_customer_data.branch_code, -2),
...