MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old June 14th, 2004, 11:50 AM
jmlsgateway jmlsgateway is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: OKC
Posts: 342 jmlsgateway User rank is Private First Class (20 - 50 Reputation Level)jmlsgateway User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 20 h 48 m 28 sec
Reputation Power: 5
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

Reply With Quote
  #2  
Old June 14th, 2004, 12:06 PM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,766 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 6 h 24 m 8 sec
Reputation Power: 37
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.

Reply With Quote
  #3  
Old June 14th, 2004, 12:18 PM
jmlsgateway jmlsgateway is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: OKC
Posts: 342 jmlsgateway User rank is Private First Class (20 - 50 Reputation Level)jmlsgateway User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 20 h 48 m 28 sec
Reputation Power: 5
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!

Reply With Quote
  #4  
Old June 14th, 2004, 02:02 PM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,766 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 6 h 24 m 8 sec
Reputation Power: 37
Without the set nocount on option each update/insert/delete statement will return an empty result set to the client.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Using the @@IDENTITY


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway