简单的Oracle变量SQL赋值

尽管花了一个小时研究我似乎无法弄清楚如何正确定义变量然后在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块中的变量(其主体由BEGIN和END分隔;).你想如何使用这个变量?

以下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;

相关文章

Java Oracle 结果集是Java语言中处理数据库查询结果的一种方...
Java AES和Oracle AES是现代加密技术中最常使用的两种AES加密...
Java是一种广泛应用的编程语言,具备可靠性、安全性、跨平台...
随着移动互联网的发展,抽奖活动成为了营销活动中不可或缺的...
Java和Oracle都是在计算机领域应用非常广泛的技术,他们经常...
Java 是一门非常流行的编程语言,它可以运行于各种操作系统上...