oracle Oracle中如何声明和显示变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8724478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to declare and display a variable in Oracle
提问by David
I would like to declare and display a variable in Oracle.
我想在 Oracle 中声明并显示一个变量。
In T-SQL I would do something like this
在 T-SQL 我会做这样的事情
DECLARE @A VARCHAR(10) --Declares @A
SELECT @A = '12' --Assigns @A
SELECT @A --Displays @A
How can I do this in Oracle.
我怎样才能在 Oracle 中做到这一点。
回答by Sathyajith Bhat
If you're talking about PL/SQL, you should put it in an anonymous block.
如果你在谈论 PL/SQL,你应该把它放在一个匿名块中。
DECLARE
v_text VARCHAR2(10); -- declare
BEGIN
v_text := 'Hello'; --assign
dbms_output.Put_line(v_text); --display
END;
回答by John Doyle
If using sqlplus you can define a variable thus:
如果使用 sqlplus 你可以这样定义一个变量:
define <varname>=<varvalue>
And you can display the value by:
您可以通过以下方式显示值:
define <varname>
And then use it in a query as, for example:
然后在查询中使用它,例如:
select *
from tab1
where col1 = '&varname';
回答by Jahanzaib Mazhar
If you are using pl/sql then the following code should work :
如果您使用的是 pl/sql,那么下面的代码应该可以工作:
set server output on -- to retrieve and display a buffer
将服务器输出设置为打开 - 以检索和显示缓冲区
DECLARE
v_text VARCHAR2(10); -- declare
BEGIN
v_text := 'Hello'; --assign
dbms_output.Put_line(v_text); --display
END;
/
-- this must be use to execute pl/sql script
-- 这必须用于执行 pl/sql 脚本
回答by Matt
Did you recently switch from MySQL and are now longing for the logical equivalents of its more simple commands in Oracle? Because that is the case for me and I had the very same question. This code will give you a quick and dirty print which I think is what you're looking for:
您最近是否从 MySQL 切换,现在渴望在 Oracle 中使用其更简单命令的逻辑等效项?因为这就是我的情况,我也有同样的问题。这段代码会给你一个快速而肮脏的打印,我认为这就是你正在寻找的:
Variable n number
begin
:n := 1;
end;
print n
The middle section is a PL/SQL bit that binds the variable. The output from print n is in column form, and will not just give the value of n, I'm afraid. When I ran it in Toad 11 it returned like this
中间部分是绑定变量的 PL/SQL 位。打印 n 的输出是列形式,恐怕不会只给出 n 的值。当我在 Toad 11 中运行它时,它像这样返回
n
---------
1
I hope that helps
我希望有帮助