1. 程式人生 > >EXCEL中CONCATENATE()函式的使用

EXCEL中CONCATENATE()函式的使用

在EXCEL中可以使用CONCATENATE()函式進行字串的拼接。或者合成SQL插入語句。

下面舉例說明EXCEL中利用CONCATENATE()函式生成資料庫的insert指令碼

1、準備EXCEL資料,如圖所示

序號 姓名 語文 數學 英語 物理 化學 生物
1 張珊 100 101 102 103 90 60
2 李四 101 102 103 104 89 59
3 王二 102 103 104 105 88 58

2、在資料庫中建立表結構

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tblScore]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
-- 刪除表
drop table [dbo].tblScore
go

create table dbo.tblScore (
	Id int not null
	, Name nvarchar(255) null
	, Score1 int null 
	, Score2 int null
	, Score3 int null
	, Score4 int null
	, Score5 int null
	, Score6 int null
)

go

3、在EXCEL中生成插入語句

=CONCATENATE("Insert into tblScore ( Id,Name,Score1,Score2,Score3,Score4,Score5,Score6 ) Values ( '",A2,"',N'",B2,"','",C2,"','",D2,"','",E2,"','",F2,"','",G2,"','",H2,"')  ")

=CONCATENATE("Insert into tblScore ( Id,Name,Score1,Score2,Score3,Score4,Score5,Score6 ) Values ( '",A3,"',N'",B3,"','",C3,"','",D3,"','",E3,"','",F3,"','",G3,"','",H3,"')  ")

=CONCATENATE("Insert into tblScore ( Id,Name,Score1,Score2,Score3,Score4,Score5,Score6 ) Values ( '",A4,"',N'",B4,"','",C4,"','",D4,"','",E4,"','",F4,"','",G4,"','",H4,"')  ")
Insert into tblScore ( Id,Name,Score1,Score2,Score3,Score4,Score5,Score6 ) Values ( '1',N'張珊','100','101','102','103','90','60')  
Insert into tblScore ( Id,Name,Score1,Score2,Score3,Score4,Score5,Score6 ) Values ( '2',N'李四','101','102','103','104','89','59')  
Insert into tblScore ( Id,Name,Score1,Score2,Score3,Score4,Score5,Score6 ) Values ( '3',N'王二','102','103','104','105','88','58')  

4、在資料庫中插入資料並查詢驗證

完整程式碼如下:

use Test
go

---------- create table Score ----------

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tblScore]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
-- 刪除表
drop table [dbo].tblScore
go

create table dbo.tblScore (
	Id int not null
	, Name nvarchar(255) null
	, Score1 int null 
	, Score2 int null
	, Score3 int null
	, Score4 int null
	, Score5 int null
	, Score6 int null
)

go
---------- insert data ----------


Insert into tblScore ( Id,Name,Score1,Score2,Score3,Score4,Score5,Score6 ) Values ( '1',N'張珊','100','101','102','103','90','60')  
Insert into tblScore ( Id,Name,Score1,Score2,Score3,Score4,Score5,Score6 ) Values ( '2',N'李四','101','102','103','104','89','59')  
Insert into tblScore ( Id,Name,Score1,Score2,Score3,Score4,Score5,Score6 ) Values ( '3',N'王二','102','103','104','105','88','58')  

select * from dbo.tblScore