ChatGPT解决这个技术问题 Extra ChatGPT

How do I use variables in Oracle SQL Developer?

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?

As a stored procedure or as a script? If you're hardcoding the value for EmpIDVar, why use a variable at all?

O
Omphaloskopie

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)


If comparing &&value1 to a string value like: &&value1 = 'Some string' then &&value1 needs to be wrapped in single quotes like: '&&value1' = 'Some string'
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 && ?)
This page in section 2.4 talks about the difference between the single ampersand (&) and double ampersand (&&)
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.
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.
g
gavenkoa

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:

http://www.oracle.com/technetwork/testcontent/sub-var-087723.html SQL*Plus Substitution Variables, Christopher Jones, 2004

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

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.
C
Community

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;

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 - Nice! I just gave up and assumed that SQL Developer was always prompting for bind variable values. I incorporated your suggestions into my answer.
@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 to a similar question I had
@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.
z
zpontikas

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.


Is there a way to use this with UPDATE instead of select?
C
Chandu

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.


B
Baodad

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.


P
Peter

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'

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';
I
Ivan Gerasimenko

Use the next query:

DECLARE 
  EmpIDVar INT;

BEGIN
  EmpIDVar := 1234;

  SELECT *
  FROM Employees
  WHERE EmployeeID = EmpIDVar;
END;

error returned "PLS-00428 an INTO clause is expected in this SELECT statement"
M
Md. Kamruzzaman

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';


This is the easiest answer that worked for me. Thanks.
D
Dwhitz

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;