/*******  導(dǎo)出到excel 
exec master..xp_cmdshell ’bcp settledb.dbo.shanghu out c:/temp1.xls -c -q -s"gnetdata/gnetdata" -u"sa" -p""’ 
/***********  導(dǎo)入excel 
select * 
from opendatasource( ’microsoft.jet.oledb.4.0’, 
  ’data source="c:/test.xls";user id=admin;password=;extended properties=excel 5.0’)...xactions 
select cast(cast(科目編號(hào) as numeric(10,2)) as nvarchar(255))+’ ’ 轉(zhuǎn)換后的別名 
from opendatasource( ’microsoft.jet.oledb.4.0’, 
  ’data source="c:/test.xls";user id=admin;password=;extended properties=excel 5.0’)...xactions 
/** 導(dǎo)入文本文件
exec master..xp_cmdshell ’bcp "dbname..tablename" in c:/dt.txt -c -sservername -usa -ppassword’ 
/** 導(dǎo)出文本文件
exec master..xp_cmdshell ’bcp "dbname..tablename" out c:/dt.txt -c -sservername -usa -ppassword’
或 
exec master..xp_cmdshell ’bcp "select * from dbname..tablename" queryout c:/dt.txt -c -sservername -usa -ppassword’ 
導(dǎo)出到txt文本,用逗號(hào)分開(kāi)
exec master..xp_cmdshell ’bcp "庫(kù)名..表名" out "d:/tt.txt" -c -t ,-u sa -p password’
bulk insert 庫(kù)名..表名 
from ’c:/test.txt’ 
with ( 
    fieldterminator = ’;’, 
    rowterminator = ’/n’ 
) 
--/* dbase iv文件 
select * from 
openrowset(’microsoft.jet.oledb.4.0’ 
,’dbase iv;hdr=no;imex=2;database=c:/’,’select * from [客戶資料4.dbf]’) 
--*/ 
--/* dbase iii文件 
select * from 
openrowset(’microsoft.jet.oledb.4.0’ 
,’dbase iii;hdr=no;imex=2;database=c:/’,’select * from [客戶資料3.dbf]’) 
--*/ 
--/* foxpro 數(shù)據(jù)庫(kù) 
select * from openrowset(’msdasql’, 
’driver=microsoft visual foxpro driver;sourcetype=dbf;sourcedb=c:/’, 
’select * from [aa.dbf]’) 
--*/ 
/**************導(dǎo)入dbf文件****************/ 
select * from openrowset(’msdasql’, 
’driver=microsoft visual foxpro driver; 
sourcedb=e:/vfp98/data; 
sourcetype=dbf’, 
’select * from customer where country != "usa" order by country’) 
go 
/***************** 導(dǎo)出到dbf ***************/ 
如果要導(dǎo)出數(shù)據(jù)到已經(jīng)生成結(jié)構(gòu)(即現(xiàn)存的)foxpro表中,可以直接用下面的sql語(yǔ)句 
insert into openrowset(’msdasql’, 
’driver=microsoft visual foxpro driver;sourcetype=dbf;sourcedb=c:/’, 
’select * from [aa.dbf]’) 
select * from 表 
說(shuō)明: 
sourcedb=c:/  指定foxpro表所在的文件夾 
aa.dbf        指定foxpro表的文件名. 
/*************導(dǎo)出到access********************/ 
insert into openrowset(’microsoft.jet.oledb.4.0’, 
   ’x:/a.mdb’;’admin’;’’,a表) select * from 數(shù)據(jù)庫(kù)名..b表 
/*************導(dǎo)入access********************/ 
insert into b表 selet * from openrowset(’microsoft.jet.oledb.4.0’, 
   ’x:/a.mdb’;’admin’;’’,a表) 
*********************  導(dǎo)入 xml 文件 
declare @idoc int 
declare @doc varchar(1000) 
--sample xml document 
set @doc =’ 
<root> 
  <customer cid= "c1" name="janine" city="issaquah"> 
      <order oid="o1" date="1/20/1996" amount="3.5" /> 
      <order oid="o2" date="4/30/1997" amount="13.4">customer was very satisfied 
      </order> 
   </customer> 
   <customer cid="c2" name="ursula" city="oelde" > 
      <order oid="o3" date="7/14/1999" amount="100" note="wrap it blue 
             white red"> 
            <urgency>important</urgency> 
            happy customer. 
      </order> 
      <order oid="o4" date="1/20/1996" amount="10000"/> 
   </customer> 
</root> 
’ 
-- create an internal representation of the xml document. 
exec sp_xml_preparedocument @idoc output, @doc 
-- execute a select statement using openxml rowset provider. 
select * 
from openxml (@idoc, ’/root/customer/order’, 1) 
      with (oid     char(5), 
            amount  float, 
            comment ntext ’text()’) 
exec sp_xml_removedocument @idoc 
/********************導(dǎo)整個(gè)數(shù)據(jù)庫(kù)*********************************************/ 
用bcp實(shí)現(xiàn)的存儲(chǔ)過(guò)程 
/* 
 實(shí)現(xiàn)數(shù)據(jù)導(dǎo)入/導(dǎo)出的存儲(chǔ)過(guò)程 
         根據(jù)不同的參數(shù),可以實(shí)現(xiàn)導(dǎo)入/導(dǎo)出整個(gè)數(shù)據(jù)庫(kù)/單個(gè)表 
 調(diào)用示例: 
--導(dǎo)出調(diào)用示例 
----導(dǎo)出單個(gè)表 
exec file2table ’zj’,’’,’’,’xzkh_sa..地區(qū)資料’,’c:/zj.txt’,1 
----導(dǎo)出整個(gè)數(shù)據(jù)庫(kù) 
exec file2table ’zj’,’’,’’,’xzkh_sa’,’c:/docman’,1 
--導(dǎo)入調(diào)用示例 
----導(dǎo)入單個(gè)表 
exec file2table ’zj’,’’,’’,’xzkh_sa..地區(qū)資料’,’c:/zj.txt’,0 
----導(dǎo)入整個(gè)數(shù)據(jù)庫(kù) 
exec file2table ’zj’,’’,’’,’xzkh_sa’,’c:/docman’,0 
*/ 
if exists(select 1 from sysobjects where name=’file2table’ and objectproperty(id,’isprocedure’)=1) 
 drop procedure file2table 
go 
create procedure file2table 
@servername varchar(200)  --服務(wù)器名 
,@username varchar(200)   --用戶名,如果用nt驗(yàn)證方式,則為空’’ 
,@password varchar(200)   --密碼 
,@tbname varchar(500)   --數(shù)據(jù)庫(kù).dbo.表名,如果不指定:.dbo.表名,則導(dǎo)出數(shù)據(jù)庫(kù)的所有用戶表 
,@filename varchar(1000)  --導(dǎo)入/導(dǎo)出路徑/文件名,如果@tbname參數(shù)指明是導(dǎo)出整個(gè)數(shù)據(jù)庫(kù),則這個(gè)參數(shù)是文件存放路徑,文件名自動(dòng)用表名.txt 
,@isout bit      --1為導(dǎo)出,0為導(dǎo)入 
as 
declare @sql varchar(8000) 
if @tbname like ’%.%.%’ --如果指定了表名,則直接導(dǎo)出單個(gè)表 
begin 
 set @sql=’bcp ’[email protected] 
  +case when @isout=1 then ’ out ’ else ’ in ’ end 
  +’ "’[email protected]+’" /w’ 
  +’ /s ’[email protected] 
  +case when isnull(@username,’’)=’’ then ’’ else ’ /u ’[email protected] end 
  +’ /p ’+isnull(@password,’’) 
 exec master..xp_cmdshell @sql 
end 
else 
begin --導(dǎo)出整個(gè)數(shù)據(jù)庫(kù),定義游標(biāo),取出所有的用戶表 
 declare @m_tbname varchar(250) 
 if right(@filename,1)<>’/’ set @[email protected]+’/’ 
 set @m_tbname=’declare #tb cursor for select name from ’[email protected]+’..sysobjects where xtype=’’u’’’ 
 exec(@m_tbname) 
 open #tb 
 fetch next from #tb into @m_tbname 
 while @@fetch_status=0 
 begin 
  set @sql=’bcp ’[email protected]+’..’[email protected]_tbname 
   +case when @isout=1 then ’ out ’ else ’ in ’ end 
   +’ "’[email protected][email protected]_tbname+’.txt " /w’ 
   +’ /s ’[email protected] 
   +case when isnull(@username,’’)=’’ then ’’ else ’ /u ’[email protected] end 
   +’ /p ’+isnull(@password,’’) 
  exec master..xp_cmdshell @sql 
  fetch next from #tb into @m_tbname 
 end 
 close #tb 
 deallocate #tb  
end 
go 
/**********************excel導(dǎo)到txt****************************************/ 
想用 
select * into opendatasource(...) from opendatasource(...) 
實(shí)現(xiàn)將一個(gè)excel文件內(nèi)容導(dǎo)入到一個(gè)文本文件 
假設(shè)excel中有兩列,第一列為姓名,第二列為很行帳號(hào)(16位) 
且銀行帳號(hào)導(dǎo)出到文本文件后分兩部分,前8位和后8位分開(kāi)。 
如果要用你上面的語(yǔ)句插入的話,文本文件必須存在,而且有一行:姓名,銀行賬號(hào)1,銀行賬號(hào)2 
然后就可以用下面的語(yǔ)句進(jìn)行插入 
注意文件名和目錄根據(jù)你的實(shí)際情況進(jìn)行修改. 
 
insert into 
opendatasource(’microsoft.jet.oledb.4.0’ 
,’text;hdr=yes;database=c:/’ 
)...[aa#txt] 
--,aa#txt) 
--*/ 
select 姓名,銀行賬號(hào)1=left(銀行賬號(hào),8),銀行賬號(hào)2=right(銀行賬號(hào),8) 
from 
opendatasource(’microsoft.jet.oledb.4.0’ 
,’excel 5.0;hdr=yes;imex=2;database=c:/a.xls’ 
--,sheet1$) 
)...[sheet1$] 
如果你想直接插入并生成文本文件,就要用bcp 
declare @sql varchar(8000),@tbname varchar(50) 
--首先將excel表內(nèi)容導(dǎo)入到一個(gè)全局臨時(shí)表 
select @tbname=’[##temp’+cast(newid() as varchar(40))+’]’ 
 ,@sql=’select 姓名,銀行賬號(hào)1=left(銀行賬號(hào),8),銀行賬號(hào)2=right(銀行賬號(hào),8) 
into ’[email protected]+’ from 
opendatasource(’’microsoft.jet.oledb.4.0’’ 
,’’excel 5.0;hdr=yes;imex=2;database=c:/a.xls’’ 
)...[sheet1$]’ 
exec(@sql) 
--然后用bcp從全局臨時(shí)表導(dǎo)出到文本文件 
set @sql=’bcp "’[email protected]+’" out "c:/aa.txt" /s"(local)" /p"" /c’ 
exec master..xp_cmdshell @sql 
--刪除臨時(shí)表 
exec(’drop table ’[email protected]) 
用bcp將文件導(dǎo)入導(dǎo)出到數(shù)據(jù)庫(kù)的存儲(chǔ)過(guò)程: 
/*--bcp-二進(jìn)制文件的導(dǎo)入導(dǎo)出 
 支持image,text,ntext字段的導(dǎo)入/導(dǎo)出 
 image適合于二進(jìn)制文件;text,ntext適合于文本數(shù)據(jù)文件 
 注意:導(dǎo)入時(shí),將覆蓋滿足條件的所有行 
  導(dǎo)出時(shí),將把所有滿足條件的行也出到指定文件中 
 此存儲(chǔ)過(guò)程僅用bcp實(shí)現(xiàn) 
鄒建 2003.08-----------------*/ 
/*--調(diào)用示例 
--數(shù)據(jù)導(dǎo)出 
 exec p_binaryio ’zj’,’’,’’,’acc_演示數(shù)據(jù)..tb’,’img’,’c:/zj1.dat’ 
--數(shù)據(jù)導(dǎo)出 
 exec p_binaryio ’zj’,’’,’’,’acc_演示數(shù)據(jù)..tb’,’img’,’c:/zj1.dat’,’’,0 
--*/ 
if exists (select * from dbo.sysobjects where id = object_id(n’[dbo].[p_binaryio]’) and objectproperty(id, n’isprocedure’) = 1) 
drop procedure [dbo].[p_binaryio] 
go 
create proc p_binaryio 
@servename varchar (30),--服務(wù)器名稱 
@username varchar (30), --用戶名 
@password varchar (30), --密碼 
@tbname varchar (500),  --數(shù)據(jù)庫(kù)..表名 
@fdname varchar (30),  --字段名 
@fname varchar (1000), --目錄+文件名,處理過(guò)程中要使用/覆蓋:@filename+.bak 
@tj varchar (1000)=’’,  --處理?xiàng)l件.對(duì)于數(shù)據(jù)導(dǎo)入,如果條件中包含@fdname,請(qǐng)指定表名前綴 
@isout bit=1   --1導(dǎo)出((默認(rèn)),0導(dǎo)入 
as 
declare @fname_in varchar(1000) --bcp處理應(yīng)答文件名 
 ,@fsize varchar(20)   --要處理的文件的大小 
 ,@m_tbname varchar(50)  --臨時(shí)表名 
 ,@sql varchar(8000) 
--則取得導(dǎo)入文件的大小 
if @isout=1 
 set @fsize=’0’ 
else 
begin 
 create table #tb(可選名 varchar(20),大小 int 
  ,創(chuàng)建日期 varchar(10),創(chuàng)建時(shí)間 varchar(20) 
  ,上次寫操作日期 varchar(10),上次寫操作時(shí)間 varchar(20) 
  ,上次訪問(wèn)日期 varchar(10),上次訪問(wèn)時(shí)間 varchar(20),特性 int) 
 insert into #tb 
 exec master..xp_getfiledetails @fname 
 select @fsize=大小 from #tb 
 drop table #tb 
 if @fsize is null 
 begin 
  print ’文件未找到’ 
  return 
 end 
end 
--生成數(shù)據(jù)處理應(yīng)答文件 
set @m_tbname=’[##temp’+cast(newid() as varchar(40))+’]’ 
set @sql=’select * into ’[email protected]_tbname+’ from( 
 select null as 類型 
 union all select 0 as 前綴 
 union all select ’[email protected]+’ as 長(zhǎng)度 
 union all select null as 結(jié)束 
 union all select null as 格式 
 ) a’ 
exec(@sql) 
select @[email protected]+’_temp’ 
 ,@sql=’bcp "’[email protected]_tbname+’" out "’[email protected]_in 
 +’" /s"’[email protected] 
 +case when isnull(@username,’’)=’’ then ’’ 
  else ’" /u"’[email protected] end 
 +’" /p"’+isnull(@password,’’)+’" /c’ 
exec master..xp_cmdshell @sql 
--刪除臨時(shí)表 
set @sql=’drop table ’[email protected]_tbname 
exec(@sql) 
if @isout=1 
begin 
 set @sql=’bcp "select top 1 ’[email protected]+’ from ’ 
  [email protected]+case isnull(@tj,’’) when ’’ then ’’ 
   else ’ where ’[email protected] end 
  +’" queryout "’[email protected] 
  +’" /s"’[email protected] 
  +case when isnull(@username,’’)=’’ then ’’ 
   else ’" /u"’[email protected] end 
  +’" /p"’+isnull(@password,’’) 
  +’" /i"’[email protected]_in+’"’ 
 exec master..xp_cmdshell @sql 
end 
else 
begin 
 --為數(shù)據(jù)導(dǎo)入準(zhǔn)備臨時(shí)表 
 set @sql=’select top 0 ’[email protected]+’ into ’ 
  [email protected]_tbname+’ from ’ [email protected] 
 exec(@sql) 
--將數(shù)據(jù)導(dǎo)入到臨時(shí)表 
 set @sql=’bcp "’[email protected]_tbname+’" in "’[email protected] 
  +’" /s"’[email protected] 
  +case when isnull(@username,’’)=’’ then ’’ 
   else ’" /u"’[email protected] end 
  +’" /p"’+isnull(@password,’’) 
  +’" /i"’[email protected]_in+’"’ 
 exec master..xp_cmdshell @sql 
  
 --將數(shù)據(jù)導(dǎo)入到正式表中 
 set @sql=’update ’[email protected] 
  +’ set ’[email protected]+’=b.’[email protected] 
  +’ from ’[email protected]+’ a,’ 
  [email protected]_tbname+’ b’ 
  +case isnull(@tj,’’) when ’’ then ’’ 
   else ’ where ’[email protected] end 
 exec(@sql) 
 --刪除數(shù)據(jù)處理臨時(shí)表 
 set @sql=’drop table ’[email protected]_tbname 
end 
--刪除數(shù)據(jù)處理應(yīng)答文件 
set @sql=’del ’[email protected]_in 
exec master..xp_cmdshell @sql 
go 
/** 導(dǎo)入文本文件 
exec master..xp_cmdshell ’bcp "dbname..tablename" in c:/dt.txt -c -sservername -usa -ppassword’ 
改為如下,不需引號(hào) 
exec master..xp_cmdshell ’bcp dbname..tablename in c:/dt.txt -c -sservername -usa -ppassword’ 
/** 導(dǎo)出文本文件 
exec master..xp_cmdshell ’bcp "dbname..tablename" out c:/dt.txt -c -sservername -usa -ppassword’ 
此句需加引號(hào)