绑定变量和替换变量(我使用 && 输入)之间有什么区别?
这两个变量声明有什么区别?
1: num number:='&&num';
2: variable num1 number;
因为在这两种情况下我都可以使用 &num
或 &&num< 来引用
num
/code> 也在其他文件中, 对于绑定变量 :num1
。
此外,我还有一个困惑:以下任何陈述是否有所不同,它们是否都有效并且含义相同吗?
1:变量num1数字;
2:var num1 数字;
What is the difference between these two variable declarations?
1: num number:='&&num';
2: variable num1 number;
Since in both cases I can reference num
by using &num
or &&num
in other files also,
and in the case of bind variables :num1
.
Moreover I have one more confusion: whether any of the below statements differ somehow, are they both valid and do they mean the same thing?
1: variable num1 number;
2: var num1 number;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎对 Oracle 中的绑定变量与 SQL*Plus 中的替换变量之间的差异有些困惑。
让我们从替换变量开始。替换变量是 SQL*Plus 所特有的,并且不是数据库的一部分。例如,如果您尝试将它们与 JDBC 一起使用,它们将无法工作。
替换变量只能保存一段文本。如果 SQL*Plus 在输入行中遇到替换变量,它将用其文本内容替换该变量:
请注意,SQL*Plus 用其文本值替换了替换变量,而不考虑它是否为我们提供了有效的 SQL。在上面的例子中,我们省略了
&subvar
周围的单引号,它给了我们无效的 SQL,所以我们得到了一个错误。以
old
和new
开头的行显示了 SQL*Plus 应用替换变量之前和之后输入的行。new
行是数据库尝试运行的行。您可以使用
SET VERIFY ON
和SET VERIFY OFF
启用或禁用旧行和新行的显示。您还可以使用SET DEFINE ON
和SET DEFINE OFF
打开或关闭替换变量的替换。如果我们想使用替换变量运行上述查询,我们必须在它周围加上引号:
如果
&subvar
碰巧包含一个有效数字的字符串(例如5
),那么我们可以不使用引号,但这只是因为取出文本&subvar
并将其替换为文本5
恰好给了我们有效的SQL。例如,假设我们有一个名为
test
的表,其中包含以下数据:然后我们可以进行
绑定变量,另一方面,变量具有类型。它们不是简单的文本值。它们的值被发送到数据库,数据库也可以设置它们的值。
当您想要使用绑定变量时,不要在绑定变量周围加上引号:
在上面的第二个示例中,我们没有返回任何行,因为
DUAL
表没有带有DUMMY 列包含文本
:bindvar
。如果您尝试将错误类型的值分配给绑定变量,您将收到错误:
绑定变量是数据库的标准部分,您可以将它们与 JDBC 或连接到您选择的数据库的任何方法一起使用。
最后,
variable num1 number
和var num1 number
含义相同。它们都定义了一个number
类型的绑定变量num1
。var
只是variable
的缩写。You appear to have some confusion about the differences between bind variables in Oracle and substitution variables in SQL*Plus.
Let's start with substitution variables. Substitution variables are unique to SQL*Plus and are not part of the database. They won't work if you try to use them with JDBC, for example.
Substitution variables can only hold a piece of text. If SQL*Plus encounters a substitution variable in a line of input, it will replace the variable with its text contents:
Note that SQL*Plus replaced our substitution variable with its text value with no regard for whether it gave us valid SQL. In the example above, we omitted the single quotes around
&subvar
and it gave us invalid SQL, so we got an error.The lines beginning
old
andnew
show us the line we entered before and after SQL*Plus applied the substitution variables. Thenew
line is the line the database tried to run.You can enable or disable the display of the
old
andnew
lines usingSET VERIFY ON
andSET VERIFY OFF
. You can also turn the replacement of substitution variables on or off by usingSET DEFINE ON
andSET DEFINE OFF
.If we want to run the above query using the substitution variable, we must put quotes around it:
If
&subvar
happens to contain a string that was a valid number (e.g.5
), then we can get away without using the quotes, but that's only because taking out the text&subvar
and replacing it with the text5
happens to give us valid SQL.For example, suppose we have a table called
test
with the following data in it:Then we can do
Bind variables, on the other hand, have types. They are not simple text values. Their values are sent to the database, and the database can also set their values.
You don't put quotes around a bind variable when you want to use it:
In the second example above, we got no rows returned because the
DUAL
table has no rows with theDUMMY
column containing the text:bindvar
.You'll get an error if you attempt to assign a value of the wrong type to a bind variable:
Bind variables are a standard part of the database, and you can use them with JDBC or whichever method of connecting to the database you choose.
Finally,
variable num1 number
andvar num1 number
both mean the same thing. They both define a bind variablenum1
of typenumber
.var
is just an abbreviation forvariable
.