2

我正在尝试在 db2 过程中执行查询:

CREATE OR REPLACE PROCEDURE TEST (IN indbnm VARCHAR(30), IN intblnm VARCHAR(30))
LANGUAGE SQL
BEGIN
DECLARE statmnt2 VARCHAR(1000); 
DECLARE VAR_COD_TIPO_ARQU CHAR(1);  
DECLARE stmt1 STATEMENT; 

SET statmnt2 = 'SELECT COD_TIPO_ARQU FROM '||indbnm||'.'||intblnm||' FETCH FIRST 1 ROWS ONLY';
PREPARE stmt1 FROM statmnt2;
SET VAR_COD_TIPO_ARQU = EXECUTE (stmt1);
END@

这会产生以下错误:

DB21034E  The command was processed as an SQL statement because it was not a 
valid Command Line Processor command.  During SQL processing it returned:
SQL0206N  "STMT1" is not valid in the context where it is used.  LINE 
NUMBER=33.  SQLSTATE=42703

VAR_COD_TIPO_ARQU动态设置值的正确方法是什么COD_TIPO_ARQU

谢谢你。

4

4 回答 4

3

问题是您设置执行结果的方式:

EXECUTE stmt1 into VAR_COD_TIPO_ARQU ;

这是成功执行的完整代码

CREATE OR REPLACE PROCEDURE TEST (IN indbnm VARCHAR(30), IN intblnm VARCHAR(30))
LANGUAGE SQL
BEGIN
DECLARE statmnt2 VARCHAR(1000); 
DECLARE VAR_COD_TIPO_ARQU CHAR(1);  
DECLARE stmt1 STATEMENT; 

SET statmnt2 = 'SELECT COD_TIPO_ARQU FROM '||indbnm||'.'||intblnm||' FETCH FIRST 1 ROWS ONLY';
PREPARE stmt1 FROM statmnt2;
EXECUTE stmt1 into VAR_COD_TIPO_ARQU ;
END@
于 2013-04-01T10:34:51.840 回答
2

嗨,这是正确的解决方案:

SET statmnt = 'SELECT COD_TIPO_ARQU FROM '||indbnm||'.'||intblnm||' FETCH FIRST 1 ROWS ONLY';
PREPARE stmt1 FROM statmnt;

BEGIN 
    DECLARE c1 CURSOR FOR stmt1;
    OPEN c1;
    FETCH c1 into sttmresult;
    CLOSE c1;
END;

泰。

于 2013-04-03T09:30:13.773 回答
2

我知道这个问题被问到已经有一段时间了,但我发现给定的答案都没有奏效。

@AngocA 的解决方案很接近,但正如@Mani_Swetha 指出的那样,该EXECUTE语句由于SELECT位而失败。

在网络上搜索和组合解决方案之后,这最终对我有用:

CREATE OR REPLACE PROCEDURE TEST (IN indbnm VARCHAR(30), IN intblnm VARCHAR(30))
LANGUAGE SQL
BEGIN
DECLARE statmnt2 VARCHAR(1000); 
DECLARE VAR_COD_TIPO_ARQU CHAR(1);  
DECLARE stmt1 STATEMENT; 

SET statmnt2 = 'set ? = (SELECT COD_TIPO_ARQU FROM '||indbnm||'.'||intblnm||' FETCH FIRST 1 ROWS ONLY)';
PREPARE stmt1 FROM statmnt2;
EXECUTE stmt1 into VAR_COD_TIPO_ARQU ;
END@

请注意,现在执行的命令是set带有SELECT内部的语句,而不是纯SELECT语句。这就是诀窍。

于 2016-01-08T10:57:41.813 回答
0

感谢@MarcosEXECUTE... INTO ...工作。

此外,使用OUT参数创建存储过程将允许您访问EXECUTE... INTO ...存储过程外部设置的值。

CREATE OR REPLACE PROCEDURE ISH.SAMPLEPROC (OUT o_result VARCHAR(5))
specific proc_user_data_retrieval
dynamic result sets 1
reads sql data
language sql
BEGIN
DECLARE stmt varchar(5000) default null;
DECLARE VAR_X VARCHAR(5);
DECLARE stmt_final STATEMENT;
SET stmt = 'SET (?) = ('||'SELECT JOB_ROLE FROM ISH.USER_DATA LIMIT 1'||')';
PREPARE stmt_final FROM stmt;
EXECUTE stmt_final INTO VAR_X;
SET o_result = VAR_X;
END

然后使用调用存储过程CALL ISH.SAMPLEPROC(?);

于 2019-12-05T18:58:48.690 回答