oracle - 简单的Oracle变量SQL分配

标签 oracle variables plsql toad

尽管花了一个小时进行研究,但我似乎无法弄清楚如何正确定义变量,然后在SQL中使用它。

这是我到目前为止产生的:
DECLARE startDate DATE := to_date('03/11/2011', 'dd/mm/yyyy');
我得到的答复:

ORA-06550: line 1, column 63: PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:

begin function package pragma procedure subtype type use form current cursor

Details: DECLARE startDate DATE := to_date('03/11/2011', 'dd/mm/yyyy'); Error at line 1 ORA-06550: line 1, column 63: PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:

begin function package pragma procedure subtype type use form current cursor



我很想知道如何完成这样一个简单的任务!

最佳答案

您的变量声明是正确的。
DECLARE关键字用于定义作用域为PL/SQL块(其主体由BEGINEND;分隔)的变量。您想如何使用此变量?

以下PL/SQL对我来说很好用:

DECLARE 
    startDate DATE := to_date('03/11/2011', 'dd/mm/yyyy');
    reccount INTEGER;
BEGIN
    SELECT count(*) INTO reccount 
        FROM my_table tab 
        WHERE tab.somedate < startDate;
    dbms_output.put_line(reccount);
END;

您也可以使用 DEFINE 语句来使用简单的字符串替换变量。它们适用于像SQL/PLUS或TOAD之类的客户端。
DEFINE start_date = "to_date('03/11/2011', 'dd/mm/yyyy')"
SELECT COUNT(*) from my_table tab where tab.some_date < &start_date;

关于oracle - 简单的Oracle变量SQL分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8039907/

相关文章:

variables - BAT if exist %environment variable folder% (系统文件夹)

java - 无法弄清楚为什么它没有正确解决

java - 概念 : Build Java Program and load it into Oracle DB - Wrapper function calls java function with return

java - 距离内多边形的 api

Oracle:如何在时间戳中添加分钟?

sql - Oracle 更快的重叠检查

PHP 和 URL 中的 A id?

oracle - 使用 Bulk Collect 将批量记录插入到远程数据库 (dblink)

sql - 在 Oracle 中通过脚本插入的更快方法?

java - 映射减少输出到oracle数据库?