Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2005 如何使用sql语句将不同表中的某些列复制到另一个表中_Sql Server 2005_Tsql - Fatal编程技术网

Sql server 2005 如何使用sql语句将不同表中的某些列复制到另一个表中

Sql server 2005 如何使用sql语句将不同表中的某些列复制到另一个表中,sql-server-2005,tsql,Sql Server 2005,Tsql,我对Sql编码非常紧张。任何解决方案请帮助。。。。 我有如下表格: OrderedFood(**TableID,FoodID**,Qty,OrderedDate) Invoices(**InvoiceID**,TableID,Amount) InvoiceDetail(**InvoiceID,FoodID**,Qty,orderedDate) 我想复制OrderedFood.TableID=>Invoices.TableID。然后复制OrderedFood.FoodID=>Invo

我对Sql编码非常紧张。任何解决方案请帮助。。。。 我有如下表格:

  OrderedFood(**TableID,FoodID**,Qty,OrderedDate)
  Invoices(**InvoiceID**,TableID,Amount)
  InvoiceDetail(**InvoiceID,FoodID**,Qty,orderedDate)
我想复制OrderedFood.TableID=>Invoices.TableID。然后复制OrderedFood.FoodID=>InvoiceDetail.FoodID,OrderedFood.Qty=>InvoiceDetail.Qty,其中包含许多行

我怎么做呢


提前感谢…

您应该使用
插入…,例如选择
类型查询

INSERT INTO table1 ( column1 )
SELECT  col1
FROM    table2

另一个是
selectinto
语句

SELECT column_name(s)
INTO new_table_name [IN externaldatabase]
FROM old_tablename

我恐怕有点难理解你到底想做什么。 希望下面的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

我认为你不能这样做;因为您不是简单地处理数据(在这种情况下,插入就足够了),而是尝试将列复制/添加到现有表中,该表需要重新构造。是的,这就是我想要的。你知道怎么做吗?听着,我想复制Invoices.InvoiceID=>InvoiceDetail.InvoiceID,同时复制OrderedFood.FoodID=>InvoiceDetail.FoodID,orderedFoody.Qty=>InvoiceDetail.QtyOh,这才是真正的解决方案。谢谢你,马克