Solved

retrieving value from dynamic SQL statament

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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 ?
Many businesses neglect disaster recovery and treat it as an after-thought. I can tell you first hand that data will be lost, hard drives die, servers will be hacked, and careless (or malicious) employees can ruin your data.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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