0

我試圖將數據插入到個人表中。同時,我必須將插入的人員ID(身份列值)與來自其他表的提供者一起復制到提供者。我試着下面的查詢,但我得到一個錯誤。使用SQL Server OUTPUT ... INTO複製數據時出錯

無法綁定多部分標識符「P.Provider_ID」。

INSERT INTO PERSONS (
      FirstName 
      ,LastName 
      ,Gender 
      ,DOB 
      ,EmailID 
      ,OfficePhone 
      ,Fax 
      ,Mobile 
      ,CertificationType_ID 
      --,License 
      --,StateCertificaionNo 
      --,StateCertificationExpiration 
      --,Comments 
      ,ERTCleared 
      ,IsAMR 
      ,IsActive 
      ,CreatedDate 
      ,ModifiedDate 
      ,IsCrewMember 
      ,UserName 
      ) 
     OUTPUT INSERTED.Person_ID 
      ,P.Provider_ID 
     --,getdate() 
     --,getdate() 
     --,@appuserId 
     --,@appuserId 
     INTO @ProviderPersons(PersonID, ProviderID) 
     --00, CreatedDate, ModifiedDate, CreatedUser_ID, ModifiedUser_ID) 
     SELECT S.FirstName 
      ,S.LastName 
      ,S.Gender 
      ,S.DOB 
      ,S.EmailID 
      ,S.OfficePhone 
      ,S.Fax 
      ,S.Mobile 
      ,1 AS CertificationType_ID 
      ,0 AS ERTCleared 
      ,CASE 
       WHEN S.IsAMR = 'Yes' 
        THEN 1 
       ELSE 0 
       END AS IsAMR 
      ,1 AS IsActive 
      ,getdate() AS CreatedDate 
      ,getdate() AS ModifiedDate 
      ,1 AS IsCrewMember 
      ,S.UserName 
     FROM [dbo].[STA_CrewMembers] S 
     INNER JOIN [dbo].[Providers] P ON P.BusinessUnitNumber = S.BusinessUnitNumber 
      OR P.NAME = S.PROVIDER 
     WHERE NOT EXISTS (
       SELECT 1 
       FROM Persons 
       WHERE EmailID = S.EmailID 
       ) 

請告訴我如何以更好的方式處理這個問題。提前致謝。

+0

可能的重複數據刪除:輸出Inserted.Id而另一場]( http://stackoverflow.com/q/37339422/15498) –

回答

1

使用

OUTPUT INSERTED.Person_ID 
     ,INSERTED.Provider_ID 

你不能因表別名使用下表alias..Example將無法正常工作

declare @id table 
(
id int 
) 

insert into t1 
output n.id into @id 
select top 10* from numbers n 


select * from @id 
+0

我知道這種方式的問題是我不插入provider_id我的人表。 – StackUser

+0

@StackUser:那麼問題是什麼? – TheGameiswar

相關問題