SQL Server insert与Select组合使用的方法

数据库 SQL Server
在SQL Server中,insert语句和Select语句是可以组合使用的,下面就将为您具体介绍该方法,希望对您学习SQL Server insert与Select的语句能有所启迪。

SQL Server insert语句是SQL中最常见也是最重要的语句之一,下文将为您解读SQL Server insert与Select组合使用的方法,供您参考。

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);

 

 

【编辑推荐】

教您如何使用SQL中的函数替代游标

SQL中的分析函数

创建SQL函数的实例

SQL中循环语句的效果实例

SQL中类似For循环处理的实例

责任编辑:段燃 来源: 赛迪网
相关推荐

2010-09-03 15:27:02

SQLSELECT语句

2010-09-13 10:43:22

SQL Server

2010-11-11 11:37:22

SQL SELECT语

2011-08-01 22:41:49

SQL Server数Insert

2010-09-07 13:50:41

SQL语句

2011-07-22 16:59:30

MySQL数据库嵌套查询

2010-09-06 09:36:51

SQL语句

2010-11-11 10:18:59

select into

2010-11-09 15:06:52

SQL Server动

2010-07-14 15:22:48

SQL Server

2010-07-02 14:52:21

SQL Server元

2011-07-01 13:39:28

SQL ServerInsert

2010-09-10 11:01:09

sql变量

2010-09-16 09:49:38

sql server建

2010-10-20 11:07:21

sql server全

2010-07-02 13:18:01

SQL Server

2010-07-15 09:14:32

SQL server组

2010-10-19 14:11:20

SQL SERVER恢

2010-09-06 09:53:41

SQL Server语句

2010-11-10 15:36:44

SQL Server存
点赞
收藏

51CTO技术栈公众号