Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can you use a Variable in SQL as a File Name

Status
Not open for further replies.

OAKEJ

Programmer
Apr 13, 2005
39
US
Is there a way to use a variable in the From SQL statement, I tried :mad:FILE and it fails to compile, when I use @FILE by itself it will compile but in the joblog it says it can't find the file *LIBL/@FILE, any suggestions

@FILE is a file name that is passed earlier in the program

SQL Statement..................
C $InsertSql BEGSR

C/EXEC SQL SET TRANSACTION ISOLATION LEVEL NO COMMIT
C/END-EXEC

C/EXEC SQL

C+ INSERT INTO FLLDSUPWP
C+ (WPFILE,WPRECD,WPDATE,WPUSER,WPPGM)

C+ SELECT :mad:FILE,COUNT(*),:ISO_DATE,:USRNAM,:pGMNAM

C+ FROM :mad:FILE

C/END-EXEC
*
C ENDSR

Compile Error...............................
SQL0104 30 80 Position 19 Token : was not valid. Valid tokens: ( TABLE LATERAL <IDENTIFIER>.


 

You have to use dynamic sql to do so.

i.e. in RPG IV

Code:
C                   Eval      Stm = 'INSERT INTO FLLDSUPWP ( +         
C                             WPFILE, WPRECD, WPDATE, WPUSER, WPPGM ) +
C                              SELECT ' + àFILE + ', COUNT(*), ' +     
C                               ISO_DATE + ', ' + USRNAM + ', ' +      
C                               PGMNAM +                               
C                             ' FROM ' + àFILE                         
C/EXEC SQL EXECUTE IMMEDIATE :STM                                      
C/END-EXEC



Philippe
 
That was what I thought, I wanted to see if it was possible with Embedded SQL, but it doesn't look like it is. Thanks for the response.
 
There's some misunderstanding there.
That dynamic sql statement is typically the sort of statement that can be used in a SQL embedded program (RPG, COBOL, etc).

Philippe
 
The following works well
Code:
/free
  strSQL = 'Select * From ' + %Trim( p@File )
         + ' Where FIELD1 = '''
         + p@Selection
         + '''';
  Exec SQL Prepare FR_Statment From :strSQL;
  Exec SQL Declare FR_Cursor Cursor For FR_Statment;
  Exec SQL Open FR_Cursor;
  Exec SQL Fetch From FR_Cursor
           For :Required Rows
           Into :FR_Data;
  Exec SQL Get Diagnostics :Retrieved = ROW_COUNT;
  For x = 1 to Retrieved;
    RRNS1 += 1;
    ........
  EndFor;
You can make the setup of strSQL as complicated as you want, adding selection on different fields, selecting different fields and even files. The FETCH statement dumps the data into an array or multi-occurrence data structure which can be easily processed in a For...EndFor loop.

PeteJ
(Contract Code-monkey)

It's amazing how many ways there are to skin a cat
(apologies to the veggies)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top