3

In ORACLE PL/SQL DEVELOPER, am I able to do the following as I could easily do in SQL Server?

DECLARE @EmpIDVar INT

SET @EmpIDVar = 1234

SELECT *
FROM Employees
WHERE EmployeeID = @EmpIDVar

This seems like such a simple thing and yet it seems impossible to do in Oracle!

I know about using & in front of variables that will prompt me to enter their values but why can't I just do something like the above?

3 Answers 3

0

Declare Using VARIABLE command

VARIABLE EmpIDVar NUMBER;

Set the value Using EXEC like this

EXEC :EmpIDVar := 1234;

Run the Query prefixing the variable with colon

SELECT *
FROM Employees
WHERE EmployeeID = :EmpIDVar;
Sign up to request clarification or add additional context in comments.

3 Comments

@user3359423 It works with SQL*Plus.. For PL/SQL Developer, you can try an answer here
Tried the other answer and I couldn't get it to work. It seems that the only way you can do this in PL SQL Developer is to just use substitution variables. ie. put a &<param_name> in place of where you put your variable and PL/SQL developer will substitute for you for e.g. select * from employees where EmployeeId = &EmployeeId
Yes, but that is not executed as bind variable.. it is just like a macro. String is formed dynamically.
0

Using Variable and select with IN clause, perhaps is more useful.

VARIABLE EmpIDVar NUMBER
DEFINE EmpIDVar ="123, 124"
EXEC : EmpIDVar := '& EmpIDVar'

SELECT 
  E.*
FROM Employees E
WHERE EmployeeID IN ( & EmpIDVar )

If not, simple replace IN with "=" and "123, 124" with "123".

Comments

0

Hi here you can see another answer

How to use variables in an Oracle PL/SQL where clause

 set serveroutput on

 DECLARE 
    EmpIDVar integer; 
    Result integer;

  BEGIN
     EmpIDVar := 2000;


     SELECT Employees_id into Result      
     FROM Employees 
     WHERE Employees_ID = EmpIDVar ;

     dbms_output.Put_line(Result); 
  end;

if you wanted to show all(*) you would need, to show all variables , so like i said you need as much varialbes as you fields have.

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.