我是PL SQL的新手。我想更新表行值。我想以xml格式从php获取值并更新特定的行值。所以我写这个pl sql函数和它的工作原理细即可。 这是代码:
FUNCTION update_payroll_transaction(employeeData NVARCHAR2) RETURN CLOB IS
ret CLOB;
xmlData XMLType;
v_code NUMBER;
v_errm VARCHAR2(500);
num1 number(4,0);
BEGIN
xmlData:=XMLType(employeeData);
UPDATE payrollfile ep SET(
SALYR,
SALMT,
EMPID,
DPTID,
SALHD,
DESCRP,
ALAMT,
OPID,
TRADT) = (SELECT x.SALYR,
x.SALMT,
x.EMPID,
x.DPTID,
x.SALHD,
x.DESCRP,
x.ALAMT,
x.OPID,
x.TRADT FROM XMLTABLE('/transaction'
PASSING xmlData
COLUMNS SALYR NUMBER(4,0) PATH 'salary_year',
SALMT NUMBER(2,0) PATH 'salary_month',
EMPID NUMBER PATH 'employee_id',
DPTID NUMBER PATH 'department_code',
SALHD VARCHAR2(2) PATH 'salary_head',
DESCRP VARCHAR2(50) PATH 'description',
ALAMT FLOAT(126) PATH 'amount',
OPID NUMBER PATH 'operator_id',
TRADT DATE PATH 'transaction_date') x
WHERE ep.EMPID=x.EMPID) WHERE EXISTS(SELECT 1 FROM XMLTABLE('/transaction'
PASSING xmlData
COLUMNS SALYR NUMBER(4,0) PATH 'salary_year',
SALMT NUMBER(2,0) PATH 'salary_month',
EMPID NUMBER PATH 'employee_id',
DPTID NUMBER PATH 'department_code',
SALHD VARCHAR2(2) PATH 'salary_head',
DESCRP VARCHAR2(50) PATH 'description',
ALAMT FLOAT(126) PATH 'amount',
OPID NUMBER PATH 'operator_id',
TRADT DATE PATH 'transaction_date') x WHERE ep.EMPID=x.EMPID);
ret:=to_char(sql%rowcount);
COMMIT;
RETURN '<result><status affectedRow='||ret||'>success</status></result>';
DBMS_OUTPUT.PUT_LINE(ret);
END update_payroll_transaction;
这是我的php代码:
$stid = oci_parse($conn, " begin
:result := PAYROLL.update_payroll_transaction('<transaction>
<salary_year>$year</salary_year>
<salary_month>$month</salary_month>
<employee_id>$empID</employee_id>
<department_code>$dptCode</department_code>
<salary_head>$salHD</salary_head>
<description>$description</description>
<amount>$amount</amount>
<operator_id>$optID</operator_id>
<transaction_date>$trDate</transaction_date>
</transaction>');
end;" );
oci_bind_by_name($stid, ':result',$ru, 5000);
$output = oci_execute($stid);
我认为更新时间很长。是否可以最小化该代码。谢谢。
答案 0 :(得分:2)
如果我是你,我会转而使用MERGE;这样做的好处是只能引用一次xmltable,而不是更新语句中的两次:
function update_payroll_transaction(employeedata nvarchar2)
return clob
is
ret clob;
xmldata xmltype;
v_code number;
v_errm varchar2(500);
num1 number(4,0);
begin
xmldata:=xmltype(employeedata);
merge into payrollfile tgt
using (select salyr,
salmt,
empid,
dptid,
salhd,
descrp,
alamt,
opid,
tradt
from xmltable('/transaction'
passing xmldata
columns salyr number(4,0) path 'salary_year',
salmt number(2,0) path 'salary_month',
empid number path 'employee_id',
dptid number path 'department_code',
salhd varchar2(2) path 'salary_head',
descrp varchar2(50) path 'description',
alamt float(126) path 'amount',
opid number path 'operator_id',
tradt date path 'transaction_date')) src
on (tgt.empid = src.empid)
when matched then
update set tgt.salyr = src.salyr,
tgt.salmt = src.salmt,
tgt.empid = src.empid,
tgt.dptid = src.dptid,
tgt.salhd = src.salhd,
tgt.descrp = src.descrp,
tgt.alamt = src.alamt,
tgt.opid = src.opid,
tgt.tradt = src.tradt;
ret:=to_char(sql%rowcount);
commit;
dbms_output.put_line(ret);
return '<result><status affectedRow='||ret||'>success</status></result>';
end update_payroll_transaction;
/