Create an Oracle Auto-increment Field

Source: Internet
Author: User

Guidance:The SQL Server database containsAuto-increment FieldThis type. HoweverOracleBut not yet. We can use the trigger function to implement it:

1. Create a storage process for creating auto-increment Fields

Create or replace procedure pro_create_seq_col
(Tablename varchar2, columnname varchar2)
As
Strsql varchar2 (1000 );
Begin
Strsql: = 'create sequence seq _ '| tablename | 'minvalue 1 maxvalue 999999999999999999 start with 1 increment by 1 nocache ';
Execute immediate strsql;
Strsql: = 'create or replace trigger trg _ '| tablename | 'before insert on' | tablename |' for each row begin select seq _ '| tablename | '. nextval into: new. '| columnname |' from dual; end ;';
Execute immediate strsql;
End;
/

2. Display authorization when executing dynamic SQL statements in Oracle, even if the user has this permission)

Grant create any sequence to UserName;
Grant create any trigger to UserName;
3. Re-Compile the Stored Procedure pro_create_seq_col;

4. Now we can use this stored procedure to create auto-incrementing self-segments.

5. When you call the stored procedure to create an auto-increment field, the following message is displayed: the first parameter is the table name, and the second parameter is the name of the auto-increment field)

Exec pro_create_seq_col ('SB _ zsxx', 'zsxh ');
Exec pro_create_seq_col ('SB _ sbxx', 'sbxh ');
Exec pro_create_seq_col ('SB _ jkx', 'pzxh ');
Exec pro_create_seq_col ('sdspfp', 'sysfpid ');

I hope that you can master the Oracle Auto-increment field creation technology flexibly. I believe this technology will be very useful.

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.