尽管花了一个小时进行研究,但我似乎无法弄清楚如何正确定义变量,然后在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;
关于oracle - 简单的Oracle变量SQL分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8039907/