2016-08-12 2 views
0

SET @sqlindent= SCOPE_IDENTITY()で新しく挿入された行から主キー値を取得できます。私は3つのテーブルに3つの挿入ステートメントを持っているストアドプロシージャを持っています。このストアドプロシージャのテーブル3から2番目の主キーを取得するにはどうすればよいですか?

  • ステップ1:主キー値を取得し、テーブルに一つの新たな行を挿入(SET @sqlindent= SCOPE_IDENTITY())

  • ステップ2:表1から表に二つのフィールドを挿入します。 Insert Into table two [id] Value (@sqlindent)したがって、テーブル2のidフィールド=テーブル1つの主キー。

  • ステップ3:テーブル2からテーブル3に挿入します。 Insert Into table three [id] Value (@sqlindent)だから、表3のidは=表も提出された1と2の主キー

最後に、私はテーブル3から主キーを持つテーブル3およびUPDATE表2から主キー値を取得する必要があります。これは私が援助を必要とする場所です。どのようにして第3のテーブルから第2の主キーを取り出すのですか?

これまで私がこれまで持っていたことは次のとおりです。すべての声明は最後を除いて動作します

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 
+0

は、すべての3つのテーブルには、ID列を持っていますか?これらの列もすべてプライマリキーとして設定されていますか、または少なくともユニークな制約が設定されていますか? –

+0

値が増えている場合は、おそらく2番目に高い値を選択できますか? – dfundako

+1

試してみてくださいoutput.inserted – TheGameiswar

答えて

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 
+0

Batstos:あなたはまだ二SCOP_Idenyity –

+0

SELECT SCOPE_IDENTITYを(取得運を示唆していないとして、いくつかの理由は、SO [ServiceAtLoc_tbl] SET(ATSIGN)ServiceIdent = SCOPE_IDENTITY() UPDATEから)私は私がした私の変更のコードでお答えさせません内側ServiceAtLoc_tbl FROM AccountContacts_tbl SET AccountSvcAtID = ServiceAtLocID はdbo.AccountContacts_tbl.AccountSvcAtID = dbo.ServiceAtLoc_tbl.ServiceAtLocID ServiceAtLoc_tbl.ServiceAtLocID =(ATSIGN)ServiceIdent ON dbo.AccountContacts_tblに参加します –

関連する問題