SQL查询,从不同的FDB数据库
问题描述:
我有2个FDB数据库company.fdb
和timeAtt.fdb
SQL查询,从不同的FDB数据库
company.fdb
连接表包含staffDetail
表
staffId - 001
staffName - Andy
staffStatus - Active
timeAtt.fdb
包含staffAtt
表
staffId - 001
staffName - Andy
timeIn - 07:30
timeOut - 04:30
LI - X (late in)
AB - X (absent)
remarks - Emergency leave
现在,我想查看仅缺席的员工h我这样做
SELECT staffId,staffName,remarks FROM timeAtt.fdb WHERE AB = 'X'
但问题是,查询也显示不活动的工作人员。所以我需要加入staffAtt
从timeAtt.fdb
和staffDetail
从company.fdb
只显示员工处于活动状态。我怎样才能做到这一点?
答
你不能。在Firebird中,您只能在同一个数据库文件中连接表。 Firebird 2.5将EXECUTE STATEMENT
扩展为在外部数据源上执行语句,但在不同数据库中只有一个查询参考表是不可能的。
您有以下选择:
- 创建一个临时表,复制你需要到该临时表中,然后加入到临时表中的数据,
- 合并数据库为一体。
答
正如马克注意到你不能直接加入他们。但是你仍然可以使用DSQL语句来获得你想要的。
一起使用execute block
和execute statement
。这是一个示例。
execute block
returning (
staffId integer,
staffName varchar(100),
remarks varchar(100)
staffStatus varchar(10))
as
begin
for SELECT staffId, staffName, remarks
FROM timeAtt
WHERE AB = 'X'
into :staffId, :staffName, :remarks do begin
execute statement 'select staffStatus from company where staffId = ' || staffId
on external "your:connection:\string\and\db.fdb" as user FOO password BAR
into :staffStatus;
suspend;
end
end
我明白了......我不能在火鸟里做这个......我要试试临时表方法。 Tq Mark – WaN