站长网 百科 简单的Oracle变量SQL赋值

简单的Oracle变量SQL赋值

尽管花了一个小时研究我似乎无法弄清楚如何正确定义变量然后在SQL中使用它. 这是我到目前为止所做的: DECLARE startDate DATE:= to_date(’03 / 11/2011′,’dd / mm / yyyy’); 其中我得到了答复: ORA-06550: line 1,column 63: PLS-00103: Encountere

尽管花了一个小时研究我似乎无法弄清楚如何正确定义变量然后在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;

本文来自网络,不代表站长网立场,转载请注明出处:https://www.zwzz.com.cn/html/baike/2021/0524/5194.html

作者: dawei

【声明】:站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。
联系我们

联系我们

0577-28828765

在线咨询: QQ交谈

邮箱: xwei067@foxmail.com

工作时间:周一至周五,9:00-17:30,节假日休息

返回顶部