Home > Database > Mysql Tutorial > sql insert select语句的使用方法

sql insert select语句的使用方法

WBOY
Release: 2016-06-07 17:48:15
Original
5385 people have browsed it

sql insert 语句的使用方法

INSERT 语句中的 SELECT 子查询可用于将一个或多个其它的表或视图的值添加到表中。使用 SELECT 子查询可同时插入多行。

下面的 INSERT 语句将 titles 中数据的 type 是 modern cooking 的所有行的数据插入到一个单独的表中:

USE pubsINSERT INTO MyBooks   SELECT title_id, title, type   FROM titles   WHERE type = 'mod_cook'子查询的选择列表必须与 INSERT 语句列的列表匹配。如果没有指定列的列表,选择列表必须与正向其插入的表或视图的列匹配。

INSERT...SELECT 语句的另一个作用是从 Microsoft® SQL Server™ 的外部数据源插入数据。INSERT 语句中的 SELECT 可以:

使用由四部分组成的名称引用链接服务器上的远程表。有关更多信息,请参见使用链接服务器名标识数据源。


使用 OPENROWSET 引用远程表。有关更多信息,请参见使用特殊名称标识数据源。


7> CREATE TABLE Customers (
8>      CustomerID nchar (5) NOT NULL ,
9>      CompanyName nvarchar (40) NOT NULL ,
10>     ContactName nvarchar (30) NULL ,
11>     ContactTitle nvarchar (30) NULL ,
12>     Address nvarchar (60) NULL ,
13>     City nvarchar (15) NULL ,
14>     Region nvarchar (15) NULL ,
15>     PostalCode nvarchar (10) NULL ,
16>     Country nvarchar (15) NULL ,
17>     Phone nvarchar (24) NULL ,
18>     Fax nvarchar (24) NULL
19> )
20> GO
1>
2>
3> INSERT INTO Customers (CustomerID, CompanyName) SELECT '1', 'Bam Bam'
4> GO

(1 rows affected)
1>
2> select * from Customers;
3> GO
CustomerID CompanyName                              ContactName                    ContactTitle                   Address                                                      City            Region
       PostalCode Country         Phone                    Fax
---------- ---------------------------------------- ------------------------------ ------------------------------ ------------------------------------------------------------ --------------- ---------
------ ---------- --------------- ------------------------ ------------------------
1          Bam Bam                                  NULL                           NULL                           NULL                                                         NULL            NULL
       NULL       NULL            NULL                     NULL

(1 rows affected)
1>
2> drop table Customers;
3> GO

实例

3> INSERT INTO BillingArchive
4>     (BankerID, BillingNumber, BillingTotal, CreditTotal,
5>     PaymentTotal, TermsID, BillingDate, BillingDueDate)
6> SELECT
7>     BankerID, BillingNumber, BillingTotal, CreditTotal,
8>     PaymentTotal, TermsID, BillingDate, BillingDueDate
9> FROM Billings
10> WHERE BillingTotal - PaymentTotal - CreditTotal = 0
11> GO

也可以利用存储过程处理

3> CREATE PROCEDURE spIns_Employee
4> @FirstName    nVarChar(50),
5> @LastName  nVarChar(25),
6> @Salary   Money
7> AS
8> INSERT INTO Employee (ID, First_name, last_Name, Salary)
9> SELECT 10, @FirstName, @LastName, @Salary
10> GO
1> spIns_Employee 'Gadget', 'Bond', 49.95
2> GO

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template