一、从excel表导入到SQLSERVER
use test
go
select * into test_table from
OpenRowSet('microsoft.jet.oledb.4.0','Excel 8.0;HDR=yes;database=c:\11.xls;','select * from [结果$]')
<pre class="reply-text mb10" id="content-657557854" name="code" style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0);">--1.通过Sql Server查询分析器查询D:\abc.xls 里面表 sheet1
SELECT * FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0','Data Source="D:\abc.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...[Sheet1$]
--2.如果表a存在,并且表a的结构与上一步的查询的结构一样
可以使用
insert into a SELECT * FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0','Data Source="D:\abc.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...[Sheet1$]
--3.如果表a不存在,使用into a
SELECT * into a FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0','Data Source="D:\abc.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...[Sheet1$] </pre>
说明: test_table为SQLSERVER数据库中的表,11.xls是EXCEL表, [结果符号。
2从SQLSERVER数据库中导出到EXCEL(一般用语句导库是在应用程序中,利用各种语言实现,如果不做开发项目,用DTS就很好了,在
SQLSERVER查询器中利用单条SQL语句实现导入导出不方便,类型转换是个问题,不好解决)
第一种,无法导入字段名:(excel表不存在,新建)
EXEC master..xp_cmdshell
'bcp zhouzhi.dbo.数据库表名 out "c:\test.xls" /c /S "服务器名" /U "用户名" -P "密码" '
第二种,excel文件已存在,要手工把表字段名填到excel文件中去,再执行下面:
insert into OPENROWSET('MICROSOFT.JET.OLEDB.4.0'
,'Excel 8.0;HDR=YES;DATABASE=c:\文件名.xls',sheet1$)
select * from 表名
第三种,想实现全自动转字段名和记录,一条语句实现不了,要用到过程,当然如果要做一个自动转换程序,用VB或者VC语言也可实现。
把下面的过程全拷入SQLSERVER查询分析器,执行,生成一个过程,过程名为:p_exporttb ,然后按应用例子执行过程就OK了,自
动导表
/*******************************/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_exporttb]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_exporttb]
GO
create proc p_exporttb
@sqlstr varchar(8000), --查询语句,如果查询语句中使用了order by ,请加上top 100 percent
@path nvarchar(1000), --文件存放目录
@fname nvarchar(250), --文件名
@sheetname varchar(250)='' --要创建的工作表名,默认为文件名
as
declare @err int,@src nvarchar(255),@desc nvarchar(255),@out int
declare @obj int,@constr nvarchar(1000),@sql varchar(8000),@fdlist varchar(8000)
--参数检测
if isnull(@fname,'')='' set @fname='temp.xls'
if isnull(@sheetname,'')='' set @sheetname=replace(@fname,'.','#')
--检查文件是否已经存在
if right(@path,1)<>'' set @path=@path+''
create table #tb(a bit,b bit,c bit)
set @sql=@path+@fname
insert into #tb exec master..xp_fileexist @sql
--数据库创建语句
set @sql=@path+@fname
if exists(select 1 from #tb where a=1)
set @constr='DRIVER={Microsoft Excel Driver (*.xls)};DSN='''';READONLY=FALSE'
+';CREATE_DB="'+@sql+'";DBQ='+@sql
else
set @constr='Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties="Excel 8.0;HDR=YES'
+';DATABASE='+@sql+'"'
--连接数据库
exec @err=sp_oacreate 'adodb.connection',@obj out
if @err<>0 goto lberr
exec @err=sp_oamethod @obj,'open',null,@constr
if @err<>0 goto lberr
--创建表的SQL
declare @tbname sysname
set @tbname='##tmp_'+convert(varchar(38),newid())
set @sql='select * into ['+@tbname+'] from('+@sqlstr+') a'
exec(@sql)
select @sql='',@fdlist=''
select @fdlist=@fdlist+',['+a.name+']'
,@sql=@sql+',['+a.name+'] '
+case
when b.name like '%char'
then case when a.length>255 then 'memo'
else 'text('+cast(a.length as varchar)+')' end
when b.name like '%int' or b.name='bit' then 'int'
when b.name like '%datetime' then 'datetime'
when b.name like '%money' then 'money'
when b.name like '%text' then 'memo'
else b.name end
FROM tempdb..syscolumns a left join tempdb..systypes b on a.xtype=b.xusertype
where b.name not in('image','uniqueidentifier','sql_variant','varbinary','binary','timestamp')
and a.id=(select id from tempdb..sysobjects where name=@tbname)
if @@rowcount=0 return
select @sql='create table '+@sheetname
+''
,@fdlist=substring(@fdlist,2,8000)
exec @err=sp_oamethod @obj,'execute',@out out,@sql
if @err<>0 goto lberr
exec @err=sp_oadestroy @obj
--导入数据
set @sql='openrowset(''MICROSOFT.JET.OLEDB.4.0'',''Excel 8.0;HDR=YES
;DATABASE='+@path+@fname+''',['+@sheetname+'$])'
exec('insert into '+@sql+'('+@fdlist+') select '+@fdlist+' from ['+@tbname+']')
set @sql='drop table ['+@tbname+']'
exec(@sql)
return
lberr:
exec sp_oageterrorinfo 0,@src out,@desc out
lbexit:
select cast(@err as varbinary(4)) as 错误号
,@src as 错误源,@desc as 错误描述
select @sql,@constr,@fdlist
go
/*******************应用例子***************************/
p_exporttb @sqlstr='select * from 贷款大户表'
,@path='c:',@fname='aa.xls',@sheetname='贷款大户表'