你不能那样做。
我想这可能是你想要做的?
INSERT INTO Contracts (
JobId,
InvoiceNumber
ExpensesId)
SELECT
j.Id,
j.InvoiceNumber,
j.ExpensesID
FROM Expenses
INNER JOIN Jobs j ON expenses.Id = j.ExpensesId
如果您需要复制费用行,您需要先执行此操作,然后用于SCOPE_IDENTITY
获取新 ID。
declare @expenseid int = -- your expense ID
declare @newid int
INSERT Expenses (Bus, Toll)
SELECT Bus, Toll FROM Expenses WHERE Id = @expenseID
SELECT @newID = SCOPE_IDENTITY()
INSERT INTO Contracts (
JobId,
InvoiceNumber
ExpensesId)
SELECT
j.Id,
j.InvoiceNumber,
@newID
FROM Expenses
WHERE Id = @expenseID
如果你想做一个批处理
DECLARE @inserts table(expenseid int, jobid int)
INSERT Expenses (Bus, Toll)
OUTPUT inserted.id, Jobs.ID INTO @inserts
SELECT Bus, Toll FROM Expenses
INNER JOIN Jobs ON Jobs.ExpenseID = Expenses.ID
INSERT Contracts (JobID, InvoiceNumber, ExpensesID)
SELECT j.ID, j.InvoiceNumber, i.ExpenseID
FROM Jobs j
INNER JOIN @inserts I on j.ID = i.jobid
...或类似的东西-不太确定我完全理解您的数据结构。