Oracle executes an INSERT statement in a function or stored procedure and returns the primary key ID value

Source: Internet
Author: User

Sometimes we need to insert a record into a table and return the primary key ID value.

Assume that the value of the primary key ID is obtained through the sequence of the corresponding table, and then the ID is assigned

Here are a few things to note:

1) If the Build Table statement contains the primary key ID of the trigger, through the trigger to achieve the primary key ID self-increment, the implementation of the following way:

INSERT  intoGp_month_bill (MONTH, Bonus_value, Cur_way, Cur_time, STATUS, Is_use, Create_time)VALUES(Cur_month, Cur_bonus_value,'Package Function Curing', Sysdate,'in Curing',             1, Sysdate) Returning ID intocur_month_bill_id;--cur_month_bill_id is a variable that receives the returned ID value

Key syntax: INSERT into TABLE VALUES (,) returning IDs into VAL

Supplement: In this case, the statement of the trigger may be as follows

CREATE OR REPLACE TRIGGER  Insert on "GPS". " Em_files " for each rowbegin  Selectto from  dual; End ; /

Or

ORALCE12C, the primary key is set sequence the next value by default

CREATE TABLEC # #EM. Gp_target_type (ID Number( +)DEFAULT"C # #EM". Iseq$$_124895". Nextval not NULL,--default defaults to the next value of SEQNAME NVARCHAR2 ( -) not NULL, REMARK NVARCHAR2 ( $), Create_time DATE not NULL, Create_uid Number( +), Delete_uid Number( +), Delete_time DATE, Is_delete Number(1) not NULL, Update_time DATE, Update_uid Number( +))

2) If the value of the primary key ID is obtained by the user's own sequence value, then the assignment can be implemented as follows:

SELECT Your_seq. Nextval  into from--mid_id as variable INSERT into TABLEVALUES(mid_id);

Supplement: In this case, there can be triggers, and the statements may be as follows:

CREATE OR REPLACE TRIGGERC # #WL. Tr_bulletin beforeINSERT onC # #WL. Wl_bulletin forEach ROW when(New.id is NULL ORNew.id= 0      )BEGIN     SELECTWl_bulletin_seq. Nextval into: new.id fromDUAL; END;/

The current inserted primary key ID is a null value or 0 o'clock, get the nextval value of the corresponding sequence to assign a value to the primary key ID

Oracle executes an INSERT statement in a function or stored procedure and returns the primary key ID value

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.