I am inserting records from our crm to our erp. The phone table uses a identity column from the people table. I need to insert the people record and capture the value in the PersonId which is an identity column and then use that PersonId as the key to insert records to the Phone table. I get the error: Msg 137, Level 16, State 1, Line 16 Must declare the scalar variable "@IdentityID". Msg 137, Level 16, State 1, Line 17 Must declare the scalar variable "@IdentityID".
--IdentityTable
CREATE TABLE [dbo].[People](
[People_ID] [int] NOT NULL,
[text] [nvarchar](50) NULL,
[PersonId] [int] IDENTITY(1,1) NOT NULL
) ON [PRIMARY]
--Phone
CREATE TABLE [dbo].[Phone](
[PersonId] [int] NOT NULL,
[text] [nvarchar](50) NULL,
[Number] [nchar](10) NULL
) ON [PRIMARY]
declare @IdentityID table (PersonId int);
INSERT INTO [Bridge].[dbo].[People]
([People_ID]
,[text])
output Inserted.PersonId into @IdentityID
VALUES
(3,'row1'),
(4,'row2');
INSERT INTO [Bridge].[dbo].[Phone]
(PersonId
,[text]
,[Number])
VALUES
(@IdentityID,'row1'),
(@IdentityID,'row2');
Print 'IdentityID' + @IdentityID
Msg 137, Level 16, State 1, Line 16 Must declare the scalar variable "@IdentityID". Msg 137, Level 16, State 1, Line 17 Must declare the scalar variable "@IdentityID".