Select @ identity in access

Source: Internet
Author: User

Does the ACCESS database contain select @ identity? The answer is yes. However, access can only execute one SQL statement at a time. Multiple SQL statements must be executed multiple times, which is a limitation. In SQL Server, You can execute multiple SQL statements at a time. Access uses jet-SQL, and SQL server uses a T-SQL, which is quite different in usage.

 

However, N consecutive statements can be executed in access, as shown below:
Cmd. commandtext = "insert into mytable (N1, N2) values (22, 11 )";
Int COUNT = cmd. executenonquery ();
Cmd. commandtext = "select @ identity ";
Int newid = (INT) cmd. executescalar ();

 

Select @ identity is the keyword used to retrieve the automatic number of the previous statement.

 

 

You can use executereader () in SQL server to execute several SQL statements together at the same time.
Petshop 4.0 has the following usage:
Using (sqldatareader RDR = cmd. executereader (commandbehavior. closeconnection )){
// Read the returned @ err
RDR. Read ();
// If the error count is not zero throw an exception
If (RDR. getint32 (1 )! = 0)
Throw new applicationexception ("data integrity error on order insert-rollback issued ");
}
The SQL statement of the CMD object is as follows (obtained in debugging ):
Declare @ ID int;
Declare @ err int;
Insert into orders values
(@ Userid, @ date, @ shipaddress1, @ shipaddress2, @ shipcity, @ shipstate, @ shipzip, @ shipcountry, @ billaddress1, @ billaddress2, @ billcity, @ billstate, @ billzip, @ billcountry, 'ups', @ total, @ billfirstname, @ billlastname, @ shipfirstname, @ shiplastname, @ authorizationnumber, 'us _ en ');
Select @ ID = @ identity;
Insert into orderstatus values (@ ID, @ ID, getdate (), 'P ');
Select @ err =@@ error;
Insert into lineitem values (@ ID, @ linenumber0, @ itemid0, @ quantity0, @ price0 );
Select @ err = @ err + @ error;
Insert into lineitem values (@ ID, @ linenumber1, @ itemid1, @ quantity1, @ price1 );
Select @ err = @ err + @ error;
Select @ ID, @ err

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.