SQL語句導(dǎo)入導(dǎo)出大全
發(fā)表日期:2009-12-1 |
-
/******* 導(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(科目編號 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文本,用逗號分開
exec master..xp_cmdshell ’bcp "庫名..表名" out "d:\tt.txt" -c -t ,-u sa -p password’
bulk insert 庫名..表名
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ù)庫
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語句
insert into openrowset(’msdasql’,
’driver=microsoft visual foxpro driver;sourcetype=dbf;sourcedb=c:\’,
’select * from [aa.dbf]’)
select * from 表
說明:
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ù)庫名..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)整個數(shù)據(jù)庫*********************************************/
用bcp實現(xiàn)的存儲過程
/*
實現(xiàn)數(shù)據(jù)導(dǎo)入/導(dǎo)出的存儲過程
根據(jù)不同的參數(shù),可以實現(xiàn)導(dǎo)入/導(dǎo)出整個數(shù)據(jù)庫/單個表
調(diào)用示例:
--導(dǎo)出調(diào)用示例
----導(dǎo)出單個表
exec file2table ’zj’,’’,’’,’xzkh_sa..地區(qū)資料’,’c:\zj.txt’,1
----導(dǎo)出整個數(shù)據(jù)庫
exec file2table ’zj’,’’,’’,’xzkh_sa’,’c:\docman’,1
--導(dǎo)入調(diào)用示例
----導(dǎo)入單個表
exec file2table ’zj’,’’,’’,’xzkh_sa..地區(qū)資料’,’c:\zj.txt’,0
----導(dǎo)入整個數(shù)據(jù)庫
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驗證方式,則為空’’
,@password varchar(200) --密碼
,@tbname varchar(500) --數(shù)據(jù)庫.dbo.表名,如果不指定:.dbo.表名,則導(dǎo)出數(shù)據(jù)庫的所有用戶表
,@filename varchar(1000) --導(dǎo)入/導(dǎo)出路徑/文件名,如果@tbname參數(shù)指明是導(dǎo)出整個數(shù)據(jù)庫,則這個參數(shù)是文件存放路徑,文件名自動用表名.txt
,@isout bit --1為導(dǎo)出,0為導(dǎo)入
as
declare @sql varchar(8000)
if @tbname like ’%.%.%’ --如果指定了表名,則直接導(dǎo)出單個表
begin
set @sql=’bcp
’+@tbname +case when @isout=1 then ’ out ’ else ’ in ’ end
+’ "
+’ /s ’+@servername +case when isnull(@username,’’)=’’ then ’’ else ’ /u
’+@username end
+’ /p ’+isnull(@password,’’)
exec master..xp_cmdshell @sql
end
else
begin --導(dǎo)出整個數(shù)據(jù)庫,定義游標(biāo),取出所有的用戶表
declare @m_tbname varchar(250)
if right(@filename,1)<>’\’ set @filename=@filename+’\’
set @m_tbname=’declare #tb cursor for select name from
’+@tbname+’..sysobjects where xtype=’’u’’’
exec(@m_tbname)
open #tb
fetch next from #tb into @m_tbname
while @@fetch_status=0
begin
set @sql=’bcp
’+@tbname+’..’+@m_tbname +case when @isout=1 then ’ out ’ else ’ in ’ end
+’ "
+’ /s ’+@servername +case when isnull(@username,’’)=’’ then ’’ else ’ /u
’+@username 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(...)
實現(xiàn)將一個excel文件內(nèi)容導(dǎo)入到一個文本文件
假設(shè)excel中有兩列,第一列為姓名,第二列為很行賬號(16位)
且銀行賬號導(dǎo)出到文本文件后分兩部分,前8位和后8位分開。
如果要用你上面的語句插入的話,文本文件必須存在,而且有一行:姓名,銀行賬號1,銀行賬號2
然后就可以用下面的語句進(jìn)行插入
注意文件名和目錄根據(jù)你的實際情況進(jìn)行修改.
insert into
opendatasource(’microsoft.jet.oledb.4.0’
,’text;hdr=yes;database=c:\’
)...[aa#txt]
--,aa#txt)
--*/
select 姓名,銀行賬號1=left(銀行賬號,8),銀行賬號2=right(銀行賬號,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)入到一個全局臨時表
select @tbname=’[##temp’+cast(newid() as varchar(40))+’]’
,@sql=’select 姓名,銀行賬號1=left(銀行賬號,8),銀行賬號2=right(銀行賬號,8)
into
’+@tbname+’ from
opendatasource(’’microsoft.jet.oledb.4.0’’
,’’excel 5.0;hdr=yes;imex=2;database=c:\a.xls’’
)...[sheet1$]’
exec(@sql)
用bcp將文件導(dǎo)入導(dǎo)出到數(shù)據(jù)庫的存儲過程:
/*--bcp-二進(jìn)制文件的導(dǎo)入導(dǎo)出
支持image,text,ntext字段的導(dǎo)入/導(dǎo)出
image適合于二進(jìn)制文件;text,ntext適合于文本數(shù)據(jù)文件
注意:導(dǎo)入時,將覆蓋滿足條件的所有行
導(dǎo)出時,將把所有滿足條件的行也出到指定文件中
此存儲過程僅用bcp實現(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ù)庫..表名
@fdname varchar (30), --字段名
@fname varchar (1000), --目錄+文件名,處理過程中要使用/覆蓋:@filename+.bak
@tj varchar (1000)=’’, --處理條件.對于數(shù)據(jù)導(dǎo)入,如果條件中包含@fdname,請指定表名前綴
@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) --臨時表名
,@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)建時間 varchar(20)
,上次寫操作日期 varchar(10),上次寫操作時間 varchar(20)
,上次訪問日期 varchar(10),上次訪問時間 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
’+@m_tbname+’ from(
select null as 類型
union all select 0 as 前綴
union all select
’+@fsize+’ as 長度
union all select null as 結(jié)束
union all select null as 格式
) a’
exec(@sql)
select @fname_in=@fname+’_temp’
,@sql=’bcp "
’+@fname_in +’" /s"
’+@servename +case when isnull(@username,’’)=’’ then ’’
else ’" /u"
’+@username end
+’" /p"’+isnull(@password,’’)+’" /c’
exec master..xp_cmdshell @sql
--刪除臨時表
set @sql=’drop table
’+@m_tbname exec(@sql)
--刪除數(shù)據(jù)處理臨時表
set @sql=’drop table
’+@m_tbname end
--刪除數(shù)據(jù)處理應(yīng)答文件
set @sql=’del
’+@fname_in exec master..xp_cmdshell @sql
go
-
關(guān)注此文的讀者還看過:
·2010-1-15 17:25:20 Sql連接查詢
·2010-1-15 17:24:47 存儲過程入門與提高
·2010-1-15 17:24:00 SQL游標(biāo)原理和使用方法
資料引用:http://www.knowsky.com/540372.html