135

Below is an example of using variables in SQL Server 2000.

DECLARE @EmpIDVar INT

SET @EmpIDVar = 1234

SELECT *
FROM Employees
WHERE EmployeeID = @EmpIDVar

I want to do the exact same thing in Oracle using SQL Developer without additional complexity. It seems like a very simple thing to do, but I can't find a simple solution. How can I do it?

Adam Musch
  • 13,286
  • 2
  • 28
  • 32
Nathan
  • 1,351
  • 2
  • 9
  • 3
  • As a stored procedure or as a script? If you're hardcoding the value for EmpIDVar, why use a variable at all? – kurosch Apr 13 '11 at 18:03

10 Answers10

111

I am using the SQL-Developer in Version 3.2. The other stuff didn't work for me, but this did:

define value1 = 'sysdate'

SELECT &&value1 from dual;

Also it's the slickest way presented here, yet.

(If you omit the "define"-part you'll be prompted for that value)

Omphaloskopie
  • 1,952
  • 1
  • 14
  • 24
  • 15
    If comparing &&value1 to a string value like: &&value1 = 'Some string' then &&value1 needs to be wrapped in single quotes like: '&&value1' = 'Some string' – Ryan E Mar 05 '14 at 21:29
  • 1
    In SQL Developer, substitution variables defined by DEFINE seem to be persistent between query executions. If I change the variable value, but do not explicitly highlight the DEFINE line when executing, the previous value remains. (Is this because of the double && ?) – Baodad May 13 '14 at 23:00
  • 10
    [This page](http://www.oracle.com/technetwork/testcontent/sub-var2-099853.html) in section 2.4 talks about the difference between the single ampersand (&) and double ampersand (&&) – Baodad May 13 '14 at 23:03
  • 1
    For those of us used to working with queries with variables in Microsoft SQL Server Management Studio, this is the best answer. We may need to get used to highlighting the whole query before executing, though. – Baodad May 13 '14 at 23:07
  • 1
    This answer was the solution that worked for me in SQL-Developer 2.1. It's definitely the most straight-forward method to implement a way for the user to change a value above the query and not have to edit the query itself. – YonkeyDonk64 Jul 17 '14 at 15:43
89

There are two types of variable in SQL-plus: substitution and bind.

This is substitution (substitution variables can replace SQL*Plus command options or other hard-coded text):

define a = 1;
select &a from dual;
undefine a;

This is bind (bind variables store data values for SQL and PL/SQL statements executed in the RDBMS; they can hold single values or complete result sets):

var x number;
exec :x := 10;
select :x from dual;
exec select count(*) into :x from dual;
exec print x;

SQL Developer supports substitution variables, but when you execute a query with bind :var syntax you are prompted for the binding (in a dialog box).

Reference:

UPDATE substitution variables are a bit tricky to use, look:

define phone = '+38097666666';
select &phone from dual; -- plus is stripped as it is a number
select '&phone' from dual; -- plus is preserved as it is a string
gavenkoa
  • 45,285
  • 19
  • 251
  • 303
  • 3
    I tried the bind in SQL Developer (4.1.1.19) and it is working too. I mean the case with `var x` and `exec :x`, no prompt. – Betlista May 12 '16 at 06:59
53

In SQL*Plus, you can do something very similar

SQL> variable v_emp_id number;
SQL> select 1234 into :v_emp_id from dual;

      1234
----------
      1234

SQL> select *
  2    from emp
  3   where empno = :v_emp_id;

no rows selected

In SQL Developer, if you run a statement that has any number of bind variables (prefixed with a colon), you'll be prompted to enter values. As Alex points out, you can also do something similar using the "Run Script" function (F5) with the alternate EXEC syntax Alex suggests does.

variable v_count number;
variable v_emp_id number;
exec :v_emp_id := 1234;
exec select count(1) into :v_count from emp;
select *
  from emp
 where empno = :v_emp_id
exec print :v_count;
Community
  • 1
  • 1
Justin Cave
  • 227,342
  • 24
  • 367
  • 384
  • 14
    if you 'run script' (F5) instead of 'run statement' then it won't prompt for the bind variables. But it doesn't seem to like the `select...into` (ORA-01006), so you'd need to do `exec :v_emp_id := 1234;` instead. – Alex Poole Apr 13 '11 at 18:37
  • @Alex - Nice! I just gave up and assumed that SQL Developer was always prompting for bind variable values. I incorporated your suggestions into my answer. – Justin Cave Apr 13 '11 at 19:00
  • 1
    @Nathan if you're looking to execute an package with :v_emp_id, you can use bind variables for the refcursors as well. See the bottom of Alex's [answer](http://stackoverflow.com/questions/3526798/best-way-tool-to-get-the-results-from-an-oracle-package-procedure/3527037#3527037) to a similar question I had – Conrad Frix Apr 13 '11 at 19:11
  • 2
    @AlexPoole is there a way to send the output to the Query Result window? In my job I run queries to export to Excel files. – tp9 Jun 09 '12 at 01:18
20

Ok I know this a bit of a hack but this is a way to use a variable in a simple query, not a script:

WITH
    emplVar AS
    (SELECT 1234 AS id FROM dual)
SELECT
    *
FROM
    employees,
    emplVar
WHERE
    EmployId=emplVar.id;

You get to run it everywhere.

zpontikas
  • 5,445
  • 2
  • 37
  • 41
13

Simple answer NO.

However you can achieve something similar by running the following version using bind variables:

SELECT * FROM Employees WHERE EmployeeID = :EmpIDVar 

Once you run the query above in SQL Developer you will be prompted to enter value for the bind variable EmployeeID.

Chandu
  • 81,493
  • 19
  • 133
  • 134
8

You can read up elsewhere on substitution variables; they're quite handy in SQL Developer. But I have fits trying to use bind variables in SQL Developer. This is what I do:

SET SERVEROUTPUT ON
declare
  v_testnum number;
  v_teststring varchar2(1000);

begin
   v_testnum := 2;
   DBMS_OUTPUT.put_line('v_testnum is now ' || v_testnum);

   SELECT 36,'hello world'
   INTO v_testnum, v_teststring
   from dual;

   DBMS_OUTPUT.put_line('v_testnum is now ' || v_testnum);
   DBMS_OUTPUT.put_line('v_teststring is ' || v_teststring);
end;

SET SERVEROUTPUT ON makes it so text can be printed to the script output console.

I believe what we're doing here is officially called PL/SQL. We have left the pure SQL land and are using a different engine in Oracle. You see the SELECT above? In PL/SQL you always have to SELECT ... INTO either variable or a refcursor. You can't just SELECT and return a result set in PL/SQL.

Baodad
  • 2,405
  • 2
  • 38
  • 39
8

I think that the Easiest way in your case is :

DEFINE EmpIDVar = 1234;

SELECT *
FROM Employees
WHERE EmployeeID = &EmpIDVar

For the string values it will be like :

DEFINE EmpIDVar = '1234';

SELECT *
FROM Employees
WHERE EmployeeID = '&EmpIDVar'
Peter
  • 162
  • 3
  • 11
  • Verified, It's working in ORACLE SQL Developer 20.2.0.175 Build 175.1842 version. I tested it with my table: define usr = 'BT'; select * from department where created_by = '&usr'; – Ortsbo Oct 24 '20 at 05:57
2

In sql developer define properties by default "ON". If it is "OFF" any case, use below steps.

set define on; define batchNo='123'; update TABLE_NAME SET IND1 = 'Y', IND2 = 'Y' WHERE BATCH_NO = '&batchNo';

Md. Kamruzzaman
  • 1,895
  • 16
  • 26
1

Use the next query:

DECLARE 
  EmpIDVar INT;

BEGIN
  EmpIDVar := 1234;

  SELECT *
  FROM Employees
  WHERE EmployeeID = EmpIDVar;
END;
Ivan Gerasimenko
  • 2,381
  • 3
  • 30
  • 46
0

Try this it will work, it's better create a procedure, if procedure is not possible you can use this script.

with param AS(
SELECT 1234 empid
FROM dual)
 SELECT *
  FROM Employees, param
  WHERE EmployeeID = param.empid;
END;
Dwhitz
  • 1,250
  • 7
  • 26
  • 38