1

I am using oracle SQL developer and am trying to run a query with an if statement in it.

I have defined a variable and assigned a value to it.

define
var buyer varchar2(1)

/* define variable values */ 
exec :buyer := 'Yep'

if :buyer = 'Yep' then 
begin
dbms_output.put_line('Yep');
else 
dbms_output.put_line('Nope');
end;

However, when I run the script, I get an error message stating that IF is an unknown command.

Error starting at line : 8 in command -
if :buyer = 'Yep' then
Error report -
Unknown Command

Any advice is greatly appreciated.

1
  • 1
    Because you can't use if outside of a PL/SQL block. Commented Dec 4, 2014 at 15:46

2 Answers 2

1

You have to define a DECLARE-BEGIN-END block to run IF-ELSIF-ELSE-END conditions. Based on your sample code, I'm assuming this is what you would like to do.

DECLARE
  buyer VARCHAR2(3) := 'Yep';
BEGIN
  IF ( buyer = 'Yep' ) THEN
    dbms_output.put_line('Yep' );
  ELSE
    dbms_output.put_line('Nope');
  END IF;
END;
/

You could run this as a script in sql developer. HTH!

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

Comments

1

Look up the PLSQL Syntax:

DECLARE
  variable type;
BEGIN
  IF variable = 'test'
  THEN
    DBMS_OUTPUT.PUT_LINE('J');
  ELSE
    DBMS_OUTPUT.PUT_LINE('N');
  END IF;
END;
/

1 Comment

Thanks for the help both. That's cleared that up... :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.