国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > SQL Server > 正文

一個Excel導(dǎo)入SQL server的例子

2024-08-31 00:53:39
字體:
供稿:網(wǎng)友

有人提問如下:

這個是Excel的,比如是test.xls
欠費年份 欠費開始月份 欠費結(jié)束月份 應(yīng)繳金額(月租)  
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) //應(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 testdb2
go
/*******************建立測試數(shù)據(jù)***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$]')
 
/*******************第一種方法,用游標(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_cursor

GO
SELECT * FROM [TBTest]
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 拜泉县| 阿拉尔市| 缙云县| 怀仁县| 息烽县| 罗甸县| 文成县| 呼图壁县| 新泰市| 台东市| 大石桥市| 通城县| 商水县| 远安县| 车致| 南昌县| 宿州市| 侯马市| 西盟| 咸丰县| 紫阳县| 财经| 灌阳县| 临清市| 苏尼特右旗| 普兰县| 沛县| 阳春市| 贵德县| 弋阳县| 汕头市| 资阳市| 宣威市| 常州市| 宁夏| 体育| 江都市| 庆安县| 德阳市| 齐齐哈尔市| 城固县|