如何从此存储过程中的表三中检索第二个主键?

时间:2016-08-12 14:48:28

标签: sql-server tsql

我可以使用SET @sqlindent= SCOPE_IDENTITY()从新插入的行中检索主键值。我有一个存储过程,在三个表中有三个插入语句。

  • 第一步:在表格中插入一个新行,检索主键值(SET @sqlindent= SCOPE_IDENTITY())

  • 第二步:从表一中插入表格中的两个字段。 Insert Into table two [id] Value (@sqlindent)所以表二id字段=表一主键。

  • 第三步:从表二中插入表三。 Insert Into table three [id] Value (@sqlindent)因此,表3的id也是=表一和两个主键

最后,我需要从表3中检索主键值,使用表3中的主键检索UPDATE表2。这是我需要帮助的地方。如何从表3中检索第二个主键?

这是我到目前为止所拥有的。除最后一个

之外,所有语句都有效
SET @sqlidentsrvat = SCOPE_IDENTITY()

像往常一样感谢。

ALTER PROCEDURE [dbo].[xxxxxxxxxx]
    @AccountCompany varchar(100),
    @AccountAddress1 varchar(256),
    @AccountAddress2 varchar(256),
    @AccountCity varchar(100),
    @AccountState varchar(4),
    @AccountZip nchar(10),
    @AccountCountry varchar(80),
    @AccountPriPhone varchar(22),
    @AccountSecPhone varchar(22),
    @AccountCreateDate DateTime,
    @sqlident int output,
    @AccountContactISPrimary int,
    @AccountContactNameFirst varchar(35),
    @AccountContactNameLast varchar(35),
    @AccountContactPhone1 varchar(22),
    @AccountContactPhone1Ext varchar(8),
    @AccountContactPhone2 varchar(22),
    @AccountContactPhone2Ext varchar(8),
    @AccountContactEmail varchar(100)
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @sqlidentsrvat INT;

    INSERT INTO [Account_tbl] ([AccountCompany], [AccountAddress1],[AccountAddress2], [AccountCity], [AccountState], [AccountZip], [AccountCountry],[AccountPriPhone], [AccountSecPhone])
    VALUES (@AccountCompany, @AccountAddress1, @AccountAddress2, @AccountCity, @AccountState, @AccountZip, @AccountCountry, @AccountPriPhone, @AccountSecPhone)

    SET @sqlident = SCOPE_IDENTITY()

    INSERT INTO [AccountContacts_tbl] ([AccountNumberID],[AccountContactISPrimary], [AccountContactNameFirst], [AccountContactNameLast],[AccountContactPhone1], [AccountContactPhone2], [AccountContactEmail],[AccountContactPhone1Ext], [AccountContactPhone2Ext])
    VALUES (@sqlident, @AccountContactISPrimary, @AccountContactNameFirst, @AccountContactNameLast, @AccountContactPhone1, @AccountContactPhone2, @AccountContactEmail, @AccountContactPhone1Ext, @AccountContactPhone2Ext)

    INSERT INTO [ServiceAtLoc_tbl] ([ServiceAccountID],[ServiceAtCompanyName], [ServiceAtAddress1], [ServiceAtAddress2],[ServiceAtCity], [ServiceAtState], [ServiceAtZip], [ServiceAtCountry])
    VALUES(@sqlident, @AccountCompany, @AccountAddress1, @AccountAddress2, @AccountCity, @AccountState, @AccountZip, @AccountCountry)

    SET @sqlidentsrvat = SCOPE_IDENTITY()

    UPDATE AccountContacts_tbl
    SET AccountSvcAtID = @sqlidentsrvat
    FROM AccountContacts_tbl
    INNER JOIN dbo.ServiceAtLoc_tbl ON dbo.AccountContacts_tbl.AccountSvcAtID = dbo.ServiceAtLoc_tbl.ServiceAtLocID
    WHERE AccountContacts_tbl.AccountSvcAtID = ServiceAtLoc_tbl.ServiceAtLocID
END

对于那些在这里阅读的最终代码,我可以根据需要进行操作。感谢所有回复的人的帮助。

ALTER PROCEDURE [dbo].[XXXXXXXXXX]

    @AccountCompany varchar(100),
    @AccountAddress1 varchar(256),
    @AccountAddress2 varchar(256),
    @AccountCity varchar(100),
    @AccountState varchar(4),
    @AccountZip nchar(10),
    @AccountCountry varchar(80),
    @AccountPriPhone varchar(22),
    @AccountSecPhone varchar(22),
    @AccountCreateDate DateTime,
    @sqlident int output,

    @AccountContactISPrimary int,
    @AccountContactNameFirst varchar(35),
    @AccountContactNameLast varchar(35),
    @AccountContactPhone1 varchar(22),
    @AccountContactPhone1Ext varchar(8),
    @AccountContactPhone2 varchar(22),
    @AccountContactPhone2Ext varchar(8),
    @AccountContactEmail varchar(100),
    @ServiceIdent int output

AS
BEGIN

    SET NOCOUNT ON;


INSERT INTO [Account_tbl] 
    ([AccountCompany],[AccountAddress1],[AccountAddress2],[AccountCity], [AccountState],[AccountZip],[AccountCountry],[AccountPriPhone],[AccountSecPhone])
    Values
    (@AccountCompany,@AccountAddress1,@AccountAddress2,@AccountCity,@AccountState,@AccountZip,@AccountCountry,@AccountPriPhone,@AccountSecPhone)

SET @sqlident = SCOPE_IDENTITY()

INSERT INTO [AccountContacts_tbl]
    ([AccountNumberID],[AccountContactISPrimary],[AccountContactNameFirst],[AccountContactNameLast],[AccountContactPhone1],[AccountContactPhone2],[AccountContactEmail],[AccountContactPhone1Ext],[AccountContactPhone2Ext])
    Values
    (@sqlident,@AccountContactISPrimary,@AccountContactNameFirst,@AccountContactNameLast,@AccountContactPhone1,@AccountContactPhone2,@AccountContactEmail,@AccountContactPhone1Ext,@AccountContactPhone2Ext)

INSERT INTO [ServiceAtLoc_tbl]
    ([ServiceAccountID],[ServiceAtCompanyName],[ServiceAtAddress1],[ServiceAtAddress2],[ServiceAtCity], [ServiceAtState],[ServiceAtZip],[ServiceAtCountry])
VALUES
    (@sqlident,@AccountCompany,@AccountAddress1,@AccountAddress2,@AccountCity,@AccountState,@AccountZip,@AccountCountry)

SET @ServiceIdent = SCOPE_IDENTITY()

UPDATE AccountContacts_tbl
SET AccountSvcAtID = ServiceAtLocID
FROM ServiceAtLoc_tbl
WHERE ServiceAccountID = AccountNumberID and ServiceAtLoc_tbl.ServiceAtLocID = @ServiceIdent

END

1 个答案:

答案 0 :(得分:0)

在第三次插入后,执行此操作以更新第二个表...

-- Retrieve the primary key (from the third insert)
SET @sqlidentsrvat = SCOPE_IDENTITY()

-- Update the record
UPDATE [AccountContacts_tbl]
SET AccountSvcAtID = @sqlidentsrvat
WHERE [AccountNumberID] = @sqlident