Solved

retrieving value from dynamic SQL statament

Posted on 2011-03-09
5
486 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Dave Ford
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: 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

For anyone that has accidentally used newSID with Server 2008 R2 (like I did) and hasn't been able to get the server running again because you were unlucky (as I was) and had no backups - I was able to get things working by doing a Registry Hive rec…
This article outlines some of the reasons why an email message gets flagged as spam on a recipient's end.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

734 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