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

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

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

2024-08-31 00:48:31
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

sql server 與access、excel的數(shù)據(jù)轉(zhuǎn)換

 

熟悉sql server 2000的數(shù)據(jù)庫(kù)管理員都知道,其dts可以進(jìn)行數(shù)據(jù)的導(dǎo)入導(dǎo)出,其實(shí),我們也可以使用transact-sql語(yǔ)句進(jìn)行導(dǎo)入導(dǎo)出操作。在transact-sql語(yǔ)句中,我們主要使用opendatasource函數(shù)、openrowset 函數(shù),關(guān)于函數(shù)的詳細(xì)說(shuō)明,請(qǐng)參考sql聯(lián)機(jī)幫助。利用下述方法,可以十分容易地實(shí)現(xiàn)sql server、access、excel數(shù)據(jù)轉(zhuǎn)換,詳細(xì)說(shuō)明如下:

 

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

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

使用dts向?qū)нw移你的access數(shù)據(jù)到sql server,你可以使用這些步驟:

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

  2services(數(shù)據(jù)轉(zhuǎn)換服務(wù)),然后選擇  czdimport data(導(dǎo)入數(shù)據(jù))。

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

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

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

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

 

transact-sql語(yǔ)句進(jìn)行導(dǎo)入導(dǎo)出:

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導(dǎo)入sql server

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

在sql server 里運(yùn)行:

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 里運(yùn)行:

insert into opendatasource( 'microsoft.jet.oledb.4.0',

  'data source=" c:/db.mdb";user id=admin;password=')...表名

(列名1,列名2)

select 列名1,列名2  from  sql表

 

實(shí)例:

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ù)導(dǎo)入導(dǎo)出

 

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$]

 

下面是個(gè)查詢的示例,它通過(guò)用于 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ù)導(dǎo)入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$]

 

實(shí)例:

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ù)導(dǎo)成一個(gè)excel文件

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

t-sql代碼:

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

參數(shù):s 是sql服務(wù)器名;u是用戶;p是密碼

說(shuō)明:還可以導(dǎo)出文本文件等多種格式

 

實(shí)例: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中應(yīng)用ado導(dǎo)出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 庫(kù)名.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') 

-------------------------------------------------------------------------------------------------

總結(jié):利用以上語(yǔ)句,我們可以方便地將sql server、access和excel電子表格軟件中的數(shù)據(jù)進(jìn)行轉(zhuǎn)換,為我們提供了極大方便!

 

參考:

http://www.itrain.de/knowhow/sql/transfer/adhoc/

 

 
,歡迎訪問(wèn)網(wǎng)頁(yè)設(shè)計(jì)愛(ài)好者web開(kāi)發(fā)。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 建德市| 望奎县| 米脂县| 偃师市| 合山市| 灵璧县| 正定县| 桐柏县| 马公市| 广德县| 兴文县| 万安县| 临武县| 邢台市| 林芝县| 金寨县| 孝感市| 湖州市| 巴林右旗| 宣武区| 简阳市| 教育| 金山区| 治多县| 虹口区| 东明县| 武定县| 鞍山市| 敦煌市| 南涧| 措勤县| 台中县| 孝感市| 抚松县| 常州市| 章丘市| 菏泽市| 南安市| 昂仁县| 克拉玛依市| 东丽区|