SQL Access Excel匯入匯出

2021-08-31 04:53:23 字數 3577 閱讀 9923

熟悉sql server 2000的資料庫管理員都知道,其dts可以進行資料的匯入匯出,其實,我們也可以使用transact-sql語句進行匯入匯出操作。在 transact-sql語句中,我們主要使用opendatasource函式、openrowset 函式,關於函式的詳細說明,請參考sql聯機幫助。利用下述方法,可以十分容易地實現sql server、access、excel資料轉換,詳細說明如下:

一、sql server 和access的資料匯入匯出

常規的資料匯入匯出:

使用dts嚮導遷移你的access資料到sql server,你可以使用這些步驟:

○1在sql server企業管理器中的tools(工具)選單上,選擇 data transformation

○2services(資料轉換服務),然後選擇 czdimport data(匯入資料)。

○3在choose a data source(選擇資料來源)對話方塊中選擇 microsoft access as the source,然後鍵入你的.mdb資料庫(.mdb副檔名)的檔名或通過瀏覽尋找該檔案。

○4在choose a destination(選擇目標)對話方塊中,選擇microsoft ole  db prov ider for sql server,選擇資料庫伺服器,然後單擊必要的驗證方式。

○5在specify table copy(指定**複製)或 query(查詢)對話方塊中,單擊copy tables(複製**)。

○6在select source tables(選擇源**)對話方塊中,單擊select all(全部選定)。下一步,完成。

transact-sql語句進行匯入匯出:

1.在sql server 裡查詢access資料:

select * from opendatasource( 'microsoft.jet.oledb.4.0','data source="c:\db.mdb";user id=admin;password=')表名

2.將access匯入 sql server

在sql server 裡執行:

select * into newtable from opendatasource ('microsoft.jet.oledb.4.0','data source="c:\db.mdb";user id=admin;password=' )表名

3.將sql server 表裡的資料插入到access表中

在sql server 裡執行:

insert into opendatasource( 'microsoft.jet.oledb.4.0','data source=" c:\db.mdb";user id=admin;password=')表名 (列名1,列名 2) select 列名1,列名2 from sql表

例項:insert into openrowset('microsoft.jet.oledb.4.0','c:\db.mdb';'admin';'', test) select id,name from test

insert into openrowset('microsoft.jet.oledb.4.0', 'c:\trade.mdb'; 'admin'; '', 表名) select * from sqltablename

二、sql server 和excel的資料匯入匯出

1、在sql server 裡查詢excel資料:

select * from opendatasource( 'microsoft.jet.oledb.4.0','data source="c:\book1.xls";user id=admin;password=;extended properties=excel 5.0')[sheet1$]

下面是個查詢的示例,它通過用於 jet 的 ole db 提供程式查詢 excel 電子**。

select * from opendatasource ( 'microsoft.jet.oledb.4.0','data source="c:\finance\account.xls";user id=admin;password=;extended properties=excel 5.0')xactions

2、將excel的資料匯入 sql server :

select * into newtable from opendatasource( 'microsoft.jet.oledb.4.0','data source="c:\book1.xls";user id=admin;password=;extended properties=excel 5.0')[sheet1$]

例項:select * into newtable from opendatasource( 'microsoft.jet.oledb.4.0','data source="c:\finance\account.xls";user id=admin;password=;extended properties=excel 5.0')xactions

3、將sql server 中查詢到的資料導成乙個excel檔案

t-sql**:

exec master..xp_cmdshell 'bcp 庫名.dbo.表名out c:\temp.xls -c -q -s"servername" -u"sa" -p""'

引數:s 是sql伺服器名;u是使用者;p是密碼

例項:exec master..xp_cmdshell 'bcp saletesttmp.dbo.cusaccount out c:\temp1.xls -c -q -s"pmserver" -u"sa" -p"sa"'

exec master..xp_cmdshell 'bcp "select au_fname, au_lname from pubs..authors order by au_lname" queryout c:\ authors.xls -c -sservername -usa -ppassword'

在vb6中應用ado匯出excel檔案**:

dim cn as new adodb.connection

cn.open "driver=;server=websvr;database=webmis;uid=sa;wd=123;"

cn.execute "master..xp_cmdshell 'bcp "select col1, col2 from 庫名.dbo.表名" queryout e:\dt.xls -c -sservername -usa -ppassword'"

4、在sql server 裡往excel插入資料:

insert into opendatasource( 'microsoft.jet.oledb.4.0','data source="c:\temp.xls";user id=admin;password=;extended properties=excel 5.0')table1 (a1,a2,a3) values (1,2,3)

t-sql**:

insert into

opendatasource('microsoft.jet.oledb.4.0','extended properties=excel 8.0;data source=c:\training\inventur.xls')[filiale1$] (bestand, produkt) values (20, 'test')

sql access excel 資料互導

一 sql server 和access的資料匯入匯出 常規的資料匯入匯出 使用dts嚮導遷移你的access資料到sql server,你可以使用這些步驟 1在sql server企業管理器中的tools 工具 選單上,選擇data transformation 2services 資料轉換服務 ...

idea導包都報錯 python 導包

python 導包 python語言的使用者對導包應該不會陌生,下面我們就一起看一下python中有哪些到爆方式 1 import 直接使用import進行導包是一種很常見的方式,每次可以匯入乙個或多個包,直接使用import導包相當於是匯入了這個模組的整體,這個模組包含的所有物件都可以呼叫,但是要...

oralce導指令碼

用命令匯出 dmp c users administrator exp cms cms 10.196.149.11 orcl owner cms file d dmp 1 將資料庫sampledb完全匯出,使用者名稱system 密碼manager 匯出到e sampledb.dmp中 exp sy...