Welcome to Dream.In.Code
Getting Help is Easy!

Join 107,710 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,112 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Am I just dense about Oracle PL/SQL IF statements?

 
Reply to this topicStart new topic

Am I just dense about Oracle PL/SQL IF statements?

Chubber
post 8 May, 2008 - 11:43 AM
Post #1


D.I.C Head

Group Icon
Joined: 16 Oct, 2006
Posts: 118



Dream Kudos: 225
My Contributions


I am mostly a SQL Server guy. But I have used Oracle for years, on and off.

Today I am trying to write an Oracle Procedure. It is a simple enough one, I could do it in SQL Server in about 5 lines. It simply either creates a new record in a table or updates one and returns the PK in either case.

This is Oracle 10g.

One thing that I have read a lot of documentation about is the IF THEN ELSE statement. It won't seem to compile any procedure that contains an IF statement with a compound condition or more than one statement in the TRUE or FALSE parts.

So,

IF BoolVar THEN
X := 1;
ELSE
X := 2;
END IF;

works just fine. But if I want to write:

IF BoolVar1 AND BoolVar2 THEN

or (heavens)
IF (A IS NULL) OR (B IS NULL) THEN

it won't take it. Should it?

Also, I can't seem to do this:
IF BoolVar THEN
X := 1;
UPDATE table SET Y=X WHERE Y=1;
Z := 2;
ELSE
X := 2;
END IF;

How can I run more than one statement inside the IF statement?

Not to mention that the compiler kicks back useless twaddle when the syntax isn't right.

Can you tell I am frustrated? I have been trying this for 4 hours on something that I could do in SQL Server in 1 minute.
User is offlineProfile CardPM

Go to the top of the page


Chubber
post 8 May, 2008 - 11:57 AM
Post #2


D.I.C Head

Group Icon
Joined: 16 Oct, 2006
Posts: 118



Dream Kudos: 225
My Contributions


I guess it would make sense to show you what I have so far.:

CODE

drop procedure updateintoconsumers_sp;

CREATE OR REPLACE PROCEDURE updateintoconsumers_sp
    (consumer_id_in IN NUMBER default NULL,
    consumer_email_in IN VARCHAR2 default NULL,
    source_ip_in IN VARCHAR2 default NULL,
    host_name_in IN VARCHAR2 default NULL,
    initial_offer_id_in IN NUMBER default NULL,
    consumer_id_out OUT NUMBER default NULL)
AS

bConsumerIDSet BOOLEAN := FALSE;
iConsumerCount NUMBER := 0;


BEGIN
    IF consumer_id_in IS NOT NULL THEN
        IF consumer_id_in > 0 THEN
            bConsumerIDSet := TRUE;
        ELSE
            bConsumerIDSet := FALSE;
        END IF;
    ELSE
        bConsumerIDSet := FALSE;
    END IF;
    
    /* Check if the consumer already exists and either update it or insert it (consumer_id_in IS NOT NULL) AND (consumer_id_in >= 0) */
    IF bConsumerIDSet THEN
        iConsumerCount := (SELECT COUNT(*) FROM consumers WHERE consumer_id = consumer_id_in);
        
        IF iConsumerCount > 0 THEN
            /* Update */
            UPDATE consumers SET consumer_email = consumer_email_in, source_ip = source_ip_in, host_name=host_name_in
            WHERE consumer_id = consumer_id_in;
            
            consumer_id_out := consumer_id_in;
        ELSE
            /* Insert */
            INSERT INTO consumers ( consumer_id, ip_address, host_name, initial_offer_id, create_date, consumer_email )
            VALUES ( consumers_seq.nextval, ip_address_in, host_name_in, initial_offer_id_in, SYSDATE, consumer_email_in )
            RETURNING consumer_id INTO consumer_id_out;
        END IF;
    ELSE
        /* Try again with the email address */
        iConsumerCount := (SELECT COUNT(*) FROM consumers WHERE consumer_email = consumer_email_in);
        IF iConsumerCount > 0 THEN
            /* Update */
            UPDATE consumers SET source_ip = source_ip_in, host_name=host_name_in
            WHERE consumer_email = consumer_email_in;
            consumer_id_out := (SELECT consumer_id FROM consumers WHERE consumer_email = consumer_email_in);
        ELSE
            /* Insert */
            INSERT INTO consumers ( consumer_id, ip_address, host_name, initial_offer_id, create_date, consumer_email )
            VALUES ( consumers_seq.nextval, ip_address_in, host_name_in, initial_offer_id_in, SYSDATE, consumer_email_in )
            RETURNING consumer_id INTO consumer_id_out;
        END IF;
    END IF;

EXCEPTION
  WHEN OTHERS THEN
    consumer_id_out := NULL;

END updateintoconsumers_sp;
    
    
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/30/08 03:13AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month