SQL分布式查詢

2021-06-16 02:50:46 字數 4287 閱讀 5061

/*

測試檔案內容:

e:/module/test.xls

job_desc  job_id  max_lvl  min_lvl

記錄一  1.0  11.0  11.0

記錄二  2.0  22.0  22.0

記錄三  3.0  33.0  33.0

記錄四  4.0  44.0  44.0

e:/module/temp.mdb檔案jobs 表

job_desc job_id  max_lvl  min_lvl

記錄一  1  11  11

記錄二  2  22  22

記錄三  3  33  33

記錄四  4  44  44

e:/module/test.csv

fname lname salary tax

smith william 1000 275

scott tiger 2000 275

john jumangi 2500 345

sam rooban 3524 600

peter norton 1234 320

kathy lee 8300 1200

e:/module/test.txt

fname lname salary tax

smith william 1000 275

scott tiger 2000 275

john jumangi 2500 345

sam rooban 3524 600

peter norton 1234 320

e:/module/union1.txt

fname lname salary tax

smith william 1000 275

scott tiger 2000 275

john jumangi 2500 345

sam rooban 3524 600

peter norton 1234 320

e:/module/union1.csv

fname lname salary tax

smith nana 1000 275

scott jany 2000 275

john leilei 2500 345

sam rooban 3524 600

peter norton 1234 320

*/--read excel sheet using opendatasource

--使用opendatasource讀取excel**

select *

from opendatasource( 'microsoft.jet.oledb.4.0','data source="e:/module/test.xls";

user id=admin;password=;extended properties=excel 5.0')...sheet1$

--read excel sheet using openrowset

--使用openrowset讀取excel**

select * from openrowset('microsoft.jet.oledb.4.0','excel 5.0;database=e:/module/test.xls', 'select * from [sheet1$]')

--read sql server table using opendatasource

--使用opendatasource 跨伺服器查詢

--經測試,data source 只接受機器名。而不接受ip位址

select * from  opendatasource('sqloledb','data source=accp;user id=sa;password=').pubs.dbo.jobs

--read sql server table using openrowset

--使用openrowset 跨伺服器查詢

select * from openrowset('msdasql','driver=;server=accp;uid=sa;pwd=',pubs.dbo.jobs)

--read ms-access table using opendatasource

select * from  opendatasource('microsoft.jet.oledb.4.0','data source="e:/module/temp.mdb";user id=admin;password=')...jobs

--read ms-access table using openrowset

select * from openrowset('microsoft.jet.oledb.4.0','e:/module/temp.mdb';'admin';'', jobs)

--查詢csv檔案

--read csv using openrowset

select * from openrowset('msdasql', 'driver=;

defaultdir=e:/module','select top 6 * from test.csv')

--read text using openrowset

--將上面用到的csv檔案拷乙份重新命名為test.txt即可

select * from openrowset('msdasql', 'driver=;

defaultdir=e:/module','select top 5 * from

test.txt')

--聯合csv和txt 檔案的查詢

select * from openrowset('msdasql','driver=;defaultdir=e:/module;',

'select top 5 * from union1.txt')

union

select * from openrowset('msdasql','driver=;defaultdir=e:/module;',

'select top 5 * from union1.csv')

--not found list

--過濾查詢

select * from openrowset('msdasql', 'driver=;

defaultdir=e:/module;','select top 5 * from union1.txt')

where lname not in

(select lname from openrowset('msdasql', 'driver=;

defaultdir=e:/module;','select top 5 * from

union1.csv') )

--建立檢視查詢.txt文字檔案內容

create view [mytext] as

select * from openrowset('msdasql', 'driver=;

defaultdir=e:module;','select top 5 * from

test.txt')go

select * from [mytext] go

select * from [mytext] where fname like 's%' go

--建立檢視查詢.xls檔案內容

create view [myexcel] as

select *

from opendatasource( 'microsoft.jet.oledb.4.0',

'data source="e:module/test.xls";

user id=admin;password=;extended properties=excel 8.0')...sheet1$ go

select * from [myexcel] where job_desc like '%三%'

//hdr有2個值

//hdr=no無字段列

//hdr=yes第一行作為欄位列

//imex有3個值:

//0:此excel只可讀取

//1:此excel只可寫入

//2:此excel同時支援讀取及寫入。

system.data.oledb.oledbcommand command  = new oledbcommand();

command.connection = connection;

command.connection.open();

command.commandtext = sql;

console.writeline (sql);

trycatch (exception ex)}

sql 分布式查詢

遠端鏈結伺服器機器名 roy 例項名 roy sql2005de 登陸名 sa 密碼 test2005 建立鏈結伺服器 exec master.dbo.sp addlinkedserver server n roy lnk srvproduct n provider n sqloledb datas...

sql分布式查詢

建立鏈結伺服器 exec sp addlinkedserver itsv sqloledb 192.168.0.88 exec sp addlinkedsrvlogin itsv false null user emenudata2fortesting sa exec sp addlinkedser...

分布式查詢

sql server所謂的分布式查詢 distributed query 是能夠訪問存放在同一部計算機或不同計算機上的sql server 或不同種類的資料來源,從概念上來說分布式查詢與普通查詢區別 它需要連線多個mssql伺服器也就是具有多了資料來源.實現在伺服器跨域或跨伺服器訪問.而這些查詢是否...