Let's say that i have two relational tables : table A and table B.
table B have a foreign key of table A , so in order to insert a row in B we need first to insert a row in A. so i have inserted the first row in A :
insert into A (c1,c2) values(v1,v2)
What i want is to get the id of the inserted row to use it as a foreign key when i insert into the table b . how to do that ?
while searching for a solution i found here , that i need to declare a new table:
declare @KEY table(KEY int)
Insert into A (c1,c2)
Output inserted.Id into @kEY
values(v1,v2)
but when i tried to use it :
Insert into B (C1,C2) values(@KEY,v2)
I get an error telling the @KEY is not declared !
so any idea ?