如何声明变量并在同一个 Oracle SQL 脚本中使用它?
我想编写可重用的代码,需要在开头声明一些变量并在脚本中重用它们,例如:
DEFINE stupidvar = 'stupidvarcontent';
SELECT stupiddata
FROM stupidtable
WHERE stupidcolumn = &stupidvar;
如何声明变量并在后面的语句中重用它,例如使用它 SQLDeveloper。
尝试
- 使用 DECLARE 部分并在
BEGIN
和END;
中插入以下 SELECT 语句。使用&stupidvar
访问变量。 - 使用关键字
DEFINE
并访问该变量。 - 使用关键字
VARIABLE
并访问变量。
但我在尝试期间遇到了各种错误(未绑定变量、语法错误、预期 SELECT INTO...)。
I want to write reusable code and need to declare some variables at the beginning and reuse them in the script, such as:
DEFINE stupidvar = 'stupidvarcontent';
SELECT stupiddata
FROM stupidtable
WHERE stupidcolumn = &stupidvar;
How can I declare a variable and reuse it in statements that follow such as in using it SQLDeveloper.
Attempts
- Use a DECLARE section and insert the following SELECT statement in
BEGIN
andEND;
. Acces the variable using&stupidvar
. - Use the keyword
DEFINE
and access the variable. - Using the keyword
VARIABLE
and access the the variable.
But I am getting all kinds of errors during my tries (Unbound variable, Syntax error, Expected SELECT INTO
...).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
在 SQL*Plus 脚本中声明变量的方法有多种。
第一种是使用 VAR,声明绑定变量。为 VAR 赋值的机制是通过 EXEC 调用:
当我们想要调用具有 OUT 参数或函数的存储过程时,VAR 特别有用。
或者我们可以使用替代变量。这些对于交互模式很有用:
当我们编写一个调用其他脚本的脚本时,预先定义变量会很有用。该代码片段运行时不会提示我输入值:
最后是匿名 PL/SQL 块。如您所见,我们仍然可以交互式地为声明的变量赋值:
There are a several ways of declaring variables in SQL*Plus scripts.
The first is to use VAR, to declare a bind variable. The mechanism for assigning values to a VAR is with an EXEC call:
A VAR is particularly useful when we want to call a stored procedure which has OUT parameters or a function.
Alternatively we can use substitution variables. These are good for interactive mode:
When we're writing a script which calls other scripts it can be useful to DEFine the variables upfront. This snippet runs without prompting me to enter a value:
Finally there's the anonymous PL/SQL block. As you see, we can still assign values to declared variables interactively:
如果它是 char 变量,请尝试使用双引号:
或
upd:
Try using double quotes if it's a char variable:
or
upd:
在PL/SQL v.10中,
关键字declare用于声明变量
以分配值,您可以在声明时设置它
,或者使用
INTO
语句将某些内容选择到该变量中,但是您需要包装语句在BEGIN
和END
中,您还需要确保只返回单个值,并且不要忘记分号。所以完整的语句如下:
你的变量只能在
BEGIN
和END
内使用,所以如果你想使用多个,你将不得不执行多个BEGIN END
包装希望这可以节省您一些时间
In PL/SQL v.10
keyword declare is used to declare variable
to assign a value you can set it when you declare
or to select something into that variable you use
INTO
statement, however you need to wrap statement inBEGIN
andEND
, also you need to make sure that only single value is returned, and don't forget semicolons.so the full statement would come out following:
Your variable is only usable within
BEGIN
andEND
so if you want to use more than one you will have to do multipleBEGIN END
wrappingsHope this saves you some time
如果您想声明日期然后在 SQL Developer 中使用它。
If you want to declare date and then use it in SQL Developer.
问题是关于在脚本中使用变量对我来说意味着它将在 SQL*Plus 中使用。
问题是您错过了引号,Oracle 无法将值解析为数字。
由于自动类型转换(或任何名称),该示例工作正常。
如果您通过在 SQL*Plus 中键入 DEFINE 进行检查,它将显示 num 变量是 CHAR。
在这种情况下这不是问题,因为 Oracle 可以处理将字符串解析为数字(如果它是有效数字)。
当字符串无法解析为数字时,Oracle无法处理它。
带引号,所以不要强迫 Oracle 解析为数字,就可以了:
所以,要回答原来的问题,应该像这个示例那样做:
还有另一种方法可以在 SQL*Plus 中存储变量,方法是使用 查询列值。
COL[UMN] 具有 new_value 选项来存储按字段名称查询的值。
正如你所看到的,X.log值被设置到了stupid_var变量中,因此我们可以在当前目录中找到一个X.log文件,里面有一些日志。
The question is about to use a variable in a script means to me it will be used in SQL*Plus.
The problem is you missed the quotes and Oracle can not parse the value to number.
This sample is works fine because of automatic type conversion (or whatever it is called).
If you check by typing DEFINE in SQL*Plus, it will shows that num variable is CHAR.
It is not a problem in this case, because Oracle can deal with parsing string to number if it would be a valid number.
When the string can not parse to number, than Oracle can not deal with it.
With a quote, so do not force Oracle to parse to number, will be fine:
So, to answer the original question, it should be do like this sample:
There is an other way to store variable in SQL*Plus by using Query Column Value.
The COL[UMN] has new_value option to store value from query by field name.
As you can see, X.log value was set into the stupid_var variable, so we can find a X.log file in the current directory has some log in it.
只是想添加 Matas 的答案。
也许这是显而易见的,但我搜索了很长时间才发现 变量只能在 BEGIN-END 结构中访问,所以如果您稍后需要在某些代码中使用它,您需要将此代码放入 BEGIN-END 块内。
请注意,这些块可以嵌套:
Just want to add Matas' answer.
Maybe it's obvious, but I've searched for a long time to figure out that the variable is accessible only inside the BEGIN-END construction, so if you need to use it in some code later, you need to put this code inside the BEGIN-END block.
Note that these blocks can be nested:
在 Toad 中,我使用此方法:
然后该值将打印到 DBMS 输出窗口。
参考此处和此处2< /a>.
In Toad I use this works:
Then the value will be print to
DBMS Output
Window.Reference to here and here2.
这是你的答案:
Here's your answer:
您可以使用
with
子句并将过滤条件从where
移动到join
。它在这里有帮助:使用 DEFINE 的 Oracle SQL 替代方案。
它适用于 Oracle 12R2。
它仅适用于一个 SQL 命令。
这是标准 ANSI 表示法。
我在 SQL Developer 中使用它。
You can use a
with
clause and move filter criteria from awhere
to ajoin
.It helps here: Oracle SQL alternative to using DEFINE.
It works in Oracle 12R2.
It works for one SQL command only.
It is standard ANSI notation.
I'm using it in SQL Developer.
如果您只需要指定一次参数并将其复制到多个位置,一种可能的方法是执行以下操作:
此代码生成一串 8 个随机数字。
请注意,我创建了一种名为
str_size
的别名,其中包含常量8
。它是交叉连接的,可以在查询中多次使用。One possible approach, if you just need to specify a parameter once and replicate it in several places, is to do something like this:
This code generates a string of 8 random digits.
Notice that I create a kind of alias named
str_size
that holds the constant8
. It is cross-joined to be used more than once in the query.有时您需要使用宏变量而不要求用户输入值。大多数情况下,这必须使用可选的脚本参数来完成。以下代码功能齐全
在rdbms/sql 目录中以某种方式找到了类似的代码。
Sometimes you need to use a macro variable without asking the user to enter a value. Most often this has to be done with optional script parameters. The following code is fully functional
Similar code was somehow found in the rdbms/sql directory.