国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > SQL Server > 正文

SQL SERVER與ACCESS、EXCEL的數(shù)據(jù)轉換

2024-08-31 00:48:41
字體:
來源:轉載
供稿:網(wǎng)友
菜鳥學堂:
熟悉sql server 2000的數(shù)據(jù)庫管理員都知道,其dts可以進行數(shù)據(jù)的導入導出,其實,我們也可以使用transact-sql語句進行導入導出操作。在transact-sql語句中,我們主要使用opendatasource函數(shù)、openrowset 函數(shù),關于函數(shù)的詳細說明,請參考sql聯(lián)機幫助。利用下述方法,可以十分容易地實現(xiàn)sql server、access、excel數(shù)據(jù)轉換,詳細說明如下:


一、sql server 和access的數(shù)據(jù)導入導出

常規(guī)的數(shù)據(jù)導入導出:

使用dts向導遷移你的access數(shù)據(jù)到sql server,你可以使用這些步驟:

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

  2services(數(shù)據(jù)轉換服務),然后選擇  czdimport data(導入數(shù)據(jù))。

  3在choose a data source(選擇數(shù)據(jù)源)對話框中選擇microsoft access as the source,然后鍵入你的.mdb數(shù)據(jù)庫(.mdb文件擴展名)的文件名或通過瀏覽尋找該文件。

  4在choose a destination(選擇目標)對話框中,選擇microsoft ole db prov ider for sql server,選擇數(shù)據(jù)庫服務器,然后單擊必要的驗證方式。

  5在specify table copy(指定表格復制)或query(查詢)對話框中,單擊copy tables(復制表格)。

6在select source tables(選擇源表格)對話框中,單擊select all(全部選定)。下一步,完成。

 

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

1. 在sql server里查詢access數(shù)據(jù):

-- ======================================================

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表里的數(shù)據(jù)插入到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的數(shù)據(jù)導入導出

 

1、在sql server里查詢excel數(shù)據(jù):

-- ======================================================

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的數(shù)據(jù)導入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中查詢到的數(shù)據(jù)導成一個excel文件

-- ======================================================

t-sql代碼:

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

參數(shù):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={sql server};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插入數(shù)據(jù):

-- ======================================================

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 server、access和excel電子表格軟件中的數(shù)據(jù)進行轉換,為我們提供了極大方便!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 邵武市| 怀集县| 高邑县| 周至县| 新河县| 安顺市| 北流市| 天津市| 石狮市| 海伦市| 涪陵区| 凤冈县| 常德市| 南雄市| 潮安县| 昭平县| 建湖县| 凤山市| 灵石县| 三江| 志丹县| 上高县| 青河县| 衡东县| 英吉沙县| 延安市| 杭锦后旗| 桑植县| 洛南县| 平利县| 江陵县| 高淳县| 洪泽县| 林芝县| 叙永县| 额济纳旗| 岱山县| 秭归县| 新野县| 龙海市| 辉县市|