| 订阅 | 在线投稿
分享
 
 
 

SQL Server中Insert与Select的组合使用

来源:互联网  宽屏版  评论
2008-06-01 02:13:16

SQL Server中有示例数据库Northwind。新建表Sales用于存储销售信息,字段为EmployeeID,ProductID,SupplierID,CustomerID,OrderDate,UnitPrice,Total,Quantity,Discount。

下面的语句从Orders,Order Details, Employees,Products, Suppliers, Customers表选择相应的

数据插入Sales表:

insert into Sales(EmployeeID,ProductID,SupplierID,CustomerID,

OrderDate,UnitPrice,Total,Quantity,Discount)

select e.EmployeeID, p.ProductID, s.SupplierID,

c.CustomerID, o.OrderDate, od.UnitPrice,

od.Quantity*od.UnitPrice*(1.0-od.Discount)Total,

Od.Quantity, od.Discount

from Orders o,[Order Details] od, Employees e,

Products p, Suppliers s, Customers c

where (o.OrderID = od.OrderID) and

(o.EmployeeID = e.EmployeeID) and

(o.CustomerID = c.CustomerID) and

(od.ProductId = p.ProductID) and

(p.SupplierID = s.SupplierID);

go

 
SQL Server中有示例数据库Northwind。新建表Sales用于存储销售信息,字段为EmployeeID,ProductID,SupplierID,CustomerID,OrderDate,UnitPrice,Total,Quantity,Discount。 下面的语句从Orders,Order Details, Employees,Products, Suppliers, Customers表选择相应的 数据插入Sales表: insert into Sales(EmployeeID,ProductID,SupplierID,CustomerID, OrderDate,UnitPrice,Total,Quantity,Discount) select e.EmployeeID, p.ProductID, s.SupplierID, c.CustomerID, o.OrderDate, od.UnitPrice, od.Quantity*od.UnitPrice*(1.0-od.Discount)Total, Od.Quantity, od.Discount from Orders o,[Order Details] od, Employees e, Products p, Suppliers s, Customers c where (o.OrderID = od.OrderID) and (o.EmployeeID = e.EmployeeID) and (o.CustomerID = c.CustomerID) and (od.ProductId = p.ProductID) and (p.SupplierID = s.SupplierID); go
󰈣󰈤
 
 
 
>>返回首页<<
 
 热帖排行
 
 
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
©2005- 王朝网络 版权所有