4

I see that there is a very similiar question already answered that discusses this same issue, but i just cannot seem to understand it. Below is a copy of my code that is in a stored procedure. I need to somehow, have the second insert statement insert the CompanyID generated from the first, into this second Statement. I appreciate any help i can get.

Thank You!!

@comp_name nvarchar (50),
@City nvarchar (50),
@State nvarchar (10),
@Address ntext,
@Zip_Code nvarchar (50),
@Country nvarchar (50),
@cust_name nvarchar (50),
@CompanyID int
AS
INSERT INTO Company_Listing
(comp_name, City, State, Address, Zip_Code, Country)
VALUES (@comp_name, @City, @State, @Address, @Zip_Code, @Country)

INSERT INTO Customer_Listing
(cust_name, City, State, Address, Zip_Code, Country, CompanyID)
VALUES (@comp_name,@City,@State,@Address,@Zip_Code,@Country,@CompanyID)
0

5 Answers 5

9

Assuming this is for SQL Server - Yes, use SCOPE_IDENTITY:

@comp_name nvarchar (50),
@City nvarchar (50),
@State nvarchar (10),
@Address ntext,
@Zip_Code nvarchar (50),
@Country nvarchar (50),
@cust_name nvarchar (50),
@CompanyID int
AS
INSERT INTO Company_Listing
(comp_name, City, State, Address, Zip_Code, Country)
VALUES (@comp_name, @City, @State, @Address, @Zip_Code, @Country)

INSERT INTO Customer_Listing
(cust_name, City, State, Address, Zip_Code, Country, CompanyId)
VALUES (@comp_name,@City,@State,@Address,@Zip_Code,@Country,SCOPE_IDENTITY())

From MSDN's documentation on SCOPE_IDENTITY():

Returns the last identity value inserted into an identity column in the same scope. A scope is a module: a stored procedure, trigger, function, or batch. Therefore, two statements are in the same scope if they are in the same stored procedure, function, or batch.

1
  • Thank you! I have never got help with an issue so fast!
    – John
    Sep 14, 2011 at 18:41
4

Yes. Assuming you are using SQL Server ... just use the SCOPE_IDENTITY() function to retrieve the last inserted identity value. In other words, replace @CompanyID with SCOPE_IDENTITY().

0
SET @MyFirstID = (SELECT SCOPE_IDENTITY())

--now you have your ID from your first insert statement, namely @MyFirstID

Code becomes:

@comp_name nvarchar (50),
@City nvarchar (50),
@State nvarchar (10),
@Address ntext,
@Zip_Code nvarchar (50),
@Country nvarchar (50),
@cust_name nvarchar (50),
@CompanyID int
AS
INSERT INTO Company_Listing
(comp_name, City, State, Address, Zip_Code, Country)
VALUES (@comp_name, @City, @State, @Address, @Zip_Code, @Country)

INSERT INTO Customer_Listing
(cust_name, City, State, Address, Zip_Code, Country, CompanyID)
VALUES (@comp_name,@City,@State,@Address,@Zip_Code,@Country,SCOPE_IDENTITY())
0

Pick up the id of the created record after the first id. This is done differently in different database systems.

For Microsoft SQL Server:

set @CompanyId = scope_identity()
-1

Yes. you just need to add an interim statement that collects the ID, store it in a variable and then use that in your second statement. So you would need to add something like

SELECT IDENT_CURRENT('dbo.CompanyTable')
1
  • 1
    never use ident_current for this, it is not limited to the scope and will return the worng value under race conditions.
    – HLGEM
    Sep 14, 2011 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.