如何查詢其他數據庫內的表(IN子句實例)?
2024-07-21 02:08:21
供稿:網友
如何查詢其他數據庫內的表(in子句實例)?
問題:
如何引用其他數據庫內的表?(查詢外部表)
jet sql 中的 in 子句到底怎么用?在哪里用?
方法一:
select能否查詢其他數據庫中表的數據?
select * from tbl_family in other.mdb"[5556]";
方括號[]內的是其他數據庫的連接字串
方法二:
select *
from [;database=c:/db2.mdb;pwd=1].表1 as a, 表1 as b
where a.id=b.id
請參考:
下面是有關select語句的幫助,請注意 in
select 語句
命令 microsoft jet數據庫引擎從數據庫里返回信息,作為一組記錄。
語法
select [predicate] { * | table.* | [table.]field1 [as alias1] [, [table.]field2 [as alias2] [, ...]]}
from tableexpression [, ...][in外部數據庫]
[where...]
]
[group by...]
]
[having...]
]
[order by...]
]
[with owneraccess option]
in 子句
標識出外部數據庫中的表,例如dbase或paradox數據庫,或microsoft jet外部數據庫,microsoft jet數據庫引擎能夠與之相連。
語法
標識目標表:
[select | insert] into destination in
{path | ["path" "type"] | ["" [type; database = path]]}
標識數據源表:
from tableexpression in
{path | ["path" "type"] | ["" [type; database = path]]}
包含 in 子句的select 語句具有以下幾個部分:
部分 說明
destination 外部表的名稱,將數據插入表中。
tableexpression 表的名稱,可從這些表中獲取數據。這個變量可能是一個單獨的表名,一個已存查詢,或是 inner join, left join, 或 right join.
path 目錄或文件的整個路徑,而且這些目錄或文件包含 table 。
type 數據庫類型的名稱,如果數據庫不是 microsoft jet 數據庫(例如 dbase iii、dbase iv、paradox 3.x或paradox 4.x),則用此類型創建 table 。
說明
通過 in 你一次只可以連接一個外部數據庫
在某些情況中,path 參數可引用包含數據庫文件的目錄。例如,當使用 dbase、foxpro或 paradox 數據庫表時,path 參數將指定包含 .dbf 或 .db 文件的目錄。由 destination 或 tableexpression 參數可得到表文件的名稱。
為了指定非 microsoft jet 數據庫,可在名稱之后附加分號 (;),并用單引號 (' ') 或 (" ") 雙引號將它括入。例如可用 'dbase iv;' 或 "dbase iv;"這兩種寫法。
也可使用 database 保留字指定外部數據庫。例如,以下示例指定相同的表:
... from table in "" [dbase iv; database=c:/dbase/data ales;];
... from table in "c:/dbase/data ales" "dbase iv;"
注意
為改進性能并方便使用,請使用鏈接表而不要用 in。
in 子句示例
下列表顯示您如何使用 in 子句來捕獲外部數據庫的數據。在每一示例之中,假設客戶表是保存在外部數據庫之中。
外部數據庫 sql 語句
microsoft® jet 數據庫 select customerid
from customers
in otherdb.mdb
where customerid like "a*";
dbase iii 或 iv.
捕獲數據由 一 dbase iii 表,以 "dbase iii;" 替代 "dbase iv;"。 select customerid
from customer
in "c:/dbase/data ales" "dbase iv;"
where customerid like "a*";
dbase iii 或 iv 使用數據庫語法。 select customerid
from customer
in "" [dbase iv; database=c:/dbase/data ales;]
where customerid like "a*";
paradox 3.x 或 4.x.
若要由一paradox version 3.x 表捕獲數據,以 "paradox 3.x;" 替代 "paradox 4.x;"。 select customerid
from customer
in "c:/paradox/data ales" "paradox 4.x;"
where customerid like "a*";
paradox 3.x 或 4.x 使用數據庫語法. select customerid
from customer
in "" [paradox 4.x;database=c:/paradox/data ales;]
where customerid like "a*";
microsoft excel工作表 select customerid, companyname
from [customers$]
in "c:/documents/xldata.xls" "excel 5.0;"
where customerid like "a*"
order by customerid;
在工作表之中的一個命名范圍 select customerid, companyname
from customersrange
in "c:/documents/xldata.xls" "excel 5.0;"
where customerid like "a*"
order by customerid;
-----------------------------------------------------------------------------------------------------
關于文本文件可以這樣
select * from [text;fmt=delimited;hdr=yes;database=c:/;].[aparm#txt];
關于在in子句中查詢文本文件如何寫鏈接字串,你可以參考:
http://support.microsoft.com/default.aspx?scid=kb;en-us;234201&product=acc
關于excel可以這樣
insert into 表 select * from [excel 5.0;hdr=yes;database=c:/test.xls].[sheet1$];
說明:
c:/test.xls 是excel文件名
sheet1是要導入的工作表名
其實,你要獲取鏈接字串還有一個方便的辦法,就是使用鏈接向導,先手工鏈接一個文件到你的數據庫中,然后執行以下代碼:
function displaylinkstringfromlinktable()
'先到工具菜單->引用 中引用 microsoft ado ext. 2.x for ddl and security
'引用 adox
dim strtablename
strtablename = "001"
'001是鏈接表的名字
dim cat as new adox.catalog
dim objtable as adox.table
set cat.activeconnection = currentproject.connection
for each objtable in cat.tables
if objtable.name = strtablename then
debug.print objtable.properties("jet oledb:link provider string").name
debug.print objtable.properties("jet oledb:link provider string").value
end if
next objtable
end function
http://www.access911.net 站長收藏
中國最大的web開發資源網站及技術社區,