如何在 Oracle SQL Developer 中使用变量?
下面是在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我使用的是 3.2 版的 SQL-Developer。其他的东西对我不起作用,但这个有用:
而且它也是这里介绍的最巧妙的方式。
(如果您省略“定义”部分,系统将提示您输入该值)
I am using the SQL-Developer in Version 3.2. The other stuff didn't work for me, but this did:
Also it's the slickest way presented here, yet.
(If you omit the "define"-part you'll be prompted for that value)
SQL-plus中有两种类型的变量:替换和绑定。
这是替换(替换变量可以替换 SQL*Plus 命令选项或其他硬编码文本):
这是绑定(绑定变量存储 SQL 和 PL/SQL 的数据值)在 RDBMS 中执行的语句;它们可以保存单个值或完整的结果集):
SQL Developer 支持替换变量,但是当您使用绑定
:var
语法执行查询时,系统会提示您进行绑定(在对话框)。参考:
更新 替换变量使用起来有点棘手,请看:
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):
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):
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:
在 SQL*Plus 中,您可以执行非常类似的操作
。在 SQL Developer 中,如果您运行具有任意数量的绑定变量(以冒号为前缀)的语句,系统将提示您输入值。正如 Alex 指出的那样,您还可以使用“运行脚本”功能 (F5) 以及 Alex 建议的备用 EXEC 语法来执行类似的操作。
In SQL*Plus, you can do something very similar
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.
好吧,我知道这有点黑客,但这是一种在简单查询中使用变量的方法,而不是脚本:
您可以在任何地方运行它。
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:
You get to run it everywhere.
简单回答:不。
但是,您可以通过使用绑定变量运行以下版本来实现类似的效果:
在 SQL Developer 中运行上面的查询后,系统将提示您输入绑定变量 EmployeeID 的值。
Simple answer NO.
However you can achieve something similar by running the following version using bind variables:
Once you run the query above in SQL Developer you will be prompted to enter value for the bind variable EmployeeID.
我认为您的情况最简单的方法是:
对于字符串值,它会像:
I think that the Easiest way in your case is :
For the string values it will be like :
您可以在其他地方阅读替代变量;它们在 SQL Developer 中非常方便。但我在尝试在 SQL Developer 中使用绑定变量时遇到了困难。这就是我所做的:
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
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 toSELECT ... INTO
either variable or a refcursor. You can't justSELECT
and return a result set in PL/SQL.在 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';
使用下一个查询:
Use the next query:
尝试一下它会起作用,最好创建一个过程,如果过程不可能,您可以使用此脚本。
Try this it will work, it's better create a procedure, if procedure is not possible you can use this script.