Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Can you use a Variable in SQL as a File Name

Can you use a Variable in SQL as a File Name

Can you use a Variable in SQL as a File Name

Is there a way to use a variable in the From SQL statement, I tried :@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                                                  
C+ INSERT INTO FLLDSUPWP                                    
C+    (WPFILE,WPRECD,WPDATE,WPUSER,WPPGM)                   
C+ FROM   :@FILE                                         
C                   ENDSR  

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


RE: Can you use a Variable in SQL as a File Name

You have to use dynamic sql to do so.

i.e. in RPG IV


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                                      



RE: Can you use a Variable in SQL as a File Name

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.

RE: Can you use a Variable in SQL as a File Name

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).


RE: Can you use a Variable in SQL as a File Name

The following works well


  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;
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.

(Contract Code-monkey)

It's amazing how many ways there are to skin a cat
(apologies to the veggies)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close