SQL插入SQLRPGLE

时间:2018-10-24 14:05:37

标签: sql db2 ibm-midrange rpgle

大家好,

我正在编写一个用于生成报告的程序,在其中生成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' 

STRSQL的错误如下

  

正在使用JALLIB类型* FILE的行或对象ORHSRPT。

1 个答案:

答案 0 :(得分: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' 

这是更好的做法,应该可以解决您的问题。

相关问题