473,320 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Dynamic SQL in DB2 procedure

Hi

i am getting the wrong result from the following code:
CREATE PROCEDURE DYNAMIC_QUERY ( )
DYNAMIC RESULT SETS 1
------------------------------------------------------------------------
-- SQL Stored Procedure
------------------------------------------------------------------------
P1: BEGIN
DECLARE V_STR VARCHAR(2000);
DECLARE V_TXN_CDE VARCHAR(4);

SET V_STR='';

FOR V_CUR AS C1 CURSOR WITH HOLD FOR SELECT TXN_PFE_TXN_CDE FROM D_REF_TXN_CDE WHERE TXN_TYP_CDE='CSH'
DO
SET V_STR=V_STR||''''||V_CUR.TXN_PFE_TXN_CDE||''''||', ';
END FOR;

SET V_STR=SUBSTR(V_STR, 1, LENGTH(V_STR)-1);
IF V_TXN_CDE IN (V_STR)
THEN
INSERT INTO TEST4 VALUES (3,'CASH TRANSACTION');
ELSE
INSERT INTO TEST4 VALUES (3,'NOT CASH TRANSACTION');
END IF;
COMMIT;
END P1
IF V_TXN_CDE IN (V_STR) IS GIVING WRONG RESULT

BUT WHEN I CHECK THE VALUE OF V_STR IT IS :
'2200','2201','2202','2203','2204','2205','2206',' 2207','2208','2209','2210','2211','2212','2213','2 214','2215','2216','2260','2261','2270','2271','33 60','3361','3370','3371','4200','4300','4201','425 8'

------------------------------------
NOW WHEN I RUN THE SAME CODE BY REPLACING V_STR I GET THE CORRECT RESULT

CREATE PROCEDURE DYNAMIC_QUERY ( )
DYNAMIC RESULT SETS 1
------------------------------------------------------------------------
-- SQL Stored Procedure
------------------------------------------------------------------------
P1: BEGIN
DECLARE V_STR VARCHAR(2000);
DECLARE V_TXN_CDE VARCHAR(4);

SET V_STR='';

FOR V_CUR AS C1 CURSOR WITH HOLD FOR SELECT TXN_PFE_TXN_CDE FROM D_REF_TXN_CDE WHERE TXN_TYP_CDE='CSH'
DO
SET V_STR=V_STR||''''||V_CUR.TXN_PFE_TXN_CDE||''''||', ';
END FOR;

SET V_STR=SUBSTR(V_STR, 1, LENGTH(V_STR)-1);
IF V_TXN_CDE IN ('2200','2201','2202','2203','2204','2205','2206', '2207','2208','2209','2210','2211','2212','2213',' 2214','2215','2216','2260','2261','2270','2271','3 360','3361','3370','3371','4200','4300','4201','42 58')
THEN
INSERT INTO TEST4 VALUES (3,'CASH TRANSACTION');
ELSE
INSERT INTO TEST4 VALUES (3,'NOT CASH TRANSACTION');
END IF;
COMMIT;
END P1

How to overcome this problem?

Regards
Bitul
Jun 12 '08 #1
0 1199

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: traceable1 | last post by:
I am trying to create a stored procedure with dynamic sql referencing the V$SESSION table (view). I need to use this dynamically, because the procedure will not compile if the user does not have...
1
by: Guinness Mann | last post by:
When you guys talk about "dynamic SQL," to what exactly are you referring? Is dynamic SQL anything that isn't a stored procedure? Specifically, I use ASP.NET to communicate with my SQL Server...
4
by: Tim.D | last post by:
People, I've ventured into the wonderful world of Stored Procedures. My first experience has been relatively successful however I am stuck on using host variables to specifiy actualy table or...
1
by: Todd Peterson | last post by:
I'm a newbie to DB2 and am trying to figure out how to write a stored procedure, using dynamic SQL statements to return a result set. I believe the majority of the hurdles I have been facing might...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.