How Oracle updates a newly inserted data collection with a trigger implementation

Source: Internet
Author: User

How Oracle can update newly inserted data with trigger implementation Collection

1, establish the test table

CREATE TABLE TEST_TB

(

ID number,

WLID number,

PM VARCHAR2 (100),

Djzt VARCHAR2 (10),

SL number,

PH VARCHAR2 (100)

);

2. Set up the package and the package body and function defined with REF CURSOR:

CREATE OR REPLACE

Package Pkg_test AS

/* Define REF CURSOR type

No return type, for weak type, allows dynamic SQL queries,

Otherwise, it is strongly typed and cannot use dynamic SQL queries;

*/

Type myrctype is REF CURSOR;

--Function declaration

function get (IntID number) return myrctype;

End Pkg_test;

/

Create or replace package body pkg_test as

--Function body

function get (IntID number) return Myrctype is

RC Myrctype; --Defining the REF CURSOR variable

Sqlstr VARCHAR2 (500);

Begin

--if Intid=0 Then

--static tests, directly returning results directly with the SELECT statement

--Open RC for select id,name,sex,address,postcode,birthday from student;

--else

--Dynamic SQL assignment, using: w_id to declare that the variable is externally derived

SQLSTR: = ' Select ph,pm from WLZD_TB where ID =:1 ';

--dynamic test, return result with SQLSTR string, pass parameter with using keyword

Open RC for sqlstr using IntID;

--End If;

return RC;

End get;

End Pkg_test;

3. Create a trigger:

Create or Replace Trigger TEST_TG

Before insert

On TEST_TB

For each row

Declare

W_RC Pkg_test.myrctype; --Define the REF CURSOR type variable

Begin

W_RC: = Pkg_test.get (: new.wlid);

Loop

Fetch W_RC into:new.ph,:new.pm;

Exit when W_rc%notfound;

--:NEW.PM: = ref.pm;

--:new.ph: = Test_fc (: new.wlid). ph;

End Loop;

End

4. Test:

Insert into TEST_TB (id,wlid) VALUES (10,345)

Results:

How Oracle updates a newly inserted data collection with a trigger implementation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.