Set Serveroutput on;
begin
update t_contract_master set liability_state = 1 where policy_code = '123456789';
if SQL%Found then
dbms_output.put_line('the Policy is updated successfully.');
commit;
else
dbms_output.put_line('the policy is updated failed.');
end if;
end;
/
运行结果:
SQL>
the policy is updated failed.
PL/SQL procedure successfully completed
Set serveroutput on;
declare
---define Cursor
Cursor cur_policy is
select cm.policy_code, cm.applicant_id, cm.period_prem,cm.bank_code,cm.bank_account
from t_contract_master cm
where cm.liability_state = 2
and cm.policy_type = 1
and cm.policy_cate in ('2','3','4')
and rownum < 5
order by cm.policy_code desc;
curPolicyInfo cur_policy%rowtype;---定义游标变量
Begin
open cur_policy; ---open cursor
Loop
--deal with extraction data from DB
Fetch cur_policy into curPolicyInfo;
Exit when cur_policy%notfound;
Dbms_Output.put_line(curPolicyInfo.policy_code);
end loop;
Exception
when others then
close cur_policy;
Dbms_Output.put_line(Sqlerrm);
if cur_policy%isopen then
--close cursor
close cur_policy;
end if;
end;
/
2)
Set serveroutput on;
declare
Cursor cur_policy is
select cm.policy_code, cm.applicant_id, cm.period_prem,cm.bank_code,cm.bank_account
from t_contract_master cm
where cm.liability_state = 2
and cm.policy_type = 1
and cm.policy_cate in ('2','3','4')
and rownum < 5
order by cm.policy_code desc;
v_policyCode t_contract_master.policy_code%type;
v_applicantId t_contract_master.applicant_id%type;
v_periodPrem t_contract_master.period_prem%type;
v_bankCode t_contract_master.bank_code%type;
v_bankAccount t_contract_master.bank_account%type;
Begin
open cur_policy;
Loop
Fetch cur_policy into v_policyCode,
v_applicantId,
v_periodPrem,
v_bankCode,
v_bankAccount;
Exit when cur_policy%notfound;
Dbms_Output.put_line(v_policyCode);
end loop;
Exception
when others then
close cur_policy;
Dbms_Output.put_line(Sqlerrm);
if cur_policy%isopen then
close cur_policy;
end if;
end;
/
3)
Set serveroutput on;
declare
Cursor cur_policy is
select cm.policy_code, cm.applicant_id, cm.period_prem,cm.bank_code,cm.bank_account
from t_contract_master cm
where cm.liability_state = 2
and cm.policy_type = 1
and cm.policy_cate in ('2','3','4')
and rownum < 5
order by cm.policy_code desc;
Begin
For rec_Policy in cur_policy loop
Dbms_Output.put_line(rec_policy.policy_code);
end loop;
Exception
when others then
Dbms_Output.put_line(Sqlerrm);
end;
/
3. Ref Cursor(动态游标):
1) 与隐式Cursor,显式Cursor的区别:Ref Cursor是可以通过在运行期间传递参数来获取数据结果集。而另外两种Cursor,是静态的,在编译期间就决定数据结果集。
2) Ref cursor的使用:
Type [Cursor type name] is ref cursor
Define 动态的Sql语句
Open cursor
操作数据---Fetch [Cursor name]
Close Cursor
Set serveroutput on;
Declare
---define cursor type name
type cur_type is ref cursor;
cur_policy cur_type;
sqlStr varchar2(500);
rec_policy t_contract_master%rowtype;
begin
---define 动态Sql
sqlStr := 'select cm.policy_code, cm.applicant_id, cm.period_prem,cm.bank_code,cm.bank_account from t_contract_master cm
where cm.liability_state = 2
and cm.policy_type = 1
and cm.policy_cate in (2,3,4)
and rownum < 5
order by cm.policy_code desc ';
---Open Cursor
open cur_policy for sqlStr;
loop
fetch cur_policy into rec_policy.policy_code, rec_policy.applicant_id, rec_policy.period_prem,rec_policy.bank_code,rec_policy.bank_account;
exit when cur_policy%notfound;
Dbms_Output.put_line('Policy_code:'||rec_policy.policy_code);
end loop;
close cur_policy;
end;
/