0
declare @amount float
declare @result char (20)

select @amount = cost from PurchaseDoc where id = 1

if @amount > 0 set @result = 'ok'
else set @result = 'empty'

print @result

2 Answers 2

2

Here is one representation of your script which can be executed against an Oracle database:

DECLARE
    amount    NUMBER;
    result    varchar2(20);
BEGIN
    SELECT SUM(cost) INTO amount
      from PurchaseDoc
     WHERE id = 1;

    if (amount > 0) 
    then
        result := 'ok';
    else 
        result := 'empty';      
    end if;

    dbms_output.put_line(result);    
END;
/

See this link for some useful information about dbms_output.
I recommend taking a look at some PL/SQL tutorials, such as the ones located at www.plsql-tutorial.com.

EDIT Updated select statement based on suggestion by Cade Roux

Sign up to request clarification or add additional context in comments.

1 Comment

I would change that to SUM(cost) for simplicity, because if no rows exist, you with get a NO_DATA_FOUND exception which will need to be handled in the transition from SQL to PL/SQL (SELECT is SQL, INTO variable is PL/SQL). You don't have this issue in T-SQL, where they are more tightly integrated.
0

There is really no need for PL/SQL here, a normal select statement with 'decode' function can do the trick.

SELECT DECODE(SUM(COST),0,'empty','ok') FROM PurchaseDoc where id = 1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.