1

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.
3
  • 1
    You're using a substitution variable - so the value will be pasted in as if it were code. That's why you need ''quotes'' around it. Note: Be aware of the SQL injection vulnerability here. e.g. the user could enter something like '; execute immediate 'drop table bobby'; Commented Apr 16, 2014 at 3:56
  • possible duplicate of SQL*Plus how to accept text variable from prompt? Commented Apr 16, 2014 at 5:08
  • Thx Jeffrey, but there is not risk for injection. The script was used temporary by team members only. Commented Aug 4, 2014 at 22:45

1 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.