SQL插入SQLRPGLE后无法更新文件

Jav*_*Ali 1 sql db2 rpgle ibm-midrange

美好的一天,

我正在编写一个程序来构建一个报告,我在其中构建SQL语句以将所选记录插入到文件中,然后在该插入之后我想对文件进行简单更新以更改某些记录中的选择字段.

问题是在插入运行后,每当我尝试更新文件时,我都会收到一个记录或文件正在使用错误.

我尝试用sqlrpgle和I/O读取和设置函数以编程方式更新它,我甚至尝试在运行程序后只更新STRSQL中的文件,我都遇到了同样的错误.

我怀疑我没有正确关闭,但我不确定是什么.

代码如下

// Assign SQL Query

    sqlstmt = 'insert into jallib/orhsrpt ('+
              'oacctd, oacmp, oaord, oacust, o8type, ' +
              'o8text, o8date, o8time ) ' +
              'select oacctd, oacmp, oaord, oacust, ' +
              'o8type, o8text, o8date, o8time ' +
              'from r50files.vcohead ' +
              'join r50files.vcopkct ' +
              'on oacmp = o8cmp and oaord = o8ord ' +
              'where oacmp = 1 ' +
              'and o8type not in (' +
              '''C'',''a'',''H'',''E'',''F'', '+
              '''A'',''1'',''N'',''M'') ' +
              'and oacctd = ' + curdate +
              ' order by oaord, o8time ';

    // Prepare for multiple sql statements 

    exec sql
        Set Option Commit = *NONE; 

    // Clear output file before executing SQL

    exec sql
        Delete from jallib/orhsrpt;

    if sqlcode < *zeros; 
        errmsg = 'Delete of file failed'; 
    endif; 

    // Execute SQL Insert statement

    exec sql prepare sqlsel from :sqlstmt;
    exec sql execute sqlsel;

    if sqlcode < *zeros; 
        errmsg = 'Insert of file failed'; 
    endif;

    // Update file data

    exec sql
        Set Option clossqlcsr = *ENDMOD; 

    exec sql  
        Update jallib/orhsrpt 
        set o8text = 'Order Invoiced' 
        where o8type = 'I' 
Run Code Online (Sandbox Code Playgroud)

STRSQL的错误如下

使用JALLIB类型*FILE中的行或对象ORHSRPT.

Pla*_*1st 7

快速回答是插件未关闭,因为您的模块未按照Set Option您指定的结束.但是,这里的正确答案是您根本没有理由使用动态SQL语句.它们通常较慢且更容易出错,并且遇到类似这样的问题.您应该使用常规嵌入式SQL语句,如下所示:

exec sql
    set option commit = *NONE; 

// Clear output file before executing SQL

exec sql
    delete from jallib/orhsrpt;

if sqlstate <> *zeros; 
    errmsg = 'Delete of file failed'; 
endif; 

exec sql
    insert into jallib/orhsrpt (
        oacctd, oacmp, oaord, oacust,
        o8type, o8text, o8date, o8time )
    select oacctd, oacmp, oaord, oacust, o8type,
           o8text, o8date, o8time
    from r50files.vcohead join r50files.vcopkct
        on oacmp = o8cmp and oaord = o8ord
    where oacmp = 1 and o8type not in (
              'C','a','H','E','F', 'A','1','N','M') and
          oacctd = :curdate
    order by oaord, o8time;

exec sql  
    update jallib/orhsrpt 
    set o8text = 'Order Invoiced' 
    where o8type = 'I' 
Run Code Online (Sandbox Code Playgroud)

这是更好的做法,应该解决您的问题.