亚洲韩日午夜视频,欧美日韩在线精品一区二区三区,韩国超清无码一区二区三区,亚洲国产成人影院播放,久草新在线,在线看片AV色

您好,歡迎來到思海網(wǎng)絡(luò),我們將竭誠為您提供優(yōu)質(zhì)的服務(wù)! 誠征網(wǎng)絡(luò)推廣 | 網(wǎng)站備案 | 幫助中心 | 軟件下載 | 購買流程 | 付款方式 | 聯(lián)系我們 [ 會員登錄/注冊 ]
促銷推廣
客服中心
業(yè)務(wù)咨詢
有事點(diǎn)擊這里…  531199185
有事點(diǎn)擊這里…  61352289
點(diǎn)擊這里給我發(fā)消息  81721488
有事點(diǎn)擊這里…  376585780
有事點(diǎn)擊這里…  872642803
有事點(diǎn)擊這里…  459248018
有事點(diǎn)擊這里…  61352288
有事點(diǎn)擊這里…  380791050
技術(shù)支持
有事點(diǎn)擊這里…  714236853
有事點(diǎn)擊這里…  719304487
有事點(diǎn)擊這里…  1208894568
有事點(diǎn)擊這里…  61352289
在線客服
有事點(diǎn)擊這里…  531199185
有事點(diǎn)擊這里…  61352288
有事點(diǎn)擊這里…  983054746
有事點(diǎn)擊這里…  893984210
當(dāng)前位置:首頁 >> 技術(shù)文章 >> 文章瀏覽
技術(shù)文章

在Excel中如何導(dǎo)入SQL Server數(shù)據(jù)

添加時(shí)間:2012-6-1  添加: admin 

這個(gè)是Excel的,比如是test.xls
 

欠費(fèi)年份 欠費(fèi)開始月份 欠費(fèi)結(jié)束月份 應(yīng)繳金額(月租) 

2001              9                    12                  94.4 

2008              5                    12                  88.8 

2010              8                     12                 90.4

___________________________________________

這個(gè)是表:比如是a表

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

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

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

___________________________________________

現(xiàn)在我要將test.xls中的數(shù)據(jù)導(dǎo)入到a表,從開始月份到結(jié)束月份要做循環(huán)導(dǎo)入,比如第一條2001年的從9月到12月要錄入4條數(shù)據(jù)到a表,導(dǎo)入后的格式如:

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

數(shù)據(jù)庫是:MS Sql server 2008

解析:

思路一:可以使用OpenRowset查詢導(dǎo)入到表變量中,再用游標(biāo)循環(huán)賦值。方法如下:

use testdb2go/*******************建立測試數(shù)據(jù)***3w@live.cn***********************/IF NOT OBJECT_ID('[TBTest]') IS NULL    DROP TABLE [TBTest]GOCREATE 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--------gouse testdb2go/*******************定義表變量***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$]') /*******************第一種方法,用游標(biāo)***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            --修改結(jié)束            FETCH NEXT FROM DateDemo_cursor into @RYear,@BMonth,@EMonth,@RMoney        END    CLOSE DateDemo_cursor    DEALLOCATE DateDemo_cursorGOSELECT * FROM [TBTest]查詢結(jié)果:

/*tid    date    Money1    2001-09    94.402    2001-10    94.403    2001-11    94.404    2001-12    94.405    2008-05    88.806    2008-06    88.807    2008-07    88.808    2008-08    88.809    2008-09    88.8010    2008-10    88.8011    2008-11    88.8012    2008-12    88.8013    2010-08    90.4014    2010-09    90.4015    2010-10    90.4016    2010-11    90.4017    2010-12    90.40*/評價(jià):該方法使用了最傳統(tǒng)的方法,思路清晰。但沒有體現(xiàn)SQL server 2008的語法特性,略顯繁瑣。

思路二:可否使用CTE實(shí)現(xiàn)?(KillKill提供)


/*******************第二種方法,用CTE,適用于sql2005/2008/2008 r2*********//***************************************3w@live.cn***********************/ TRUNCATE table [TBTest]goDeclare @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 cfrom @TableVar t inner join seq on t.BMonth+seq.val <= EMonth;思路三:可否使用SQL Server 2008新提供的Merge實(shí)現(xiàn)?

思路四:使用NPOI在業(yè)務(wù)層實(shí)現(xiàn)數(shù)據(jù)轉(zhuǎn)換。

思路五:用Master..spt_values表實(shí)現(xiàn)(由小F提供)

利用該表,可獲取一定區(qū)間內(nèi)的列表,最長不超過2048,如

select number from master..spt_valueswhere type='P' andnumber between 1 and 5/*number12345*/因?yàn)樵路葑疃?2,不超過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 moneyfrom   master..spt_values a, @TableVar bwhere   number between BMonth and EMonthand   type='p'

思路六:使用SSIS實(shí)現(xiàn)

關(guān)鍵字:Excel、導(dǎo)入、SQL Server、數(shù)據(jù)

分享到:

頂部 】 【 關(guān)閉
版權(quán)所有:佛山思海電腦網(wǎng)絡(luò)有限公司 ©1998-2024 All Rights Reserved.
聯(lián)系電話:(0757)22630313、22633833
中華人民共和國增值電信業(yè)務(wù)經(jīng)營許可證: 粵B1.B2-20030321 備案號:粵B2-20030321-1
網(wǎng)站公安備案編號:44060602000007 交互式欄目專項(xiàng)備案編號:200303DD003  
察察 工商 網(wǎng)安 舉報(bào)有獎  警警  手機(jī)打開網(wǎng)站