声明&在 Select 语句中设置变量

发布于 2024-09-12 21:04:19 字数 306 浏览 5 评论 0原文

我正在尝试编写一个简单的查询,在其中声明一些变量,然后在 Oracle 的 select 语句中使用它们。我之前已经能够在 SQL Server 中通过以下操作执行此操作:

DECLARE @date1   DATETIME
SET @date1 = '03-AUG-2010'

SELECT U.VisualID
FROM Usage u WITH(NOLOCK)
WHERE U.UseTime > @Date1

从我所做的搜索来看,您似乎无法在 Select 语句中声明和设置这样的变量。这是正确的还是我错过了什么?

I'm attempting to write a simple query where I declare some variables and then use them in a select statement in Oracle. I've been able to do this before in SQL Server with the following:

DECLARE @date1   DATETIME
SET @date1 = '03-AUG-2010'

SELECT U.VisualID
FROM Usage u WITH(NOLOCK)
WHERE U.UseTime > @Date1

From the searching I've done it appears you can not declare and set variables like this in Select statements. Is this right or am I mssing something?

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

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

发布评论

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

评论(5

只是在用心讲痛 2024-09-19 21:04:19

从我所做的搜索来看,您似乎无法在 Select 语句中声明和设置这样的变量。这是正确的还是我遗漏了什么?

在 Oracle PL/SQL 和 SQL 中,是两种独立的语言,具有两个独立的引擎。您可以将 SQL DML 嵌入到 PL/SQL 中,这将为您提供变量。例如下面的匿名 PL/SQL 块。请注意,末尾的 / 不是 PL/SQL 的一部分,但告诉 SQL*Plus 发送前面的块。

declare 
    v_Date1 date := to_date('03-AUG-2010', 'DD-Mon-YYYY');
    v_Count number;
begin
    select count(*) into v_Count
    from Usage
    where UseTime > v_Date1;
    dbms_output.put_line(v_Count);
end;
/

问题在于,与 T-SQL 代码等效的块将不起作用:

SQL> declare 
  2      v_Date1 date := to_date('03-AUG-2010', 'DD-Mon-YYYY');
  3  begin
  4      select VisualId
  5      from Usage
  6      where UseTime > v_Date1;
  7  end;
  8  /
    select VisualId
    *
ERROR at line 4:
ORA-06550: line 4, column 5:
PLS-00428: an INTO clause is expected in this SELECT statement

要将查询结果传递出 PL/SQL(匿名块、存储过程或存储函数),必须声明游标,打开然后返回到调用程序。 (超出了回答这个问题的范围。编辑:请参阅获取来自oracle存储过程的结果集

连接到数据库的客户端工具可能有它自己的绑定变量。在 SQL*Plus 中:

SQL> -- SQL*Plus does not all date type in this context
SQL> -- So using varchar2 to hold text
SQL> variable v_Date1 varchar2(20)
SQL>
SQL> -- use PL/SQL to set the value of the bind variable
SQL> exec :v_Date1 := '02-Aug-2010';

PL/SQL procedure successfully completed.

SQL> -- Converting to a date, since the variable is not yet a date.
SQL> -- Note the use of colon, this tells SQL*Plus that v_Date1
SQL> -- is a bind variable.
SQL> select VisualId
  2  from Usage
  3  where UseTime > to_char(:v_Date1, 'DD-Mon-YYYY');

no rows selected

请注意,以上是在 SQLPlus 中,可能无法(可能不会)在 Toad PL/SQL Developer 等中工作。以变量和 exec 开头的行是 SQLPlus 命令。它们不是 SQL 或 PL/SQL 命令。由于表为空,因此未选择任何行。

From the searching I've done it appears you can not declare and set variables like this in Select statements. Is this right or am I missing something?

Within Oracle PL/SQL and SQL are two separate languages with two separate engines. You can embed SQL DML within PL/SQL, and that will get you variables. Such as the following anonymous PL/SQL block. Note the / at the end is not part of PL/SQL, but tells SQL*Plus to send the preceding block.

declare 
    v_Date1 date := to_date('03-AUG-2010', 'DD-Mon-YYYY');
    v_Count number;
begin
    select count(*) into v_Count
    from Usage
    where UseTime > v_Date1;
    dbms_output.put_line(v_Count);
end;
/

The problem is that a block that is equivalent to your T-SQL code will not work:

SQL> declare 
  2      v_Date1 date := to_date('03-AUG-2010', 'DD-Mon-YYYY');
  3  begin
  4      select VisualId
  5      from Usage
  6      where UseTime > v_Date1;
  7  end;
  8  /
    select VisualId
    *
ERROR at line 4:
ORA-06550: line 4, column 5:
PLS-00428: an INTO clause is expected in this SELECT statement

To pass the results of a query out of an PL/SQL, either an anonymous block, stored procedure or stored function, a cursor must be declared, opened and then returned to the calling program. (Beyond the scope of answering this question. EDIT: see Get resultset from oracle stored procedure)

The client tool that connects to the database may have it's own bind variables. In SQL*Plus:

SQL> -- SQL*Plus does not all date type in this context
SQL> -- So using varchar2 to hold text
SQL> variable v_Date1 varchar2(20)
SQL>
SQL> -- use PL/SQL to set the value of the bind variable
SQL> exec :v_Date1 := '02-Aug-2010';

PL/SQL procedure successfully completed.

SQL> -- Converting to a date, since the variable is not yet a date.
SQL> -- Note the use of colon, this tells SQL*Plus that v_Date1
SQL> -- is a bind variable.
SQL> select VisualId
  2  from Usage
  3  where UseTime > to_char(:v_Date1, 'DD-Mon-YYYY');

no rows selected

Note the above is in SQLPlus, may not (probably won't) work in Toad PL/SQL developer, etc. The lines starting with variable and exec are SQLPlus commands. They are not SQL or PL/SQL commands. No rows selected because the table is empty.

萌无敌 2024-09-19 21:04:19

我已经尝试过这个并且它有效:

define PROPp_START_DT = TO_DATE('01-SEP-1999')

select * from proposal where prop_start_dt = &PROPp_START_DT

 

I have tried this and it worked:

define PROPp_START_DT = TO_DATE('01-SEP-1999')

select * from proposal where prop_start_dt = &PROPp_START_DT

 

孤独陪着我 2024-09-19 21:04:19

SET 命令是 TSQL 特定的 - 这是与您发布的内容等效的 PLSQL:

v_date1 DATE := TO_DATE('03-AUG-2010', 'DD-MON-YYYY');

SELECT u.visualid
  FROM USAGE u 
 WHERE u.usetime > v_date1;

也不需要在变量前添加“@”前缀;我倾向于在变量前加上“v_”前缀来区分变量和变量。列/等

请参阅此线程有关 Oracle 等效的 NOLOCK...

The SET command is TSQL specific - here's the PLSQL equivalent to what you posted:

v_date1 DATE := TO_DATE('03-AUG-2010', 'DD-MON-YYYY');

SELECT u.visualid
  FROM USAGE u 
 WHERE u.usetime > v_date1;

There's also no need for prefixing variables with "@"; I tend to prefix variables with "v_" to distinguish between variables & columns/etc.

See this thread about the Oracle equivalent of NOLOCK...

心的憧憬 2024-09-19 21:04:19

尝试使用 to_date 函数。

Try the to_date function.

尬尬 2024-09-19 21:04:19

同样来自 SQL Server,这确实让我很烦恼。对于那些使用 Toad Data PointToad for Oracle 的人来说,这非常简单。只需在变量名称前面放置一个冒号,Toad 就会提示您打开一个对话框,您可以在其中输入执行时的值。

SELECT * FROM some_table WHERE some_column = :var_name;

Coming from SQL Server as well, and this really bugged me. For those using Toad Data Point or Toad for Oracle, it's extremely simple. Just putting a colon in front of your variable name will prompt Toad to open a dialog where you enter the value on execute.

SELECT * FROM some_table WHERE some_column = :var_name;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文