Thanks Birgitta!
I was afraid of this.
Steve
-----Original Message-----
From: midrange-l-bounces@xxxxxxxxxxxx [mailto:midrange-l-bounces@xxxxxxxxxxxx] On Behalf Of Birgitta Hauser
Sent: Thursday, June 21, 2012 2:16 PM
To: 'Midrange Systems Technical Discussion'
Subject: AW: Embedded SQL using IN and a host variable
Hi,
You can't do it in this way!
There are 3 options:
1. pass a host variable for each parameter specified in the IN predicate
2. use dynamic SQL
3. Try something strange like this:
/Free
//Build your string containing all elements separated by a comma
Path = 'Data1' + ', ' + 'Data2' + ', '+ ...;
Exec SQL
Declare get cursor with return to client for
SELECT distinct Column1, Column2, Column3
FROM Table
WHERE :Path like '%' concat Trim(Column3) concat ', ' concat
'%';
... etc
I didn't try it, but it should work and I'm not sure about the performance.
Mit freundlichen Grüßen / Best regards
Birgitta Hauser
"Shoot for the moon, even if you miss, you'll land among the stars." (Les
Brown)
"If you think education is expensive, try ignorance." (Derek Bok)
"What is worse than training your staff and losing them? Not training them
and keeping them!"
-----Ursprüngliche Nachricht-----
Von: midrange-l-bounces@xxxxxxxxxxxx
[mailto:midrange-l-bounces@xxxxxxxxxxxx] Im Auftrag von Needles,Stephen J
Gesendet: Thursday, 21.6 2012 20:33
An: midrange-l@xxxxxxxxxxxx
Betreff: Embedded SQL using IN and a host variable
I have a need to accept a list of values that needs to be compared to a
column in a table using an embedded SQL statement in RPGLE.
The list:
'data1'
'data2'
This list is assembled so that path looks like:
'data1','data2'
When used in the SQL statement...
Select stuff from table where column3 in (:path);
I end up with an SQLCod = 100.
Running the SQL outside of the program produces the needed data.
Select * from table where column3 in ('data1','data2');
Is there a solution short of repeating the condition over and over again?
Using STRIP and TRIM with the host variable had no impact.
Stub of Code below:
dpath s 9999
dcolumn1 s 5
dcolumn2 s 3
dcolumn3 s 9999
/free
eval path = '''';
eval path = %trim(path) +
'data1' + '''';
eval path = %trim(path) + ',' + '''' +
'data2' + '''';
exec sql
declare get cursor with return to client for
SELECT distinct
Column1, Column2, Column3 FROM Table WHERE
Column3 in (:path);
exec sql open get;
exec sql fetch get into :Column1, :Column2, :Column3;
if sqlcod = sqlcod;
eval sqlcod = sqlcod;
endif;
/end-free
============================================================================
==
This communication, including attachments, is confidential, may be subject
to legal privileges, and is intended for the sole use of the addressee. Any
use, duplication, disclosure or dissemination of this communication, other
than by the addressee, is prohibited. If you have received this
communication in error, please notify the sender immediately and delete or
destroy this communication and all copies.
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list
To post a message email: MIDRANGE-L@xxxxxxxxxxxx To subscribe, unsubscribe,
or change list options,
visit:
http://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx Before posting, please take a
moment to review the archives at
http://archive.midrange.com/midrange-l.
As an Amazon Associate we earn from qualifying purchases.