【问题标题】:Firebird: simulating create table as?Firebird:模拟创建表为?
【发布时间】:2013-03-14 16:00:46
【问题描述】:

我正在寻找一种方法来从 SP 中模拟 Firebird 中的“create table as select”。

我们在另一个产品中经常使用这个语句,因为它很容易制作更小的、可索引的集合,并在服务器端提供非常快速的结果。

create temp table a select * from xxx where ...
create indexes on a ...
create temp table b select * from xxx where ...
create indexes on b ...

select * from a
union
select * from b

或避免子查询中的三个或更多级别。

select * 
from a where id in (select id 
                    from b 
                    where ... and id in (select id from c where))

“create table as select”非常好,因为它提供了正确的字段类型和名称,所以我不需要预先定义它们。

我可以用 Delphi 在 Firebird 中模拟“创建表为”:

选择不带行,获取表字段类型,将它们转换为创建表 SQL,运行它,然后“插入临时表”+ 带行的 selectsql(无排序依据)。 没关系。

但是我可以在一个通用的存储过程中创建相同的东西,它获取一个 select sql,并使用结果创建一个新的临时表吗?

那么:我可以获取查询结果的字段类型,以便我可以从中创建字段创建器 SQL 吗?

我只是问是否有办法(然后我必须指定列)。

【问题讨论】:

  • 您可以使用 select * from rdb$relation_fields where rdb$relation_name = 'A' 获取列定义,然后从那里继续。
  • 但是我可以用连接字段做什么(选择 a.x, b.y from ....)? :-(
  • 那么如何使用 Create View ..... 作为选择?
  • 不知道。如果您在 Firebird 邮件列表中提出这个问题,您可能会得到更好的答案。
  • 创建视图是否支持在子视图上使用索引? select * from view1, view2, view3 where ...

标签: delphi stored-procedures firebird temp-tables create-table


【解决方案1】:

Firebird 不支持在存储过程中执行 DDL。您可以使用EXECUTE STATEMENT 来执行此操作,但不建议这样做(请参阅“No data returned”主题末尾的警告)。

拥有“临时集”的一种方法是使用(事务级)Global Temporary Table。创建 GTT 作为数据库的一部分,使用正确的数据类型但没有约束(当您只填充一些列而不是全部时,这些可能会进入方式) - 然后每个事务只看到它自己的表和数据版本...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多