AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 3652
Accepted
Mark Allison
Mark Allison
Asked: 2011-07-06 01:54:26 +0800 CST2011-07-06 01:54:26 +0800 CST 2011-07-06 01:54:26 +0800 CST

如何在 Oracle 中声明和使用变量?

  • 772

我的主要技能是使用 SQL Server,但有人要求我对 Oracle 查询进行一些调整。我编写了以下 SQL:

declare @startDate int
select @startDate = 20110501

我得到这个错误:

declare @startDate int
select @startDate = 20110501
Error at line 1
ORA-06550: line 1, column 9:
PLS-00103: Encountered the symbol "@" when expecting one of the following:

   begin function package pragma procedure subtype type use
   <an identifier> <a double-quoted delimited-identifier> form
   current cursor

如何在 Oracle 中声明和使用变量?

oracle oracle-10g
  • 3 3 个回答
  • 246491 Views

3 个回答

  • Voted
  1. Best Answer
    ik_zelf
    2011-07-06T02:41:25+08:002011-07-06T02:41:25+08:00

    在 pl/sql 块内:

    declare
     startdate number;
    begin
      select 20110501 into startdate from dual;
    end;
    /
    

    使用绑定变量:

    var startdate number;
    begin
      select 20110501 into :startdate from dual;
    end;
    /
    

    PL/SQL 过程成功完成。

    SQL> print startdate
    
     STARTDATE
    ----------
      20110501
    

    在查询中:

    select object_name 
    from user_objects 
    where created > to_date (:startdate,'yyyymmdd');  /*prefix the bind variable wïth ":" */
    
    • 21
  2. Jon of All Trades
    2016-06-22T14:47:53+08:002016-06-22T14:47:53+08:00

    SQL*Plus 支持一种附加格式:

    DEFINE StartDate = TO_DATE('2016-06-21');
    DEFINE EndDate   = TO_DATE('2016-06-30');
    
    SELECT
        *
    FROM
        MyTable
    WHERE
        DateField BETWEEN &StartDate and &EndDate;
    

    请注意要在查询中执行替换的与号。

    • 5
  3. Ortsbo
    2020-10-24T22:06:51+08:002020-10-24T22:06:51+08:00

    在 ORACLE SQL Developer 20.2.0.175 中,我们可以运行脚本 (F5):

    DEFINE usr = 'YourName'; 
    SELECT * FROM Department WHERE created_by = '&usr';
    
    • 0

相关问题

  • Oracle 中的数据库备份 - 导出数据库还是使用其他工具?

  • ORDER BY 使用文本列的自定义优先级

  • 舒服的sqlplus界面?[关闭]

  • 如何在数据库中找到最新的 SQL 语句?

  • 如何使用正则表达式查询名称?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    您如何显示在 Oracle 数据库上执行的 SQL?

    • 2 个回答
  • Marko Smith

    如何选择每组的第一行?

    • 6 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    我可以查看在 SQL Server 数据库上运行的历史查询吗?

    • 6 个回答
  • Marko Smith

    如何在 PostgreSQL 中使用 currval() 来获取最后插入的 id?

    • 10 个回答
  • Marko Smith

    如何在 Mac OS X 上运行 psql?

    • 11 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Marko Smith

    将数组参数传递给存储过程

    • 12 个回答
  • Martin Hope
    Manuel Leduc PostgreSQL 多列唯一约束和 NULL 值 2011-12-28 01:10:21 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Stuart Blackler 什么时候应该将主键声明为非聚集的? 2011-11-11 13:31:59 +0800 CST
  • Martin Hope
    pedrosanta 使用 psql 列出数据库权限 2011-08-04 11:01:21 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST
  • Martin Hope
    BrunoLM Guid vs INT - 哪个更好作为主键? 2011-01-05 23:46:34 +0800 CST
  • Martin Hope
    bernd_k 什么时候应该使用唯一约束而不是唯一索引? 2011-01-05 02:32:27 +0800 CST
  • Martin Hope
    Patrick 如何优化大型数据库的 mysqldump? 2011-01-04 13:13:48 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve