Oracle DB 更新语句,相同的值不同的目标行
Oracle DB Update statement, same value different targeted rows
在我的程序中,我想插入 table 订单并更新 table 销售员
create or replace PROCEDURE place_order(
PURCH_AMT in varchar,
CUSTOMER_ID in varchar,
SALESMAN_ID in varchar
) AS
order_id varchar(50);
ORD_DATE varchar(50);
--INICIO
begin
--VARS
order_id := sys_guid();
ORD_DATE := SYSDATE;
--INSERT da order
insert into ORDERS values (order_id, PURCH_AMT, ORD_DATE, CUSTOMER_ID, SALESMAN_ID);
DBMS_OUTPUT.put_line('out put with variable salesman_id');
DBMS_OUTPUT.put_line(SALESMAN_ID);
--UPDATE sales do salesman
update salesman
set n_sales = n_sales + 1
where salesman.SALESMAN_ID = SALESMAN_ID;
--COMMIT
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
end place_order;
但是当我 运行 它更新所有的销售员
当我更改程序更新并对销售员 ID 进行硬编码时,它起作用了
DBMS_OUTPUT.put_line('output with harcoded 5001');
DBMS_OUTPUT.put_line(5001);
--UPDATE sales do salesman
update salesman
set n_sales = n_sales + 1
where salesman.SALESMAN_ID = '5001';
这是错误的:
create or replace PROCEDURE place_order(
PURCH_AMT in varchar,
CUSTOMER_ID in varchar,
SALESMAN_ID in varchar --> this
) AS
<snip>
--UPDATE sales do salesman
update salesman
set n_sales = n_sales + 1
where salesman.SALESMAN_ID = SALESMAN_ID;
----------- -----------
this
Never 名称参数与列名称相同。正如您所做的那样,where
子句被评估为类似于 where 1 = 1
的内容,这意味着 更新 table.
中的所有行
重命名参数;通常,我们前缀他们,例如
create or replace PROCEDURE place_order(
PAR_PURCH_AMT in varchar2,
PAR_CUSTOMER_ID in varchar2,
PAR_SALESMAN_ID in varchar2
) AS
<snip>
--UPDATE sales do salesman
update salesman
set n_sales = n_sales + 1
where salesman.SALESMAN_ID = PAR_SALESMAN_ID;
(此外,Oracle 建议我们使用 varchar2
而不是 varchar
)
在我的程序中,我想插入 table 订单并更新 table 销售员
create or replace PROCEDURE place_order(
PURCH_AMT in varchar,
CUSTOMER_ID in varchar,
SALESMAN_ID in varchar
) AS
order_id varchar(50);
ORD_DATE varchar(50);
--INICIO
begin
--VARS
order_id := sys_guid();
ORD_DATE := SYSDATE;
--INSERT da order
insert into ORDERS values (order_id, PURCH_AMT, ORD_DATE, CUSTOMER_ID, SALESMAN_ID);
DBMS_OUTPUT.put_line('out put with variable salesman_id');
DBMS_OUTPUT.put_line(SALESMAN_ID);
--UPDATE sales do salesman
update salesman
set n_sales = n_sales + 1
where salesman.SALESMAN_ID = SALESMAN_ID;
--COMMIT
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
end place_order;
但是当我 运行 它更新所有的销售员
当我更改程序更新并对销售员 ID 进行硬编码时,它起作用了
DBMS_OUTPUT.put_line('output with harcoded 5001');
DBMS_OUTPUT.put_line(5001);
--UPDATE sales do salesman
update salesman
set n_sales = n_sales + 1
where salesman.SALESMAN_ID = '5001';
这是错误的:
create or replace PROCEDURE place_order(
PURCH_AMT in varchar,
CUSTOMER_ID in varchar,
SALESMAN_ID in varchar --> this
) AS
<snip>
--UPDATE sales do salesman
update salesman
set n_sales = n_sales + 1
where salesman.SALESMAN_ID = SALESMAN_ID;
----------- -----------
this
Never 名称参数与列名称相同。正如您所做的那样,where
子句被评估为类似于 where 1 = 1
的内容,这意味着 更新 table.
重命名参数;通常,我们前缀他们,例如
create or replace PROCEDURE place_order(
PAR_PURCH_AMT in varchar2,
PAR_CUSTOMER_ID in varchar2,
PAR_SALESMAN_ID in varchar2
) AS
<snip>
--UPDATE sales do salesman
update salesman
set n_sales = n_sales + 1
where salesman.SALESMAN_ID = PAR_SALESMAN_ID;
(此外,Oracle 建议我们使用 varchar2
而不是 varchar
)