views:

98

answers:

3

I'm having troubles pulling the id from the first insert to use on the second insert. Here's my SQL (I'm using stored procedures):

DECLARE @JoinDate date
DECLARE @ID int
SET @JoinDate = getdate()

EXEC Members_Add $(UserID), '$(UserName)', 
       @JoinDate, '$(firstname)', '$(lastname)', NULL, 
      '$(Country)', NULL,  '$(stateorprovince)', '$(city)', 
      '$(ziporpostalcode)', '$(addressline1)', '$(addressline2)', 
      '$(MailCountry)', NULL, '$(mailstateprovince)', '$(MailCity)', 
      '$(mailzipcode)', '$(mailaddress)', NULL, NULL, NULL, 
      '$(mobilephone)', NULL, '$(Fax)', '$(Email)', NULL, NULL

SELECT @ID = SCOPE_IDENTITY()

EXEC Merchants_Add @ID, NULL, '$(BusinessName)', '$(CorporateName)', 
      '$(contactperson)', '$(OfficePhone)', '$(website)', 
      '$(DirectoryListing)', 'False'

I need to get the ID of the record added by the first stored procedure, I read up that you should use SELECT @@IDENTITY instead of SELECT Max(ID) but it doesn't seem to be working...

EDIT: I just updated the SELECT @@IDENTITY AS NEW_ID to SELECT SCOPE_IDENTITY AS NEW_ID and now I'm getting a cannot convert nvarchar to int error... any ideas?

EDIT #2: Updated the code again... now I'm getting cannot insert the vaule NULL into column 'MemberID' that's the one that @ID is in for the Merchants_Add procedure.

+2  A: 

Try:

DECLARE @JoinDate date
DECLARE @newId int
SET @JoinDate = getdate()

EXEC Members_Add $(UserID), '$(UserName)', @JoinDate, '$(firstname)', '$(lastname)', NULL, '$(Country)', NULL, '$(stateorprovince)', '$(city)', '$(ziporpostalcode)', '$(addressline1)', '$(addressline2)', '$(MailCountry)', NULL, '$(mailstateprovince)', '$(MailCity)', '$(mailzipcode)', '$(mailaddress)', NULL, NULL, NULL, '$(mobilephone)', NULL, '$(Fax)', '$(Email)', NULL, NULL

SELECT @newId = SCOPE_IDENTITY()

EXEC Merchants_Add @newId, NULL, '$(BusinessName)', '$(CorporateName)', '$(contactperson)', '$(OfficePhone)', '$(website)', '$(DirectoryListing)', 'False'

EDIT: Changed source to reflect needed syntax using the preferred SCOPE_IDENTITY() instead of @@IDENTITY.

md5sum
+3  A: 

You should use SCOPE_IDENTITY().

Have a look at SCOPE_IDENTITY (Transact-SQL)

@@IDENTITY and SCOPE_IDENTITY will return the last identity value generated in any table in the current session. However, SCOPE_IDENTITY returns the value only within the current scope; @@IDENTITY is not limited to a specific scope.

Try using something like this

DECLARE @ID INT
EXEC Members_Add $(UserID), '$(UserName)', 
       @JoinDate, '$(firstname)', '$(lastname)', NULL, 
      '$(Country)', NULL,  '$(stateorprovince)', '$(city)', 
      '$(ziporpostalcode)', '$(addressline1)', '$(addressline2)', 
      '$(MailCountry)', NULL, '$(mailstateprovince)', '$(MailCity)', 
      '$(mailzipcode)', '$(mailaddress)', NULL, NULL, NULL, 
      '$(mobilephone)', NULL, '$(Fax)', '$(Email)', NULL, NULL

SELECT @ID = SCOPE_IDENTITY()

EXEC Merchants_Add @ID, NULL, '$(BusinessName)', '$(CorporateName)', 
      '$(contactperson)', '$(OfficePhone)', '$(website)', 
      '$(DirectoryListing)', 'False'

Note the use of the @ID

From @@IDENTITY

astander
From the documentation : 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.
astander
Read the spec... ignore my question :D and +1
md5sum
I tried changing SELECT SELECT @@IDENTITY AS NEW_ID to SELECT SCOPE_IDENTITY() AS NEW_ID and now I'm getting a cannot convert nvarchar to in error... I updated the question, can you please take another look?
Matt
You should use DECLARE @ID INT... then after insert SELECT @ID = SCOPE_IDENTITY(). Check if that works. You need to assign the value to a param.
astander
Just did it... now it's saying cant insert null into MemberID field (that's where the @ID is in your code) I'm guessing SCOPE_IDENTITY() is returning NULL for some reason...
Matt
Is MemberID an IDENTITY COLUMN?
astander
It is in the Members table, not in the Merchants table. It's a foreign key to link up to the Members table.
Matt
Yes, in the Members table,Is MemberID an IDENTITY COLUMN?
astander
Yes, it is an IDENTITY COLUMN. Stupid 15 character requirement...
Matt
+1  A: 

You need to declare and use a T-Sql variable to hold the identity value. And you should generally use Scope_Identity() not @@Identity...

  DECLARE @JoinDate dateSET @JoinDate = getdate()
  Declare @NewId Integer 
  EXEC Members_Add $(UserID), '$(UserName)',  @JoinDate, '$(firstname)', 
       '$(lastname)', NULL, '$(Country)', NULL,  '$(stateorprovince)',      
       '$(city)',  '$(ziporpostalcode)', '$(addressline1)',        
       '$(addressline2)','$(MailCountry)', NULL, '$(mailstateprovince)', '
       $(MailCity)','$(mailzipcode)', '$(mailaddress)', NULL, NULL, NULL,
       '$(mobilephone)', NULL, '$(Fax)', '$(Email)', NULL, NULL

  -- Here get identity Value
  Set @NewId = Scope_Identity()

  EXEC Merchants_Add @NewId, NULL, '$(BusinessName)', '$(CorporateName)',
       '$(contactperson)', '$(OfficePhone)', '$(website)',
       '$(DirectoryListing)', 'False'
Charles Bretana