我有一个 Firebird 数据库,我想确保每一行一次最多可以由一个用户编辑。 为此,我想OWNER
在每个适用的表格上放置一列。为每张表手动执行此操作会有点乏味,因此我尝试编写一种自动化方法。首先,创建一个视图,为您提供所有需要更新的表:
CREATE VIEW OWNABLE_TABLES_V
(
NAME
)
AS
SELECT tables.RDB$RELATION_NAME
FROM RDB$RELATIONS tables
WHERE (tables.RDB$SYSTEM_FLAG=0) and (tables.rdb$view_source is null)
and not exists (
--table containing names of tables that are exceptions to the rule
select name from META_NOT_OWNABLE
where name = tables.RDB$RELATION_NAME)
and not exists (
select * from RDB$RELATION_FIELDS fields
where (fields.RDB$RELATION_NAME = tables.RDB$RELATION_NAME)
and (fields.RDB$FIELD_NAME = 'OWNER'))
order by tables.RDB$RELATION_NAME;
这工作正常。
然后,创建一个 proc 来进行维护:
CREATE PROCEDURE PREPARE_OWNERSHIP
AS
declare variable name varchar(31);
BEGIN
for select NAME from OWNABLE_TABLES_V into :name do
BEGIN
execute statement replace('ALTER TABLE %T ADD OWNER INTEGER', '%T', :name)
with autonomous transaction;
execute statement replace('ALTER TABLE %T ADD OWNER_TIMEOUT TIMESTAMP', '%T', :name)
with autonomous transaction;
execute statement replace('ALTER TABLE %T ADD CONSTRAINT FK_%T_OWNER foreign key (OWNER) references USERS', '%T', :name)
with autonomous transaction;
END
END
但是当我运行这个时,什么也没有发生。没有报告错误,但没有表更新为新的簿记。当我在 Firebird 的 Hopper proc 调试器下运行 proc 时,我再次没有收到任何错误。
知道出了什么问题,以及如何正确地做到这一点?