将某个目录上的Excel表,导入到数据库中.sql
生活随笔
收集整理的這篇文章主要介紹了
将某个目录上的Excel表,导入到数据库中.sql
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
--將某個目錄上的Excel表,導(dǎo)入到數(shù)據(jù)庫中
--將所有的Excel文件放到一個目錄中,假設(shè)為c:\test\,然后用下面的方法來做
create table #t(fname varchar(260),depth int,isf bit)
insert into #t exec master..xp_dirtree 'c:\test',1,1
declare tb cursor for select fn='c:\test'+fname from #t
?? ?where isf=1 and fname like '%.xls'? --取.xls文件(EXCEL)
declare @fn varchar(8000)
open tb
fetch next from tb into @fn
while @@fetch_status=0
begin
?? ?--下面是查詢語句,需要根據(jù)你的情況改為插入語句
??? --插入已有的表用:insert into 表 selct * from ...
??? --創(chuàng)建表用:select * into 表 from ...
?? ?set @fn='select * from
?? ?OPENROWSET(''MICROSOFT.JET.OLEDB.4.0'',''Excel 5.0;HDR=YES;DATABASE='+@fn+''',全部客戶$)'
?? ?exec(@fn)
?? ?fetch next from tb into @fn
end
close tb
deallocate tb
drop table #t
--將所有的Excel文件放到一個目錄中,假設(shè)為c:\test\,然后用下面的方法來做
create table #t(fname varchar(260),depth int,isf bit)
insert into #t exec master..xp_dirtree 'c:\test',1,1
declare tb cursor for select fn='c:\test'+fname from #t
?? ?where isf=1 and fname like '%.xls'? --取.xls文件(EXCEL)
declare @fn varchar(8000)
open tb
fetch next from tb into @fn
while @@fetch_status=0
begin
?? ?--下面是查詢語句,需要根據(jù)你的情況改為插入語句
??? --插入已有的表用:insert into 表 selct * from ...
??? --創(chuàng)建表用:select * into 表 from ...
?? ?set @fn='select * from
?? ?OPENROWSET(''MICROSOFT.JET.OLEDB.4.0'',''Excel 5.0;HDR=YES;DATABASE='+@fn+''',全部客戶$)'
?? ?exec(@fn)
?? ?fetch next from tb into @fn
end
close tb
deallocate tb
drop table #t
總結(jié)
以上是生活随笔為你收集整理的将某个目录上的Excel表,导入到数据库中.sql的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ext2、Ext3和Ext4之间的区别
- 下一篇: CCNA-(9)-思科交换机特点