Home > Ask the AS/400 Experts > DB2/400 and DB2 UDB Questions & Answers > SQL server error message -321
Ask The iSeries 400 Expert: Questions & Answers
EMAIL THIS

SQL server error message -321

Kent Milligan EXPERT RESPONSE FROM: Kent Milligan

Pose a Question
Other iSeries 400 Categories
Meet all iSeries 400 Experts
Become an Expert for this site


iSeries news and advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 16 March 2009
I'm trying to run a query as below from an SQLRPGLE program, but I am receiving a SQL server error message: SQL error -312 (variable cannot be used). The code is as below:
d  mycount     s      10p  0 
  
c     eval string = 'select count(*) into : mycount from MyTable' 
 * 
c/exec-sql 
c+ prepare S1 from string 
c/end-sql 
 * 
c/exec-sql 
c+ execute S1 
c/end-sql
How do I overcome this error and execute the code dynamically?

>
If you are on IBM iSeries 6.1, then you can you can use the new values statement as I demonstrate below with SQL pocedural language:
  DECLARE v1 int; 
  SET string1= 'VALUES(SELECT COUNT(*)  from MyTable) INTO ?'; 

  PREPARE s1 from string1; 
  EXECUTE s1 using v1; 
The solution is more complicated if you are on a release prior to 6.1 since you need to declare a cursor using dynamic SQL.
DECLARE C1 CURSOR FOR S1 ; 

  SET STRING2 = 'SELECT COUNT(*) FROM MyTable' ; 
  PREPARE S1 FROM STRING2 ; 
  OPEN C1 ; 
  FETCH C1 INTO V1 ; 
 


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



RELATED CONTENT
DB2/400 and DB2 UDB
When is the YES option for 'reuse deleted files' function the best choice?
Monitoring members 'stuck' within a physical file on an EDI system
Creating a host variable of the 'where in' statement in SQL
Choose which column names are returned via ODBC when working with DB2 files
Developing tables in a parent-child relationship in DB2
Allow access to data from a stored procedure result set using COBOL or RPG
Access path and an open data path differences
Convert a numeric physical file to a character in SQL without leading zeros
Inserting data from a CTE into a file in SQL
Implement variables in SQL when creating an alias

DB2 UDB (universal databases)
Oracle boasts 11g on SPARC is faster than IBM DB2 on Power 595
When is the YES option for 'reuse deleted files' function the best choice?
Monitoring members 'stuck' within a physical file on an EDI system
Creating a host variable of the 'where in' statement in SQL
Choose which column names are returned via ODBC when working with DB2 files
Developing tables in a parent-child relationship in DB2
Access path and an open data path differences
Database performance comparisons on IBM i
Implement variables in SQL when creating an alias
Simplify the process of converting a spool file from iSeries into an Excel spreadsheet
DB2 UDB (universal databases) Research

iSeries SQL commands and statements
Enhancing RPG with external SQL stored procedures
Tracking data changes on IBM i with triggers
Introduction to SQLRPGLE on IBM i: Making a report
Making the most of RPG data handling on IBM i
When is the YES option for 'reuse deleted files' function the best choice?
Monitoring members 'stuck' within a physical file on an EDI system
Choose which column names are returned via ODBC when working with DB2 files
Creating a host variable of the 'where in' statement in SQL
Convert a numeric physical file to a character in SQL without leading zeros
Inserting data from a CTE into a file in SQL
iSeries SQL commands and statements Research

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



iSeries Networking - Printing, Remote Access, TCP/IP
HomeNewsTopicsITKnowledge ExchangeTipsBlogsAsk the ExpertsMultimediaWhite PapersProducts
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts