I am facing a strange issue.

I have a small pl/sql anonymous block which prompt the user for a date. I want to re-use this date in a query later, but I get an error like "SP2-0552: Bind variable "01" not declared.".

The issue is that ":01" in the time is interpreted as a bind variable. A workaround is to enter the date between quote '2014/04/16 01:01:01' and not directly 2014/04/16 01:01:01. However, I want to be able to enter my date without the quote.

here is a simple script:

declare
    adate VARCHAR2(20);
begin
    adate := &adate;
    query := 'select to_date(''' || adate ||''', ''YYYY/MM/DD HH24:MI:SS'') from dual';
    dbms_output.put_line(query);
end;

Enter value for adate: 2014/04/15 01:01:01
old   4:    adate := &adate;
new   4:    adate := 2014/04/15 01:01:01;
SP2-0552: Bind variable "01" not declared.
有帮助吗?

解决方案

I found the answer.

The variable must be between quote in the script.

declare
    adate VARCHAR2(20);
begin
    adate := '&adate';
    query := 'select to_date(''' || adate ||''', ''YYYY/MM/DD HH24:MI:SS'') from dual';
    dbms_output.put_line(query);
end;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top