How do I get the ID value that I just inserted into the SQL Server self-growth?

Source: Internet
Author: User
For SQL Server 2000来, it provides two new functions (ident_current,scope_identity) and improves the @ @IDENTITY. When you insert a new record, you can call the function:
PRINT ident_current (' table ') ' This will get the new identity value, regardless of whether there is a record added to the database (this avoids the @ @IDENTITY connection limit)
Or: PRINT scope_identity () ' This will get the IDENTITY value of the latest record created by other programs, such as the current stored procedure, trigger, and so on.
The global variable @ @IDENTITY has a problem when an insert is performed on a table, if the table has a trigger program executing the insert operation, and then inserting a record in another table, this returns the @ @IDENTITY value is the identity value of the second table.
If you're not using SQL Server 2000, you'd better have a simple stored procedure to solve this problem.
CREATE PROCEDURE MyProc
@param1 INT
As
BEGIN
SET NOCOUNT on
INSERT into SomeTable
(
Intfield
)
VALUES
(
@param1
)
SET NOCOUNT off
SELECT NEWID = @ @IDENTITY
End
In the ASP you can do this:
<%
Fakevalue = 5
Set conn = Server.CreateObject ("ADODB. Connection ")
Conn.Open "<conn string>"
Set rs = Conn.execute ("exec myProc @param1 =" & Fakevalue)
Response.Write "New ID was" & RS (0)
Rs.close:set rs = Nothing
Conn.close:set conn = Nothing
%>

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.