Self-Increasing ID value for insert data after an Oracle database insert __python

Source: Internet
Author: User
Tags oracleconnection

The previous period was a select last_number-1 from user_sequences where sequence_name= ' sequence name '. This method is a hidden problem. Error values may occur when there is a large number of concurrent access inserts. A new method was found today. Use the Select Sequence name after the insert. Currval from dual can also get the value. And this currval is only retained in the Operation dialog after the insert, which means that each user will see a different currval after the insert. Not affected by other users, And when you make another insert in the current operation, the Currval also refreshes. There is no reason to take the wrong number. You can use it safely.

Attach an example to it:

Background a stored procedure

Create or Replace procedure Newarchfileid (insert_sql varchar, seq_name out number) is
Str_sql varchar (100);
Begin

Execute immediate insert_sql;
Str_sql: = ' SELECT ' | | seq_name| | '. Currval from dual ';
Execute immediate str_sql into newid;
End Newarchfileid;

To invoke the C # code of a stored procedure:

String ConnectionString = "Connection string";
OracleConnection conn = new OracleConnection (ConnectionString);//Create a connection

OracleCommand cmd = new OracleCommand ("Newarchfileid", conn);
Cmd.commandtype = CommandType.StoredProcedure;
Cmd. Parameters.Add ("Insert_sql", Oracletype.varchar);
Cmd. Parameters.Add ("Seq_name", Oracletype.varchar);
Cmd. Parameters.Add ("newid", oracletype.number);

Cmd. Parameters[0]. Value = "specific INSERT statement";
Cmd. PARAMETERS[1]. Value = "sequence name";
Cmd. PARAMETERS[2]. Direction = ParameterDirection.Output;

Cmd. ExecuteNonQuery ();

Output a new ID

        Fileid = cmd. parameters["newid"]. Value.tostring ();
        Response.Write (Fileid);

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.