Боюсь, немного сложно понять, что именно вы пытаетесь сделать.Надеемся, что следующий SQL поможет вам указать правильное направление
DECLARE @OrderedFood Table(TableID int ,FoodID int ,Qty int ,OrderedDate datetime)
DECLARE @Invoices Table(InvoiceID int IDENTITY(1,1)PRIMARY KEY CLUSTERED, TableID int ,Amount money)
DECLARE @InvoiceDetail Table(InvoiceID int,FoodID int,Qty int ,orderedDate datetime)
insert into @orderedfood values(1,1,1,'2010/05/21')
insert into @orderedfood values(1,2,3,'2010/05/21')
insert into @orderedfood values(1,3,2,'2010/05/21')
insert into @orderedfood values(2,1,4,'2010/05/21')
insert into @orderedfood values(2,2,2,'2010/05/21')
insert into @Invoices(TableId) SELECT distinct TableId From @OrderedFood
Insert into @InvoiceDetail(InvoiceID,FoodID,Qty,orderedDate)
SELECT InvoiceID,FoodId,Qty,OrderedDate
FROM @OrderedFood o
inner join @Invoices i on o.tableid = i.tableid
select * from @invoices i inner join @invoicedetail id on i.invoiceid = id.invoiceid