Solved

retrieving value from dynamic SQL statament

Posted on 2011-03-09
5
457 Views
Last Modified: 2012-05-11
Hi, i am preparing a dynamic SELECT (*) with dynamic where part.

It looks like SELECT .... into :hostvar where ..........

Now, how to retrieve this value after executing this statement ? . My hostvar is always 0

0
Comment
Question by:Rozamunda
  • 3
5 Comments
 
LVL 16

Expert Comment

by:theo kouwenhoven
ID: 35099159
Is it Stand alone SQL or included in RPG ???
0
 
LVL 18

Expert Comment

by:daveslash
ID: 35100145

Since Rozamunda is using dynamic SQL, it kind of has to be embedded in a high-level language, right? I don't believe the AS/400 can handle dynamic SQL interactively.

You say your host variable is always coming back with zero. What value do you get if you run the query interactively? You could always run the program in debug-mode, that at the point of running the query, and run the query manually.

Also, if I were you, I'd check the SQLCODE after the query since it may have gotten an error.

HTH,
DaveSlash
0
 
LVL 16

Expert Comment

by:theo kouwenhoven
ID: 35100714
If it is embedded SQL, the Select statment will NEVER return a value, AFAIK you need the fetch to get the data.


How it works the most flexible way:
- Define a variable containing your SQL statement (compose it by concatenating all constant and variable parts)
- Declare a cursor with this variable
- Open the Cursor
- Create a loop where you use Fetch Next from cursor into DS.
  Where DS is a datastructure containing all your fields.
  If you use "Select *", you can use a external defined DS using your file as external definition
- At the end of the loop do a close of the cursor.

If you like I can post you an example when I'm in the office tomorrow



0
 
LVL 27

Assisted Solution

by:tliotta
tliotta earned 100 total points
ID: 35102774
I'm aware of two general methods of returning SELECTed data into a RPG program with dynamic SQL. First is SQL CLI, which might be more than is wanted. Second is Murph's suggestion of FETCHing a prepared SELECT through a cursor. The Embedded SQL manual has an example -- Example: Dynamic SQL in an ILE RPG application that uses SQL.

If anyone can show an example of a different method, I'd be as interested as anyone else to see it.

Tom
0
 
LVL 16

Accepted Solution

by:
theo kouwenhoven earned 400 total points
ID: 35108314
Hi Rozamunda,

here a overview how to work with SQL with getting the result in a DS You can even prefix the ds so that the SQLed data has different field names then the original file (could be useful)

     D MyDs          E DS                  EXTNAME(MyFile)              
                                                                        
     C                   EVAL      SQLCMD = 'Select * from MyFile ' +   
     C                                       'where MyField = ' + Field1
     C                   ExSr      OpenSql                              
                                                                        
     C     SQLCOD        DOWEQ     0                                    
     C                   EXSR      ReadSql                              
     C                   ENDDO                                          
     C                   ExSr      CloseSql                             
                                                                        
     C     OpenSql       BegSr                                          
                                                                        
     C/Exec Sql                                                         
     C+ Prepare  SQLSLT   from :SQLCMD                               
     C/End-Exec                                                         
     C*                                                                 
     C/Exec Sql                                                         
     C+ Declare CC1  Cursor for  SQLSLT                                 
     C/End-Exec                    
     C*                            
     C/Exec Sql                    
     C+ Open  CC1                  
     C/End-Exec                    
     C                   ENDSR     
                                   
     C     ReadSql       BegSr     
                                   
     C/EXEC SQL                    
     C+ FETCH CC1 INTO :MyDs       
     C/END-EXEC                    
                                   
     C                   ENDSR     
                                   
     C     CloseSql      BegSr     
                                   
     C/Exec Sql                    
     C+ Close CC1                  
     C/End-Exec                    
                                    
     C                   ENDSR      

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need BRMS Recovery Analysis Report examples 7 201
OPNQRYF with numeric fields 4 231
IBM i - CL Array 5 92
iSeries email authority 4 25
In the hope of saving someone else's sanity... About a year ago we bought a Cisco 1921 router with two ADSL/VDSL EHWIC cards to load balance local network traffic over the two broadband lines we have, but we couldn't get the routing to work consi…
Cloud-based technologies and services will continue to grow in popularity in 2017 thanks to the simple, scalable and cost-effective solutions they deliver. Here are three areas where cloud adoption is poised to really take off.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now