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
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
1 Comment
Cade Roux
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.