Let's say that I have a transact-SQL query that does something like this:
INSERT INTO Table1 -- Table1 has an identity column for the primary key
(
Table1Value1,
Table1Value2
)
VALUES
(
@Table1Value1, -- SqlParameter
@Table1Value2 -- SqlParameter
)
DECLARE @table1ID int = SCOPE_IDENTITY();
INSERT INTO Table2 -- Table2 also has and identity column for the PK
(
Table1ID -- Must have foreign key to Table1 record
Table2Value1,
Table2Value2
)
VALUES
(
@table1ID,
@Table2Value1, -- SqlParameter
@Table2Value2 -- SqlParameter
)
DECLARE @table2ID int = SCOPE_IDENTITY();
INSERT INTO Table3
(
Table1ID, -- Foreign Key to Table1 record
Table2ID, -- Foreign Key to Table2 record
Table3Value1,
Table3Value2
)
VALUES
(
@table1ID, -- Foreign Key to Table1 record
@table2ID, -- Foreign Key to Table2 record
@Table3Value1, -- SqlParameter
@Table3Value2 -- SqlParameter
)
Is there a way to make this work? Can you escape the local transact-sql variable names so they're ignored by whatever figures out the location of the parameters? Can you tell SqlClient to use something other than @ to identify parameters? Is there a way to make this work without using a stored proc or having to run subqueries?
Thanks.