SqlServer obtains the auto-increment ID in the transaction

Source: Internet
Author: User
Thanks to the spirit of lee576, the Code is as follows: values (idINT, employeeVARCHAR (32) values (1, one) values (2, two) INSERTINTOtable1VALUES (3, three) INSERTINTOtable1VALUES (4, four) GOCREATE

Thanks to the spirit of lee576, the Code is as follows: USE tempdbgoCREATE TABLE table1 (id INT, employee VARCHAR (32) goINSERT INTO table1 VALUES (1, one) insert into table1 VALUES (2, two) insert into table1 VALUES (3, three) insert into table1 VALUES (4, four) GOCREATE

Thanks for the spirit of lee576, the Code is as follows:

USE tempdbgoCREATE TABLE table1 (id INT, employee VARCHAR (32) goINSERT INTO table1 VALUES (1, 'one') insert into table1 VALUES (2, 'two ') insert into table1 VALUES (3, 'three ') insert into table1 VALUES (4, 'four') gocreate table table2 (id int identity (), employee VARCHAR (32 )) GO -- ======================================================= -- use OUTPUT insert @ MyTableVar -- ================================== DECLARE @ MyTableVar TABLE (id INT) insert into table2 (employee) output inserted. id INTO @ MyTableVarSELECT employee FROM table1 -- ============================== = -- Query @ MyTableVar -- ==================================== SELECT * FROM @ MyTableVar -- drop table table1, table 2

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.