![](/img/trans.png)
[英]How do I create an index from a stored procedure or create index on every table in MySQL?
[英]How do I create a stored proc to ensure that every table contains certain columns?
我有一个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时,再次没有得到任何错误。
知道发生了什么问题以及如何正确执行此操作吗?
我发现了问题。 尝试将其作为EXECUTE BLOCK
语句运行后,实际上收到了一条有用的错误消息:“未知令牌:_OWNER”。
显然,在Hopper调试器下运行proc时,这不会显示,但是包含表名的元数据选择结果带有尾随空格。 所以下面这行:
replace('ALTER TABLE %T ADD CONSTRAINT FK_%T_OWNER foreign key (OWNER) references USERS', '%T', :name)
解决如下问题:
ALTER TABLE TABLENAME ADD CONSTRAINT FK_TABLENAME _OWNER foreign key (OWNER) references USERS
这显然是无效的。 添加行name = trim(name);
在for
循环中的BEGIN
之后立即将其修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.