1、在SQL数据库中直接从Excel里面查询数据:

select \* from

OPENROWSET('MICROSOFT.JET.OLEDB.4.0'

,'Excel 5.0;HDR=YES;DATABASE=c:\\test.xls',sheet1$)

2、从Excel文件中,导入数据到SQL数据库中,

select \* into 表 from

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) OPENROWSET('MICROSOFT.JET.OLEDB.4.0'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) ,'Excel 5.0;HDR=YES;DATABASE=c:\\test.xls',sheet1$)

3、从SQL数据库中,导出数据到Excel(excel存在),

insert into OPENROWSET('MICROSOFT.JET.OLEDB.4.0'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) ,'Excel 5.0;HDR=YES;DATABASE=c:\\test.xls',sheet1$)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select \* from 表

4、从SQL数据库中,导出数据到Excel(excel不存在),

\---- 导出表

EXEC master..xp\_cmdshell 'bcp 数据库名.dbo.表名 out "c: est.xls" /c -/S"服务器名" /U"用户名" -P"密码"'

---- 导出查询语句

EXEC master..xp\_cmdshell 'bcp "SELECT au\_fname, au\_lname FROM pubs..authors ORDER BY au\_lname" queryout "c: est.xls" /c -/S"服务器名" /U"用户名" -P"密码"'

5、导入导出的存储过程

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)\--下面是导出真正Excel文件的方法:(请将一下所有代码复制到存储过程中)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)ifexists (select\*from dbo.sysobjects where id \=object\_id(N'\[dbo\].\[p\_exporttb\]') andOBJECTPROPERTY(id, N'IsProcedure') \=1)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)dropprocedure\[dbo\].\[p\_exporttb\]

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)GO

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)/\*\--数据导出EXCEL

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) 导出表中的数据到Excel,包含字段名,文件为真正的Excel文件

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) ,如果文件不存在,将自动创建文件

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) ,如果表不存在,将自动创建表

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) 基于通用性考虑,仅支持导出标准数据类型

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\--邹建 2003.10(引用请保留此信息)--\*/

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)/\*\--调用示例

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) p\_exporttb @tbname='地区资料',@path='c:',@fname='aa.xls'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\--\*/

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)createproc p\_exporttb

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)@tbname sysname,    \--要导出的表名

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)@pathnvarchar(1000),   \--文件存放目录

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)@fnamenvarchar(250)\=''\--文件名,默认为表名

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)as

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)declare@errint,@srcnvarchar(255),@descnvarchar(255),@outint

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)declare@objint,@constrnvarchar(1000),@sqlvarchar(8000),@fdlistvarchar(8000)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)\--参数检测

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)ifisnull(@fname,'')\=''set@fname\=@tbname+'.xls'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)\--检查文件是否已经存在

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)ifright(@path,1)<>''set@path\=@path+''

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)createtable #tb(a bit,b bit,c bit)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)set@sql\=@path+@fname

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)insertinto #tb exec master..xp\_fileexist @sql

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)\--数据库创建语句

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)set@sql\=@path+@fname

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)ifexists(select1from #tb where a\=1)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) set@constr\='DRIVER={Microsoft Excel Driver (\*.xls)};DSN='''';READONLY=FALSE'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)       +';CREATE\_DB="'+@sql+'";DBQ='+@sql

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)else

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) set@constr\='Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties="Excel 8.0;HDR=YES'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    +';DATABASE='+@sql+'"'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)\--连接数据库

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)exec@err\=sp\_oacreate 'adodb.connection',@obj out

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)if@err<>0goto lberr

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)exec@err\=sp\_oamethod @obj,'open',null,@constr

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)if@err<>0goto lberr

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)/\*\--如果覆盖已经存在的表,就加上下面的语句

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\--创建之前先删除表/如果存在的话

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)select @sql='drop table \['+@tbname+'\]'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)exec @err=sp\_oamethod @obj,'execute',@out out,@sql

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\--\*/

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)\--创建表的SQL

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)select@sql\='',@fdlist\=''

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)select@fdlist\=@fdlist+',\['+a.name+'\]'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) ,@sql\=@sql+',\['+a.name+'\] '

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  +case

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   when b.name like'%char'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   thencasewhen a.length\>255then'memo'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    else'text('+cast(a.length asvarchar)+')'end

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   when b.name like'%int'or b.name\='bit'then'int'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   when b.name like'%datetime'then'datetime'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   when b.name like'%money'then'money'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   when b.name like'%text'then'memo'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   else b.name end

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)FROM syscolumns a leftjoin systypes b on a.xtype\=b.xusertype

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)where b.name notin('image','uniqueidentifier','sql\_variant','varbinary','binary','timestamp')

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) andobject\_id(@tbname)\=id

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)select@sql\='create table \['+@tbname

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) +'\]('+substring(@sql,2,8000)+')'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) ,@fdlist\=substring(@fdlist,2,8000)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)exec@err\=sp\_oamethod @obj,'execute',@out out,@sql

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)if@err<>0goto lberr

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)exec@err\=sp\_oadestroy @obj

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)\--导入数据

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)set@sql\='openrowset(''MICROSOFT.JET.OLEDB.4.0'',''Excel 8.0;HDR=YES;IMEX=1

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   ;DATABASE='+@path+@fname+''',\['+@tbname+'$\])'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)exec('insert into '+@sql+'('+@fdlist+') select '+@fdlist+' from '+@tbname)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)return

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)lberr:

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) exec sp\_oageterrorinfo 0,@src out,@desc out

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)lbexit:

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) selectcast(@errasvarbinary(4)) as 错误号

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  ,@srcas 错误源,@descas 错误描述

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select@sql,@constr,@fdlist

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)go

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)ifexists (select\*from dbo.sysobjects where id \=object\_id(N'\[dbo\].\[p\_exporttb\]') andOBJECTPROPERTY(id, N'IsProcedure') \=1)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)dropprocedure\[dbo\].\[p\_exporttb\]

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)GO

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)/\*\--数据导出EXCEL

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) 导出查询中的数据到Excel,包含字段名,文件为真正的Excel文件

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) 如果文件不存在,将自动创建文件

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) 如果表不存在,将自动创建表

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) 基于通用性考虑,仅支持导出标准数据类型

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\--邹建 2003.10(引用请保留此信息)--\*/

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)/\*\--调用示例

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) p\_exporttb @sqlstr='select \* from 地区资料'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  ,@path='c:',@fname='aa.xls',@sheetname='地区资料'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\--\*/

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)createproc p\_exporttb

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)@sqlstrvarchar(8000),   \--查询语句,如果查询语句中使用了order by ,请加上top 100 percent

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)@pathnvarchar(1000),   \--文件存放目录

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)@fnamenvarchar(250),   \--文件名

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)@sheetnamevarchar(250)\=''\--要创建的工作表名,默认为文件名

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)as

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)declare@errint,@srcnvarchar(255),@descnvarchar(255),@outint

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)declare@objint,@constrnvarchar(1000),@sqlvarchar(8000),@fdlistvarchar(8000)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)\--参数检测

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)ifisnull(@fname,'')\=''set@fname\='temp.xls'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)ifisnull(@sheetname,'')\=''set@sheetname\=replace(@fname,'.','#')

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)\--检查文件是否已经存在

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)ifright(@path,1)<>''set@path\=@path+''

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)createtable #tb(a bit,b bit,c bit)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)set@sql\=@path+@fname

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)insertinto #tb exec master..xp\_fileexist @sql

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)\--数据库创建语句

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)set@sql\=@path+@fname

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)ifexists(select1from #tb where a\=1)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) set@constr\='DRIVER={Microsoft Excel Driver (\*.xls)};DSN='''';READONLY=FALSE'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)       +';CREATE\_DB="'+@sql+'";DBQ='+@sql

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)else

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) set@constr\='Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties="Excel 8.0;HDR=YES'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    +';DATABASE='+@sql+'"'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)\--连接数据库

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)exec@err\=sp\_oacreate 'adodb.connection',@obj out

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)if@err<>0goto lberr

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)exec@err\=sp\_oamethod @obj,'open',null,@constr

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)if@err<>0goto lberr

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)\--创建表的SQL

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)declare@tbname sysname

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)set@tbname\='##tmp\_'+convert(varchar(38),newid())

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)set@sql\='select \* into \['+@tbname+'\] from('+@sqlstr+') a'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)exec(@sql)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)select@sql\='',@fdlist\=''

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)select@fdlist\=@fdlist+',\['+a.name+'\]'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) ,@sql\=@sql+',\['+a.name+'\] '

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  +case

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   when b.name like'%char'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   thencasewhen a.length\>255then'memo'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    else'text('+cast(a.length asvarchar)+')'end

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   when b.name like'%int'or b.name\='bit'then'int'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   when b.name like'%datetime'then'datetime'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   when b.name like'%money'then'money'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   when b.name like'%text'then'memo'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   else b.name end

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)FROM tempdb..syscolumns a leftjoin tempdb..systypes b on a.xtype\=b.xusertype

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)where b.name notin('image','uniqueidentifier','sql\_variant','varbinary','binary','timestamp')

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) and a.id\=(select id from tempdb..sysobjects where name\=@tbname)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)if@@rowcount\=0return

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)select@sql\='create table \['+@sheetname

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) +'\]('+substring(@sql,2,8000)+')'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) ,@fdlist\=substring(@fdlist,2,8000)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)exec@err\=sp\_oamethod @obj,'execute',@out out,@sql

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)if@err<>0goto lberr

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)exec@err\=sp\_oadestroy @obj

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)\--导入数据

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)set@sql\='openrowset(''MICROSOFT.JET.OLEDB.4.0'',''Excel 8.0;HDR=YES

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   ;DATABASE='+@path+@fname+''',\['+@sheetname+'$\])'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)exec('insert into '+@sql+'('+@fdlist+') select '+@fdlist+' from \['+@tbname+'\]')

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)set@sql\='drop table \['+@tbname+'\]'

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)exec(@sql)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)return

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)lberr:

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) exec sp\_oageterrorinfo 0,@src out,@desc out

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)lbexit:

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) selectcast(@errasvarbinary(4)) as 错误号

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  ,@srcas 错误源,@descas 错误描述

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) select@sql,@constr,@fdlist

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)go

excel生成mysql语句_通过SQL语句直接实现Excel与数据库的导入导出相关推荐

  1. mysql原生sql语句_原生SQL语句

    -- -sql语句的注意 : 1 以;作为结束符 2不区分大小写--01mysql 数据库的操作--链接数据库 mysql-uroot -pmysql--不显示密码 mysql-uroot -p my ...

  2. mysql删除字段sql语句_用SQL语句添加删除修改字段

    用SQL语句添加删除修改字段 1.增加字段 alter table docdsp add dspcode char(200) 2.删除字段 ALTER TABLE table_NAME DROP CO ...

  3. mysql 查询数据库索引语句_利用SQL语句查询数据库中所有索引

    本章我们就要讲解一下如何利用sql语句来查询出数据库中所有索引明细.当然了,我们可以在microsoft sql server management studio中选择"表"- & ...

  4. c语言解析sql语句_解析SQL语句比解析类C语言更麻烦?

    最近想做一个SQL语句解析器,换句话说想给自己的系统加上类似SQL语句的查询引擎.我之前做过一个解析类似C语言语法的解析器,可以解析 C/C++里的运算表达式,if-else-等基本语句.我以为做个S ...

  5. 使用了无效的sql语句_使用SQL语句创建数据库

    使用SQL语句创建数据库(SQL Server 2008) 创建一个名为'DA_sales'的数据库. 主文件组'DA_sales_data'(初始大小:5MB;最大值:200MB;自动增长率:10% ...

  6. mysql经典sql语句大全_经典SQL语句大全

    下列语句部分是Mssql语句,不可以在access中使用. SQL分类: DDL-数据定义语言(CREATE,ALTER,DROP,DECLARE) DML-数据操纵语言(SELECT,DELETE, ...

  7. MySQL数据库https接口_第三章 mysql 数据库接口程序以及SQL语句操作

    mysql  数据库接口程序以及SQL语句操作 用于管理数据库: 命令接口自带命令 DDL:数据定义语言(create drop ) DCL: 数据控制语言(grant revoke) DML: 数据 ...

  8. mysql 语句_如何记录MySQL执行过的SQL语句

    很多时候,我们需要知道 MySQL 执行过哪些 SQL 语句,比如 MySQL 被注入后,需要知道造成什么伤害等等.只要有 SQL 语句的记录,就能知道情况并作出对策.服务器是可以开启 MySQL 的 ...

  9. mysql书写规则_每天10分钟带你学会MySQL(二)SQL语句的基本书写规则

    SQL语句时必须要遵守一些规则.这些规则都非常简单,接下来就让我们逐一认识一下吧. 1,SQL语句以分号(;)结尾. ■SQL语句要以分号(;)结 尾 一条SQL语句可以描述一个数据库操作.在RDBM ...

最新文章

  1. 输出主键的值 output inserted.id
  2. VC调用外部程序接口
  3. Eclipse中的插件安装
  4. 宝宝头三年至关重要,不看悔掉肠子
  5. js中的时间与毫秒数互相转换
  6. 【转】0.SharePoint服务器端对象模型 之 序言
  7. 如何评价个人在软件开发团队中的绩效
  8. 给员工授予svn相关权限
  9. NOI 2021 游记
  10. greensock下载_GreenSock面向初学者:Web动画教程(第1部分)
  11. 11210怎么等于24_小学生24点题目大全附答案
  12. php如何把汉字转换为拼音,php 把汉字转换为拼音 php 如何把拼音转换汉字
  13. 浏览器登录系统登录不进去的问题
  14. matlab 实现马赫带效应,图像上机实验.doc
  15. 2018年六级第三套自行车翻译
  16. switch删除用户显示无法连接服务器,switch无法连接互联网怎么办 NS无法联机联网详细解决办法...
  17. 【Python】random.randint()用法
  18. 华雨欢:昨日黄花在逐渐奋起的市场里夭折与否已没多少人关注
  19. ffmpeg中的ts/mp4封装格式支持哪些编码格式
  20. android 不把jra包编译到apk中的方法

热门文章

  1. MVC常见的控制器,接口,数据层之间的操作
  2. 我理解的invoke和begininvoke
  3. lighttpd,thttpd,shttpd - 轻量级WebServer介绍
  4. PMP读书笔记(第4章)
  5. 为什么BDLocationListener没有被调用
  6. cesium面板动态显示并跟随移动
  7. 微信支付 商户Key 支付Key API密钥 的获取
  8. 从底层重学 Java 之两大浮点类型 GitChat连接
  9. vs2010编写的net3.5用vs2008打开
  10. matlab 数字图像滤波,数字图像处理 (基于Matlab) 滤波