1. 程式人生 > >SQL語句匯入匯出大全

SQL語句匯入匯出大全


/*********** 匯入excel
select *
from opendatasource( microsoft.jet.oledb.4.0,
data source="c:/test.xls";user id=admin;password=;extended properties=excel 5.0)...xactions


/*動態檔名
declare @fn varchar(20),@s varchar(1000)
set @fn = c:/test.xls
set @s =microsoft.jet.oledb.4.0,
data source="[email protected]+";user id=admin;password=;extended properties=excel 5.0
set @s = select * from opendatasource (

[email protected]+)...sheet1$
exec(@s)
*/


select cast(cast(科目編號 as numeric(10,2)) as nvarchar(255))+  轉換後的別名
from opendatasource( microsoft.jet.oledb.4.0,
data source="c:/test.xls";user id=admin;password=;extended properties=excel 5.0)...xactions


/********************** excel導到遠端sql
insert opendatasource(
sqloledb,
data source=遠端ip;user id=sa;password=密碼
).庫名.dbo.表名 (列名1,列名2)
select 列名1,列名2
from opendatasource( microsoft.jet.oledb.4.0,
data source="c:/test.xls";user id=admin;password=;extended properties=excel 5.0)...xactions



/** 匯入文字檔案
exec master..xp_cmdshell bcp dbname..tablename in c:/dt.txt -c -sservername -usa -ppassword


/** 匯出文字檔案
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


匯出到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 資料庫
select * from openrowset(msdasql,
driver=microsoft visual foxpro driver;sourcetype=dbf;sourcedb=c:/,
select * from [aa.dbf])
--*/


/**************匯入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
/***************** 匯出到dbf ***************/
如果要匯出資料到已經生成結構(即現存的)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表的檔名.



/*************匯出到access********************/
insert into openrowset(microsoft.jet.oledb.4.0,
x:/a.mdb;admin;,a表) select * from 資料庫名..b表


/*************匯入access********************/
insert into b表 selet * from openrowset(microsoft.jet.oledb.4.0,
x:/a.mdb;admin;,a表)


檔名為引數
declare @fname varchar(20)
set @fname = d:/test.mdb
exec(select a.* from opendatasource(microsoft.jet.oledb.4.0,
[email protected]+;admin;, topics) as a )


select *
from opendatasource( microsoft.jet.oledb.4.0,
data source="f:/northwind.mdb";jet oledb:database password=123;user id=admin;password=;)...產品


********************* 匯入 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


/**********************excel導到txt****************************************/
想用
select * into opendatasource(...) from opendatasource(...)
實現將一個excel檔案內容匯入到一個文字檔案


假設excel中有兩列,第一列為姓名,第二列為很行帳號(16位)
且銀行帳號匯出到文字檔案後分兩部分,前8位和後8位分開。



鄒健:
如果要用你上面的語句插入的話,文字檔案必須存在,而且有一行:姓名,銀行賬號1,銀行賬號2
然後就可以用下面的語句進行插入
注意檔名和目錄根據你的實際情況進行修改.


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表內容匯入到一個全域性臨時表
select @tbname=[##temp+cast(newid() as varchar(40))+]
,@sql=select 姓名,銀行賬號1=left(銀行賬號,8),銀行賬號2=right(銀行賬號,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從全域性臨時表匯出到文字檔案
set @sql=bcp "[email protected]+" out "c:/aa.txt" /s"(local)" /p"" /c
exec master..xp_cmdshell @sql


--刪除臨時表
exec(drop table [email protected])



/********************導整個資料庫*********************************************/


用bcp實現的儲存過程



/*
實現資料匯入/匯出的儲存過程
根據不同的引數,可以實現匯入/匯出整個資料庫/單個表
呼叫示例:
--匯出呼叫示例
----匯出單個表
exec file2table zj,,,xzkh_sa..地區資料,c:/zj.txt,1
----匯出整個資料庫
exec file2table zj,,,xzkh_sa,c:/docman,1


--匯入呼叫示例
----匯入單個表
exec file2table zj,,,xzkh_sa..地區資料,c:/zj.txt,0
----匯入整個資料庫
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) --伺服器名
,@username varchar(200) --使用者名稱,如果用nt驗證方式,則為空
,@password varchar(200) --密碼
,@tbname varchar(500) --資料庫.dbo.表名,如果不指定:.dbo.表名,則匯出資料庫的所有使用者表
,@filename varchar(1000) --匯入/匯出路徑/檔名,如果@tbname引數指明是匯出整個資料庫,則這個引數是檔案存放路徑,檔名自動用表名.txt
,@isout bit --1為匯出,0為匯入
as
declare @sql varchar(8000)


if @tbname like %.%.% --如果指定了表名,則直接匯出單個表
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 --匯出整個資料庫,定義遊標,取出所有的使用者表
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



/************* oracle **************/
exec sp_addlinkedserver oraclesvr,
oracle 7.3,
msdaora,
orcldb
go


delete from openquery(mailser,select * from yulin)


select * from openquery(mailser,select * from yulin)


update openquery(mailser,select * from yulin where id=15)set disorder=555,catago=888


insert into openquery(mailser,select disorder,catago from yulin)values(333,777)


補充:


對於用bcp匯出,是沒有欄位名的.


用openrowset匯出,需要事先建好表.


用openrowset匯入,除access及excel外,均不支援非本機資料匯入 

相關推薦

SQL語句匯入匯出大全

/*********** 匯入excelselect * from opendatasource( microsoft.jet.oledb.4.0,data source="c:/test.xls";user id=admin;password=;extended properties=excel 5.

SQL語句故關鍵詞大全

目錄   SQL語句的整理以及總結 目錄 SQL語句的基本型別SQL語句的基本型別 特殊關鍵字特殊關鍵字 建立/插入/刪除/修改建立/插入/刪除/修改 SQL查詢語句−單表查詢SQL查詢語句−單表查詢 SQL

MS SQL 2008 匯入匯出 提示 未在本地計算機上註冊"Microsoft.ACE.Oledb.12.0"提供程

安裝Microsoft Office Access Database Engine 2007 需要下載安裝:Microsoft Office Access Database Engine 2007 http://download.microsoft.com/download/7/0

linux 資料庫操作,新增資料庫,刪除資料庫,sql檔案匯入匯出

匯入sql檔案需要先將sql上傳至伺服器,故連線上xshell後,cd進入到根目錄,再cd進入www(目錄隨意,只要記得自己的sql檔案是匯入到哪個資料夾就可以了) 執行 rz 選擇本地資料庫檔案(rz 是安裝的上傳工具包,具體請移步另外一篇部落格可檢視安裝使用命令),

SQL server2014匯入匯出Excel詳解

1.找到需要匯出的資料庫,進行如下操作: 2.選擇資料來源、Excel檔案路徑、Excel版本 注意:第一次使用時選擇Excel進行下一步時會報錯 需要下載安裝:Microsoft Office Access Database Engine 2007

sql server匯入匯出功能 把excel匯入sql的表中,提示匯入成功,但是資料庫表中並沒有資料?

這是典型的操作錯誤問題 在匯入資料的最後一步之前“選擇源表和檢視”步驟中會出現兩個表,這裡假設你是從“ab”表中匯出的資料,那麼這裡會出現“ab”和“ab$”兩個選項,如果選擇上面的選項,則匯入資料肯定為空,應該選擇下面的那個選項,然後將“目的”中表名最後的“$”去掉,然後

sql語句合集大全(個人總結)

--查詢emp表 select * from emp; --查詢emp表的sal select a.SAL from emp a; --查詢emp表的ename select a.ename from

SQL Server2014匯入匯出資料及報錯

1,資料庫右鍵——任務——匯入資料 2,根據匯入嚮導選擇資料來源 注意:excel2007以上版本選擇2007即可 然後出現報錯: 未在本地計算機上註冊“Microsoft.ACE.Oledb.12.0”提供程式。 解決辦法:下載安裝 Microsoft Offi

MySQL根據select語句匯入匯出資料(含解決中文亂碼方式)

所有都親測,不廢話,上程式碼: 匯出 select count(1) from table  into outfile '/tmp/test.xls' character set gbk; 匯入

PL/SQL Developer匯入匯出資料庫的方法以及說明

PL/SQL Developer是Oracle資料庫中用於匯入或匯出資料庫的主要工具,本文主要介紹了利用PL/SQL Developer匯入和匯出資料庫的過程,並對匯入或匯出時的一些注意事項進行了說明,接下來我們就一一介紹。匯出步驟:    1 tools ->exp

sql語句匯入execl檔案(office2007版)

use tablename go truncate table tablename go insert into table(x,x,x,x,x,x) select x,x,x,x,x,x from OpenDataSource('Microsoft.Ace.OLEDB.12

Oracle自帶的sql developer匯入匯出資料

最近使用oracle匯入匯出資料時,遇到了不少的麻煩!遂對oracle資料庫進行了認真的學習,總結了幾種匯入匯出的命令方法,以備以後查閱。       資料匯出:  1、 將資料庫test完全匯出,使用者名稱system 密碼manager 匯出到d:/daochu.dmp中                 

sql檔案匯入匯出亂碼解決

今天從windows上匯出一個sql執行檔案,再倒入到unbutn中,結果出現亂碼,  解決方式  在匯出mysql sql執行檔案的時候,指定一下編碼格式:  複製程式碼 程式碼如下:mysqldump -uroot -p --default-character-set=

在sqlserver裡用SQL語句匯入EXCEL裡的資料

最近因工作需要,需從第三方的excel表中讀取資料與sqlserver2008中的表進行資料比對。因第一次操作,費了不少勁。一.32位的系統對應安裝32位的excel,64位作業系統對應安裝64位的excel,不然office的access驅動安裝後起不了作用,甚至會安裝報錯

word文件處理成富文字生成sql語句匯入mysql

開發十年,就只剩下這套架構體系了! >>>   

sql語句的備份(匯入匯出

----------匯出----------- #結構+資料 mysqldump -u 使用者名稱 -p 密碼 資料庫名稱>匯出檔案路徑 #結構 mysqldump -u 使用者名稱 -p 密碼 -d 資料庫名稱>匯出檔案路徑   ----------匯入-----

常用SQL語句例項(含過濾及刪除重複資料、匯入匯出資料等)

本文整理一些常用的SQL語句例項(如過濾重複資料、刪除重複資料、匯入匯出資料等),收藏備用,不斷更新中……。歡迎訪問作者網站獲取最新版:http://hi.wonsoft.cn -------------------------------第一部份 高階技巧-------

通過SQL語句直接實現Excel與資料庫的匯入匯出

--下面是匯出真正Excel檔案的方法:(請將一下所有程式碼複製到儲存過程中) ifexists (select*from dbo.sysobjects where id =object_id(N'[dbo].[p_exporttb]') andOBJECTPROPERTY(id, N'IsProced

Sql Server】經典SQL語句大全

left 提高 status 需要 minute etime 路徑 求和 組合 一、基礎 1、說明:創建數據庫 CREATE DATABASE database-name 2、說明:刪除數據庫 drop database dbname 3、說明:備份sql

SQL語句大全

大於等於 算術 特定 per 字符 ger 選擇 小年 外連接 SQL語句實例 表操作 例 1 對於表的教學管理數據庫中的表 STUDENTS ,可以定義如下: CREATE TABLE STUDENTS (SNO NUMERIC