我們向表插入數據的傳統方法有三個,介紹如下:
方法一
假設我們有一個名為MyTestDB的數據庫,其中有一個名為MyTest1的表,數據庫和表的創建過程如下:
USE[master]
GO
IFEXISTS(SELECTnameFROMsys.databases
WHEREname=N'MyTestDB')
DROPDATABASE[MyTestDB]
GO
CreatedatabaseMyTestDB
Go
Use[MyTestDB]
Go
IFEXISTS(SELECT*FROMsys.objects
WHEREobject_id=OBJECT_ID(N'[dbo].[MyTest1]')
ANDtypein(N'U'))
DROPTABLE[dbo].[MyTest1]
GO
USE[MyTestDB]
GO
SETANSI_NULLSON
GO
SETQUOTED_IDENTIFIERON
GO
SETANSI_PADDINGON
GO
CREATETABLE[dbo].[MyTest1](
[Id][int]NULL,
[Fname][varchar](100)NULL,
[Lname][varchar](100)NULL,
[salary][money]NULL
)ON[PRIMARY]
GO
SETANSI_PADDINGOFF
GO
現在我們用傳統的ANSI插入語句向上表添加5行數據,這裡需要用到帶VALUE從句的INSERT SQL語句來插入數據,腳本如下:
insert into MyTest1 (id ,fname ,lname , salary) values (1 , 'John' , 'Smith' , 150000.00)
insert into MyTest1 (id ,fname ,lname , salary) values (2 , 'Hillary' , 'Swank' , 250000.00)
insert into MyTest1 (id ,fname ,lname , salary) values (3 , 'Elisa' , 'Smith' , 120000.00)
insert into MyTest1 (id ,fname ,lname , salary) values (4 , 'Liz' , 'Carleno' , 151000.00)
insert into MyTest1 (id ,fname ,lname , salary) values (5 , 'Tony' , 'Mcnamara' , 150300.00)
執行結果如下:
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
方法二
假設我們在上述的MyTestDB數據庫中有表MyTest2,如下:
USE[MyTestDB]
GO
SETANSI_NULLSON
GO
SETQUOTED_IDENTIFIERON
GO
SETANSI_PADDINGON
GO
IFEXISTS(SELECT*FROMsys.objectsWHEREobject_id=OBJECT_ID(N'[dbo].[MyTest2]')ANDtypein(N'U'))
DROPTABLE[dbo].[MyTest2]
GO
CREATETABLE[dbo].[MyTest2](
[Id][int]NULL,
[Fname][varchar](100)NULL,
[Lname][varchar](100)NULL,
[salary][money]NULL
)ON[PRIMARY]
GO
SETANSI_PADDINGOFF
GO
下面我們再用另外一種傳統的插入方法同樣添加5行數據,也就是使用帶SELECT從句的INSERT SQL語句,腳本如下:
insert into MyTest2 select 1 , 'John' , 'Smith' , 150000.00
insert into MyTest2 select 2 , 'Hillary' , 'Swank' , 250000.00
insert into MyTest2 select 3 , 'Elisa' , 'Smith' , 120000.00
insert into MyTest2 select 4 , 'Liz' , 'Carleno' , 151000.00
insert into MyTest2 select 5 , 'Tony' , 'Mcnamara' , 150300.00
執行結果如下:
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
方法三
同樣的,我們再假設上述的MyTestDB數據庫中有表MyTest3,如下:
USE[MyTestDB]
GO
IFEXISTS(SELECT*FROMsys.objectsWHEREobject_id=OBJECT_ID(N'[dbo].[MyTest3]')ANDtypein(N'U'))
DROPTABLE[dbo].[MyTest3]
GO
USE[MyTestDB]
GO
SETANSI_NULLSON
GO
SETQUOTED_IDENTIFIERON
GO
SETANSI_PADDINGON
GO
CREATETABLE[dbo].[MyTest3](
[Id][int]NULL,
[Fname][varchar](100)NULL,
[Lname][varchar](100)NULL,
[salary][money]NULL
)ON[PRIMARY]
GO
SETANSI_PADDINGOFF
GO
下面我們用第三種傳統的插入方法同樣添加5行數據,這裡使用的是帶SELECT從句和UNION從句的INSERT SQL語句,腳本如下:
insert into MyTest3
select 1 , 'John' , 'Smith' , 150000.00
union select 2 , 'Hillary' , 'Swank' , 250000.00
union select 3 , 'Elisa' , 'Smith' , 120000.00
union select 4 , 'Liz' , 'Carleno' , 151000.00
union select 5 , 'Tony' , 'Mcnamara' , 150300.00
執行結果如下:
(5 row(s) affected)
方法四
最後一種方法,需要插入數據的對象是MyTestDB數據庫中的表MyTest4,如下:
USE[MyTestDB]
GO
IFEXISTS(SELECT*FROMsys.objectsWHEREobject_id=OBJECT_ID(N'[dbo].[MyTest4]')ANDtypein(N'U'))
DROPTABLE[dbo].[MyTest4]
GO
USE[MyTestDB]
GO
SETANSI_NULLSON
GO
SETQUOTED_IDENTIFIERON
GO
SETANSI_PADDINGON
GO
CREATETABLE[dbo].[MyTest4](
[Id][int]NULL,
[Fname][varchar](100)NULL,
[Lname][varchar](100)NULL,
[salary][money]NULL
)ON[PRIMARY]
GO
SETANSI_PADDINGOFF
GO
現在我們要用到SQL Server 2008中提供的新方法——行值構造器的插入SQL語句為上述表插入5行數據,這種方法可以在一個INSERT語句中一次性插入多行數據,腳本如下:
insert into MyTest4 (id ,fname ,lname , salary) values
(1 , 'John' , 'Smith' , 150000.00),
(2 , 'Hillary' , 'Swank' , 250000.00),
(3 , 'Elisa' , 'Smith' , 120000.00),
(4 , 'Liz' , 'Carleno' , 151000.00),
(5 , 'Tony' , 'Mcnamara' , 150300.00)
執行結果如下:
(5 row(s) affected)