代码改变世界

T-SQL 之 Insert

2013-01-27 21:52  Eric.Hu  阅读(370)  评论(0编辑  收藏  举报

1. Insert 基本语法:

INSERT [INTO] <table>

  [(<column list>)]

VALUES (<data values>) [,(<data values>)] [,...n]

 

2. 多行插入(sql 2008 新功能)

INSERT INTO  Sales

  (StoreCode,OrderNumber,OrderDate)

VALUES

  ('TST1','TESTORDER1','01/01/2000'),

  ('TST1','TESTORDER2','01/01/2000');

 

3. INSERT INTO <table name>

  (<column list>)

  <SELECT statement>

注:<table name>需要在数据库存在或者事先申明.

USE AdventureWorks2008

DECLARE @MyTable Table

(

  SalesOrderID int,

  CustomerID char(5)

);

INSERT INTO @MyTable

  SELECT SalesOrderID,CustomerID

  FROM AdventureWorks2008.Sales.SalesOrderHeader

  WHERE SalesOrderID BETWEEN 44000 AND 44010;

 

SELECT * FROM @MyTable;