如何在 Oracle SQL Developer 中使用变量?

发布于 2024-11-01 06:39:27 字数 254 浏览 6 评论 0原文

下面是在 SQL Server 2000 中使用变量的示例。

DECLARE @EmpIDVar INT

SET @EmpIDVar = 1234

SELECT *
FROM Employees
WHERE EmployeeID = @EmpIDVar

我想使用 SQL Developer 在 Oracle 中执行完全相同的操作,而不需要额外的复杂性。这似乎是一件非常简单的事情,但我找不到简单的解决方案。我该怎么做呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

别靠近我心 2024-11-08 06:39:27

我使用的是 3.2 版的 SQL-Developer。其他的东西对我不起作用,但这个有用:

define value1 = 'sysdate'

SELECT &&value1 from dual;

而且它也是这里介绍的最巧妙的方式。

(如果您省略“定义”部分,系统将提示您输入该值)

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)

浮光之海 2024-11-08 06:39:27

SQL-plus中有两种类型的变量:替换和绑定。

这是替换(替换变量可以替换 SQL*Plus 命令选项或其他硬编码文本):

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

这是绑定(绑定变量存储 SQL 和 PL/SQL 的数据值)在 RDBMS 中执行的语句;它们可以保存单个值或完整的结果集):

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

SQL Developer 支持替换变量,但是当您使用绑定 :var 语法执行查询时,系统会提示您进行绑定(在对话框)。

参考:

更新 替换变量使用起来有点棘手,请看:

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

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
笔落惊风雨 2024-11-08 06:39:27

在 SQL*Plus 中,您可以执行非常类似的操作

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

。在 SQL Developer 中,如果您运行具有任意数量的绑定变量(以冒号为前缀)的语句,系统将提示您输入值。正如 Alex 指出的那样,您还可以使用“运行脚本”功能 (F5) 以及 Alex 建议的备用 EXEC 语法来执行类似的操作。

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;

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;
戒ㄋ 2024-11-08 06:39:27

好吧,我知道这有点黑客,但这是一种在简单查询中使用变量的方法,而不是脚本:

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

您可以在任何地方运行它。

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.

墨小沫ゞ 2024-11-08 06:39:27

简单回答:不。

但是,您可以通过使用绑定变量运行以下版本来实现类似的效果:

SELECT * FROM Employees WHERE EmployeeID = :EmpIDVar 

在 SQL Developer 中运行上面的查询后,系统将提示您输入绑定变量 EmployeeID 的值。

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.

风月客 2024-11-08 06:39:27

我认为您的情况最简单的方法是:

DEFINE EmpIDVar = 1234;

SELECT *
FROM Employees
WHERE EmployeeID = &EmpIDVar

对于字符串值,它会像:

DEFINE EmpIDVar = '1234';

SELECT *
FROM Employees
WHERE EmployeeID = '&EmpIDVar'

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'
九公里浅绿 2024-11-08 06:39:27

您可以在其他地方阅读替代变量;它们在 SQL Developer 中非常方便。但我在尝试在 SQL Developer 中使用绑定变量时遇到了困难。这就是我所做的:

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 使得文本可以打印到脚本输出控制台。

我相信我们在这里所做的正式名称为 PL/SQL。我们已经离开了纯粹的 SQL 领域,并在 Oracle 中使用了不同的引擎。您看到上面的SELECT了吗?在 PL/SQL 中,您始终必须SELECT ... INTO变量或引用游标。您不能仅在 PL/SQL 中SELECT 并返回结果集。

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.

记忆で 2024-11-08 06:39:27

在 sql Developer 中定义属性默认为“ON”。如果在任何情况下它都是“关闭”,请使用以下步骤。

<代码>
设置定义为;
定义batchNo='123';
更新 TABLE_NAME SET IND1 = 'Y', IND2 = 'Y' WHERE BATCH_NO = '&batchNo';

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

匿名。 2024-11-08 06:39:27

使用下一个查询:

DECLARE 
  EmpIDVar INT;

BEGIN
  EmpIDVar := 1234;

  SELECT *
  FROM Employees
  WHERE EmployeeID = EmpIDVar;
END;

Use the next query:

DECLARE 
  EmpIDVar INT;

BEGIN
  EmpIDVar := 1234;

  SELECT *
  FROM Employees
  WHERE EmployeeID = EmpIDVar;
END;
街道布景 2024-11-08 06:39:27

尝试一下它会起作用,最好创建一个过程,如果过程不可能,您可以使用此脚本。

with param AS(
SELECT 1234 empid
FROM dual)
 SELECT *
  FROM Employees, param
  WHERE EmployeeID = param.empid;
END;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文