无法在 db2 的 SQL PL 存储过程中更改表(添加唯一约束)
我正在尝试在 SQL PL 中为 db2 创建一个存储过程。需要添加一个新字段作为唯一键的一部分,因此我需要先删除以前的 uk,然后添加新的唯一键。这对于 MS SQLServer 和 Oracle 来说效果很好,但对于 db2 却不能。
CREATE PROCEDURE update_unique_key ()
LANGUAGE SQL
BEGIN
DECLARE uk_constraint_name VARCHAR(50);
DECLARE sql_stmt VARCHAR(100);
SELECT constname INTO uk_constraint_name FROM SYSCAT.TABCONST WHERE tabname = 'TABLE_NAME' AND type = 'U';
SET sql_stmt = 'alter table TABLE_NAME drop constraint ' || char(uk_constraint_name);
SET sql_stmt_2 = 'alter table TABLE_NAME add unique (F1, F2)';
prepare s1 from sql_stmt;
prepare s2 from sql_stmt_2;
EXECUTE IMMEDIATE s1;
EXECUTE IMMEDIATE s2;
END
尝试使用 IBM Data Studio 作为客户端部署例程时出现此错误(数据库是 db2 9.5)
DEV.UPDATE_UNIQUE_KEY - Deploy started.
Create stored procedure returns SQLCODE: -206, SQLSTATE: 42703.
DEV.UPDATE_UNIQUE_KEY: 8: "SQL_STMT_2" is not valid in the context where it is used.. SQLCODE=-206, SQLSTATE=42703, DRIVER=4.12.55
"SQL_STMT_2" is not valid in the context where it is used.. SQLCODE=-206, SQLSTATE=42703, DRIVER=4.12.55
我不明白的是:
- 如果在例程中我只是放弃它起作用的约束。
- 我可以添加约束,只需从 sql 客户端 (Squirrel) 执行更改表
- 如果我只有一个例程尝试添加约束,我会得到相同的错误。
我很感激任何帮助。谢谢
I am trying to create a store procedure in SQL PL for db2. A new field needs to be added as part of the unique key, so I need to drop the previous uk first, and then add the new unique key. That worked fine for MS SQLServer and Oracle, but I cannot make it for db2.
CREATE PROCEDURE update_unique_key ()
LANGUAGE SQL
BEGIN
DECLARE uk_constraint_name VARCHAR(50);
DECLARE sql_stmt VARCHAR(100);
SELECT constname INTO uk_constraint_name FROM SYSCAT.TABCONST WHERE tabname = 'TABLE_NAME' AND type = 'U';
SET sql_stmt = 'alter table TABLE_NAME drop constraint ' || char(uk_constraint_name);
SET sql_stmt_2 = 'alter table TABLE_NAME add unique (F1, F2)';
prepare s1 from sql_stmt;
prepare s2 from sql_stmt_2;
EXECUTE IMMEDIATE s1;
EXECUTE IMMEDIATE s2;
END
Getting this error when trying to deploy the routine with IBM Data Studio as client (the database is db2 9.5)
DEV.UPDATE_UNIQUE_KEY - Deploy started.
Create stored procedure returns SQLCODE: -206, SQLSTATE: 42703.
DEV.UPDATE_UNIQUE_KEY: 8: "SQL_STMT_2" is not valid in the context where it is used.. SQLCODE=-206, SQLSTATE=42703, DRIVER=4.12.55
"SQL_STMT_2" is not valid in the context where it is used.. SQLCODE=-206, SQLSTATE=42703, DRIVER=4.12.55
The thing I don't understand is:
- If in the routine I just drop the constraint it works.
- I can add the constraint just doing an alter table from a sql client (Squirrel)
- If I have just a routine trying to add the constraint I get the same error.
I appreciate any help. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只是忘记声明变量... DECLARE sql_stmt_2 VARCHAR(100);
I just forgot to declare the variable... DECLARE sql_stmt_2 VARCHAR(100);