ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

SQLRPGLE: How to update/delete without host variables? (sql is in a string)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • SQLRPGLE: How to update/delete without host variables? (sql is in a string)

    Can anyone tell me how to best run a delete/inster/update that has been stored in a string?

    E.g.:
    I can code the following:
    Code:
    Exec SQL delete from table1 where lastUse < current timestamp - 7 days;
    But, I would like something like:
    Code:
    MyStatement = 'delete from table1 where lastUse < current timestamp - 7 days';
    Exec SQL :MyStatement;
    Can I do that?/What do I have to do to make it work?
    http://dk.linkedin.com/in/aliceraunsbaek

  • #2
    Re: SQLRPGLE: How to update/delete without host variables? (sql is in a string)

    Hi aliceraunsbaek:

    I think i stole this from Bob Cozzi:

    Code:
    0001.00 H DFTACTGRP(*NO) ACTGRP(*NEW) OPTION(*SRCSTMT)                   
    0002.00 D RunSQL          PR                  EXTPGM('RUNSQLLE')         
    0005.00 D  stmt                       3000A   Const Varying              
    0006.00 D RunSQL          PI                                             
    0007.00 D  stmt                       3000A   Const Varying              
    0008.00                                                                  
    0009.00 D hostSQLStmt     S           3000A     
    0013.00  /free    
    0013.01      *INLR = *ON;                            
    0014.00      EXEC SQL                                             
    0015.00         SET OPTION                                        
    0016.00         commit = *NONE,                                   
    0017.00         CLOSQLCSR = *ENDMOD,                              
    0018.00         NAMING = *SYS;                                    
    0019.00                                                           
    0020.00         if (%len(stmt) > 0 and stmt <> *blanks);          
    0021.00             hostSqlStmt = stmt;                           
    0022.00            exec sql execute immediate :hostSqlStmt;       
    0023.00         endif;                                            
    0024.00  /end-free
    Best of Luck
    GLS
    The problem with quotes on the internet is that it is hard to verify their authenticity.....Abraham Lincoln

    Comment


    • #3
      Re: SQLRPGLE: How to update/delete without host variables? (sql is in a string)

      why use dynamic SQL for this?

      Code:
      Exec SQL
        Delete From Table1 where lastUse < current timestamp - 7 days
      that should work and perform better...
      I'm not anti-social, I just don't like people -Tommy Holden

      Comment


      • #4
        Re: SQLRPGLE: How to update/delete without host variables? (sql is in a string)

        Originally posted by GLS400 View Post
        Code:
        0020.00         if (%len(stmt) > 0 and stmt <> *blanks);          
        0021.00             hostSqlStmt = stmt;                           
        0022.00            exec sql execute immediate :hostSqlStmt;       
        0023.00         endif;
        Ahh! Thank you! That is the one =0)

        Originally posted by tomholden View Post
        why use dynamic SQL for this?
        Code:
        Exec SQL
          Delete From Table1 where lastUse < current timestamp - 7 days
        that should work and perform better...
        I'm creating a 'clean up' program. It reads from a table how to clean up each file:

        Code:
        Format - FILOPRYD            ....+...10....+...20....+...30....+...40...
        FIL...........K01.....(10)   SYSOVSYS  :                                
        OPRYDHYP.............(4.0)     10 :                                     
        REORGHYP.............(4.0)      0 :                                     
        SQLQUERY.............(256)    SidstOpdat > (current timestamp - 1 days) 
        SIDSTKOERT............(26)   0001-01-01-00.00.00.000000:                
        STATUSKODE...........(5.0)       0 :
        As I want to read the where clause from the table I cannot put it directly in the code.
        http://dk.linkedin.com/in/aliceraunsbaek

        Comment

        Working...
        X