Solved

retrieving value from dynamic SQL statament

Posted on 2011-03-09
5
454 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
What is Backup? Backup software creates one or more copies of the data on your digital devices in case your original data is lost or damaged. Different backup solutions protect different kinds of data and different combinations of devices. For e…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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