Solved

field name as variable

Posted on 2010-09-22
5
1,563 Views
Last Modified: 2012-08-13
Hi,

I have the following situation:
a data structure defined as:

File1Fields       E DS                  Extname(File1)

in File2 (fldname,value)  I have all the field names of File1 and their values (fld1,val1 ; fld2, val1).

now, i want to initialize File1Fields with the values from file2,

File1Fields.fld1 would equal a value of fld1 from File2 and so on...

The problem is how to reference  those fields in File1Fields without hardcoding their name
and having only field name in a variable (field) of File2.
if there would be a way to iterate through a data structure than there would not be a problem:

File1Fields(i) = value;

Any suggestions ?
0
Comment
Question by:iskibinska
  • 4
5 Comments
 
LVL 16

Expert Comment

by:theo kouwenhoven
ID: 33743154
Hi Iskibinsa,

What you can do is to use fields from file 2, for creating a SQL insert or update command .
(Dynamic imbedded SQL)

Good Luck,
Murph


0
 

Author Comment

by:iskibinska
ID: 33743451
Hi Murph,

Could you send me a code snippet ?
i am not sure if i follow your idea

Thanks
0
 
LVL 16

Expert Comment

by:theo kouwenhoven
ID: 33743848
Just an example to get the idea, not sompiled, not tested:
D**************************************************                                                                
D SQLSTMT         S           1024A   
D**************************************************                                                                
 /free
    Eval SQLSTM = 'INSERT INTO FILE1 (' + Field1 + ', 'Field2 + ','    
                + Field2 + ', 'Field3 + ',' + Field4 + ') Values(' +    
                + Val1 + ', 'Val2 + ',' + Val3 +',' + Val4 + ')
 /end-free
C*************************************************************
C* Prepare STMT as initialized in declare section            *
C*************************************************************
C/EXEC SQL
C+ PREPARE S1 FROM :SQLSTM
C/END-EXEC
C*
C*************************************
C* Declare Cursor for STMT           *
C*************************************
C/EXEC SQL
C+ DECLARE C1 CURSOR FOR S1
C/END-EXEC

C**********************
C* Open Cursor        *
C**********************
C/EXEC SQL
C+ OPEN C1 
C/END-EXEC
C*
C******************
C* Close cursor   *
C******************
C/EXEC SQL
C+  CLOSE C1
C/END-EXEC    

Open in new window

0
 
LVL 16

Accepted Solution

by:
theo kouwenhoven earned 500 total points
ID: 33743927
Hi Hi Iskibinsa,

See also this link for more info and examples, I hope it will help


http://www.mcpressonline.com/programming/sql/using-dynamic-sql-in-cl-part-1running-action-queries.html


GoodLuck,
murph
0
 
LVL 16

Expert Comment

by:theo kouwenhoven
ID: 33783789
And does it work????
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ODBC Issues with Microsoft Access 2010 6 394
Query Last Used Date on Multiple Servers 8 50
SQL400 max size 5 99
FTP Server IBM i -  Passive Modus and IP Address 2 71
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Some of the SEO trends we might expect in 2017.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question