从存储过程完成的第一次插入中拉出id的麻烦在第二个存储过程中使用

时间:2009-12-10 16:24:03

标签: sql sql-server stored-procedures

我从第一个插件中拉出id以在第二个插件上使用时遇到了麻烦。这是我的SQL(我正在使用存储过程):

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'

我需要获取第一个存储过程添加的记录的ID,我读到你应该使用SELECT @@ IDENTITY而不是SELECT Max(ID),但它似乎没有工作... < / p>

编辑:我刚刚将SELECT @@IDENTITY AS NEW_ID更新为SELECT SCOPE_IDENTITY AS NEW_ID,现在我无法将nvarchar转换为int错误......有什么想法吗?

编辑#2:再次更新代码...现在我无法将vaule NULL插入到{Member}中{I} @ID列进入Merchants_Add程序。

3 个答案:

答案 0 :(得分:3)

您应该使用SCOPE_IDENTITY()。

查看SCOPE_IDENTITY (Transact-SQL)

  

@@ IDENTITY和SCOPE_IDENTITY会   返回最后一个标识值   在当前的任何表中生成   会话。但是,SCOPE_IDENTITY   仅返回值内的值   当前范围; @@ IDENTITY不是   仅限于特定范围。

尝试使用类似的东西

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'

请注意使用@ID

来自@@IDENTITY

答案 1 :(得分:2)

尝试:

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'

编辑:使用首选的SCOPE_IDENTITY()代替@@ IDENTITY更改了源代码以反映所需的语法。

答案 2 :(得分:1)

您需要声明并使用T-Sql变量来保存标识值。你通常应该使用Scope_Identity()而不是@@ 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'