If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Execute and conditional statements

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-21-11, 12:00
ricci ricci is offline
Registered User
 
Join Date: Sep 2011
Posts: 39
Execute and conditional statements

Hi everyone.

I'm trying to make a conditional statement inside a execute stayment, Is this posible??. I put what i'm trying to do, thanks for your help.


Code:
 
DECLARE VLI_CR1REG, VLI_CR2REG, VLV_CR1COMP, VLV_CR2COMP VARCHAR(5);

SET VLV_QUERY = ('BEGIN ATOMIC  DECLARE VLV_QUERY VARCHAR(1000);' ||
'IF ' || VLI_CR2REG || ' ' || VLV_CR1COMP || ' ' || VLI_CR1REG ||' AND ' || VLI_CR1REG ||' ' || VLV_CR2COMP || ' ' ||VLI_CR2REG ||' THEN ' ||
'INSERT INTO SESSION.TEMP_REGLASNUME VALUES(0);'||
'ELSE ' || 
'INSERT INTO SESSION.TEMP_REGLASNUME VALUES(1);' ||
'END IF; END;');
EXECUTE IMMEDIATE VLV_QUERY;

Last edited by ricci; 12-21-11 at 12:10.
Reply With Quote
  #2 (permalink)  
Old 12-21-11, 13:11
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
why not have your if statement set the value for the insert rather than a literal?
Reply With Quote
  #3 (permalink)  
Old 12-21-11, 13:22
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by ricci View Post
Is this posible??.
I think the easiest way to figure that out is to try, no? Your particular statement will likely fail, because it contains a delimiter (the semicolon at the end), but generally speaking this is certainly possible.
Reply With Quote
  #4 (permalink)  
Old 12-21-11, 13:45
ricci ricci is offline
Registered User
 
Join Date: Sep 2011
Posts: 39
Hi dav1mo.

I canīt do beacause is dynamic, I mean I can inserte any kind of commparison it's a rule game.

Hi n_i.

I've try it but the matter is what you said, de semicolon. I've try to run it but the same error, because find a semicolon and take this as a limiter but if I don't use it it's like is needing it.

Best regards.
Reply With Quote
  #5 (permalink)  
Old 12-21-11, 20:41
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
Originally Posted by ricci View Post
Hi everyone.

I'm trying to make a conditional statement inside a execute stayment, Is this posible??. I put what i'm trying to do, thanks for your help.


Code:
 
DECLARE VLI_CR1REG, VLI_CR2REG, VLV_CR1COMP, VLV_CR2COMP VARCHAR(5);

SET VLV_QUERY = ('BEGIN ATOMIC  DECLARE VLV_QUERY VARCHAR(1000);' ||
'IF ' || VLI_CR2REG || ' ' || VLV_CR1COMP || ' ' || VLI_CR1REG ||' AND ' || VLI_CR1REG ||' ' || VLV_CR2COMP || ' ' ||VLI_CR2REG ||' THEN ' ||
'INSERT INTO SESSION.TEMP_REGLASNUME VALUES(0);'||
'ELSE ' || 
'INSERT INTO SESSION.TEMP_REGLASNUME VALUES(1);' ||
'END IF; END;');
EXECUTE IMMEDIATE VLV_QUERY;
(1) Why did you declared VLV_QUERY inside set the value to it?

(2) I thought that combining two insert statement into one was possible and better, like
Code:
DECLARE VLV_QUERY VARCHAR(1000);

SET VLV_QUERY
    = 'INSERT INTO SESSION.TEMP_REGLASNUME VALUES('
      || ' CASE WHEN ' || VLI_CR2REG || ' ' || VLV_CR1COMP || ' ' || VLI_CR1REG
           || '  AND ' || VLI_CR1REG || ' ' || VLV_CR2COMP || ' ' || VLI_CR2REG
           || ' THEN 0 ELSE 1 END )';
(3) SESSION.TEMP_REGLASNUME looks one column temporary table.
And you inserted 0 or 1.
Why did you used temporary table rather than a declared variable?

If you want pass the value to another stored procedure,
you can pass the value as a parameter for the procedure.

And I thought that passing as a parameter is better for debugging/maintenance,
because it make clear the procedures using the value.
If passed as a temporary table,
you might influence unexpectedly to a procedure which is using the value outside your knowledge,
at the time of modifying this procedure.

Last edited by tonkuma; 12-21-11 at 20:56.
Reply With Quote
  #6 (permalink)  
Old 12-22-11, 11:32
ricci ricci is offline
Registered User
 
Join Date: Sep 2011
Posts: 39
Thanks

Hi tonkuma.

I'm grateful for your help, It works, it's exactly what i needed.

Thanks a lot.

Best regards.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On