經過SQL語句直接實現Excel與數據庫的導入導出

一、在SQL數據庫中直接從Excel裏面查詢數據:sql

select \* from  
    OPENROWSET('MICROSOFT.JET.OLEDB.4.0'  
    ,'Excel 5.0;HDR=YES;DATABASE=c:\\test.xls',sheet1$)

二、從Excel文件中,導入數據到SQL數據庫中,
  shell

 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$)

三、從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 表

四、從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"密碼"'

五、導入導出的存儲過程ide

![](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
相關文章
相關標籤/搜索