成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

一個Excel導入SQL Server的例子

數據庫 SQL Server
你可曾想過要如何將Excel表中的數據導入到SQL Server中?在導入的時候,你是否能找到自己的主鍵呢?筆者通過一個例子告訴你,該怎么導入,希望對大家有所幫助。

編者注:你可曾想過要如何將Excel表中的數據導入到SQL Server中?在導入的時候,你是否能找到自己的主鍵呢?筆者通過一個例子告訴你,該怎么導入,希望對大家有所幫助。

有人提問如下:

這個是Excel的,比如是test.xls
 

欠費年份 欠費開始月份 欠費結束月份 應繳金額(月租)  

   2001              9                    12                  94.4  

   2008              5                    12                  88.8  

   2010              8                     12                 90.4

___________________________________________

這個是表:比如是a表

a(pk,int,not null) //主鍵,自動增長

b(varchar(19),null) //費款所屬期

c(decimal(10,2),null) //應繳金額

___________________________________________

現在我要將test.xls中的數據導入到a表,從開始月份到結束月份要做循環導入,比如第一條2001年的從9月到12月要錄入4條數據到a表,導入后的格式如:

select * from a

a        b       c

1 2001-09 94.4

2 2001-10 94.4

3 2001-11 94.4

4 2001-12 94.4

數據庫是:MS Sql server 2008

解析:

思路一:可以使用OpenRowset查詢導入到表變量中,再用游標循環賦值。方法如下:

use testdb2
go
/*******************建立測試數據***3w@live.cn***********************/
IF NOT OBJECT_ID('[TBTest]') IS NULL
DROP TABLE [TBTest]
GO
CREATE TABLE [TBTest](
[tid] int identity(1,1) primary key,

[date] NVARCHAR(20) null,
[Money] decimal(10,2) null)
go

/*******************啟用Ad Hoc Distributed Queries***3w@live.cn***********************/

--------USE master
--
------go

--------sp_configure 'show advanced options', 1
--
------GO
--
----------reconfigure
--
--------啟用分布式查詢 Ad Hoc Distributed Queries
--
------sp_configure 'Ad Hoc Distributed Queries', 1
--
------GO
--
------reconfigure
--
------go

use testdb2
go

/*******************定義表變量***3w@live.cn***********************/

Declare @TableVar table
(PKId
int primary key identity(1,1)
,RYear
int not null,BMonth int not null
,EMonth
int not null,RMoney Decimal(15,2) not null
----,d1 date null,d2 Date null
)

insert into @TableVar
(RYear ,BMonth ,EMonth ,RMoney)
select * from OpenRowSet('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;HDR=Yes;IMEX=1;Database=D:\test\test20110501.xls',
'select * from [Sheet1$]')
 
/*******************第一種方法,用游標***3w@live.cn***********************/

DECLARE @RYear int
declare @BMonth int
declare @EMonth int
declare @RMoney int

DECLARE DateDemo_cursor CURSOR FOR
select RYear,BMonth,EMonth,RMoney from @TableVar where 1=1
OPEN DateDemo_cursor

FETCH NEXT FROM DateDemo_cursor
INTO @RYear,@BMonth,@EMonth,@RMoney

WHILE @@FETCH_STATUS = 0
BEGIN
----print @RYear
----print @BMonth
----print @EMonth
----print @RMoney

--修改記錄
while(@EMonth-@BMonth>=0)
begin
insert INTO [TBTest]
SELECT TOP 1 cast(RYear AS nvarchar(4))+'-'+
CASE WHEN (@BMonth<10) THEN '0'+cast(@BMonth AS nvarchar(2))
ELSE cast(@BMonth AS nvarchar(2)) END,
Rmoney
from @TableVar where Ryear=@RYear

SET @BMonth=@BMonth+1
end
--修改結束
FETCH NEXT FROM DateDemo_cursor into @RYear,@BMonth,@EMonth,@RMoney

END
CLOSE DateDemo_cursor
DEALLOCATE DateDemo_cursor

GO
SELECT * FROM [TBTest]

查詢結果:

/*
tid date Money
1 2001-09 94.40
2 2001-10 94.40
3 2001-11 94.40
4 2001-12 94.40
5 2008-05 88.80
6 2008-06 88.80
7 2008-07 88.80
8 2008-08 88.80
9 2008-09 88.80
10 2008-10 88.80
11 2008-11 88.80
12 2008-12 88.80
13 2010-08 90.40
14 2010-09 90.40
15 2010-10 90.40
16 2010-11 90.40
17 2010-12 90.40
*/

評價:該方法使用了最傳統的方法,思路清晰。但沒有體現SQL server 2008的語法特性,略顯繁瑣。

思路二:可否使用CTE實現?(KillKill提供)

/*
******************第二種方法,用CTE,適用于sql2005/2008/2008 r2*********/
/***************************************3w@live.cn***********************/

TRUNCATE table [TBTest]
go

Declare @TableVar table
(PKId
int primary key identity(1,1)
,RYear
int not null,BMonth int not null
,EMonth
int not null,RMoney Decimal(15,2) not null
);

insert into @TableVar(RYear ,BMonth ,EMonth ,RMoney)
select * from OpenRowSet('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;HDR=Yes;IMEX=1;Database=D:\test\test20110501.xls',
'select * from [Sheet1$]');

with seq as (select top 12 row_number() over (order by object_id) val
from sys.objects)
select
cast(t.RYear AS nvarchar(4))+'-'+
CASE WHEN (t.BMonth+seq.val<10) THEN '0'+cast(t.BMonth+seq.val AS nvarchar(2))
ELSE cast(t.BMonth+seq.val AS nvarchar(2)) END
,RMoney c
from @TableVar t inner join seq
on t.BMonth+seq.val <=
EMonth;

思路三:可否使用SQL Server 2008新提供的Merge實現?

思路四:使用NPOI在業務層實現數據轉換。

思路五:用Master..spt_values表實現(由小F提供)

利用該表,可獲取一定區間內的列表,最長不超過2048,如

select number from master..spt_values
where type='P' and
number between 1 and 5
/*
number
1
2
3
4
5
*/

因為月份最多12,不超過2048,因此可以利用 master..spt_values。

/*******************第五種方法,用master..spt_values,適用于sql2005/2008/2008 r2*********/
/***************************************3w@live.cn***********************/

Declare @TableVar table
(PKId
int primary key identity(1,1)
,RYear
int not null,BMonth int not null
,EMonth
int not null,RMoney Decimal(15,2) not null
----,d1 date null,d2 Date null
);

insert into @TableVar
(RYear ,BMonth ,EMonth ,RMoney)
select * from OpenRowSet('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;HDR=Yes;IMEX=1;Database=D:\test\test20110501.xls',
'select * from [Sheet1$]');

select
tid
=row_number()over(order by getdate()),ltrim(RYear)+'-'+ltrim(right(100+number,2)) as date,
     b.RMoney
as money
from
master..spt_values a,
@TableVar b
where
number between BMonth and EMonth
and
type
='p'

思路六:使用SSIS實現

 

原文鏈接:http://www.cnblogs.com/downmoon/archive/2011/05/02/2034191.html

【編輯推薦】

  1. 曬曬我的通用數據訪問層
  2. 幾步走,教你創建簡單訪問數據庫方法
  3. 一句代碼實現批量數據綁定 下
  4. 一步一步設計你的數據庫1
  5. 不重復隨機數列生成算法

 

責任編輯:艾婧 來源: 博客園
相關推薦

2010-06-28 09:53:11

SQL Server數

2011-08-03 10:40:17

SQL Server數全文檢索

2011-07-12 13:01:11

ExcelOracleSql Server

2009-07-28 11:23:04

Excel導入SQL

2011-08-04 12:49:31

SQL Server數重復數據

2009-07-14 16:02:42

JDBC例子

2009-07-28 11:00:24

Excel導入SQL

2010-11-09 15:30:01

Sql server時

2011-05-19 14:40:33

SQL Server

2014-07-07 10:58:22

SQL Server

2010-07-16 10:12:21

SQL Server導

2010-07-15 13:09:07

SQL Server成

2010-07-21 14:17:36

SQL Server數

2020-03-26 17:00:53

HashMapputJava

2010-04-19 17:21:36

Oracle寫文件

2025-03-26 00:35:00

Javaweb開發

2011-08-15 11:24:46

SQL Server事務

2011-08-01 16:10:00

SQL Server

2022-10-19 11:31:49

TDD開發

2009-08-26 15:53:42

C#數據訪問XML
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲一区二区中文字幕 | 国产乱码精品一区二区三区中文 | 99久久久国产精品 | 国产一区二区三区色淫影院 | 91亚洲国产成人精品一区二三 | xx性欧美肥妇精品久久久久久 | 在线播放一区 | 亚洲精品一二区 | 亚洲日本激情 | 亚洲欧洲精品一区 | 一区二区三区四区国产 | 91亚洲国产成人久久精品网站 | 午夜不卡福利视频 | 成人免费在线网 | 免费在线一区二区三区 | 最新日韩在线 | 国产精品欧美一区二区三区不卡 | 日韩资源 | 91精品国产综合久久婷婷香蕉 | 欧美另类视频 | 日韩第一页 | 国产一级视频在线观看 | 国产高清在线观看 | 北条麻妃一区二区三区在线观看 | 国产在线观看 | 国产精品久久久久久久久久不蜜臀 | 蜜桃视频成人 | 黄色91在线| chinese中国真实乱对白 | 99精品亚洲国产精品久久不卡 | 337p日本欧洲亚洲大胆鲁鲁 | 亚洲精品一区二区在线观看 | 久久这里只有精品首页 | 日韩av啪啪网站大全免费观看 | 中文字幕视频在线观看 | 久久av综合| 性国产xxxx乳高跟 | 国产伦精品一区二区三区视频金莲 | 中文字幕av在线播放 | 久久久久99 | 久久精品国产亚洲 |