|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
Using the @@IDENTITY
I am using MS SQL 2000, with vb.net. I have the following query that inserts some information into two different tables.
Table1 has a primary key that is used to relate to Table2. After the information has been inserted into the tables, I want to return the PropertyID (@@IDENTITY). This all works, if I don't include the second insert. But when I include the second insert as shown, the return value for @@IDENTITY is 0. Thanks in advanced! CREATE PROCEDURE [dbo].[Add_Residential_Property] @UserID As int, @CountryID As int, @LandDimensions As int, @SQFT As int, AS INSERT INTO Table1(CountryID,LandDimensions) VALUES( @CountryID, @LandDimensions) INSERT INTO Table2(PropertyID, LandDimensions, SQFT)VALUES(@@IDENTITY, @GarageCap, @TotalBeds) RETURN @@IDENTITY GO |
|
#2
|
|||
|
|||
|
Code:
CREATE PROCEDURE dbo.Add_Residential_Property @UserID As int, @CountryID As int, @LandDimensions As int, @SQFT As int AS declare @id int set nocount on INSERT INTO Table1(CountryID,LandDimensions) VALUES( @CountryID, @LandDimensions) set @id = @@identity INSERT INTO Table2(PropertyID, LandDimensions, SQFT)VALUES(@id, @GarageCap, @TotalBeds) select @id GO You shouldn't use return in this case as it is more intended for reporting a status back to the caller. You should also include some error handling. |
|
#3
|
|||
|
|||
|
Thanks for the help! It is working great!
Just wondering what is the "set nocount on" for as i have never seen it. Thanks again! |
|
#4
|
|||
|
|||
|
Without the set nocount on option each update/insert/delete statement will return an empty result set to the client.
|
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > Using the @@IDENTITY |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|