views:

59

answers:

2

Hi everybody,

I am doing some tests here and write some SQLs. I try to write some reusable code and therefore I want to declare some variables at the beginning and reuse them in the script, like this:

DEFINE stupidvar = 'stupidvarcontent';

SELECT stupiddata
FROM stupidtable
WHERE stupidcolumn = &stupidvar;

I tried so far:

Use a DECLARE section and insert the following SELECT statement in BEGIN and END;. Acces the variable using &stupidvar. Use the keyword DEFINE and access the variable. Using the keyword VARIABLE and access the the variable.

I am getting all kinds of errors during my tries (Unbound variable, Syntax error, Expected SELECT INTO...).

While searching the net I find a lot of different explanations, concerning SQL, PL/SQL, etc.

I am using SQLDeveloper to access an Oracle DB. How can I declare a variable and reuse it in the following statements?

Thank you in advance!

+1  A: 

Try using double quotes if it's a char variable:

DEFINE stupidvar = "'stupidvarcontent'";

or

DEFINE stupidvar = 'stupidvarcontent';

SELECT stupiddata  
FROM stupidtable  
WHERE stupidcolumn = '&stupidvar'

upd:

SQL*Plus: Release 10.2.0.1.0 - Production on Wed Aug 25 17:13:26 2010

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

SQL> conn od/od@etalon
Connected.
SQL> define var = "'FL-208'";
SQL> select code from product where code = &var;
old   1: select code from product where code = &var
new   1: select code from product where code = 'FL-208'

CODE
---------------
FL-208

SQL> define var = 'FL-208';
SQL> select code from product where code = &var;
old   1: select code from product where code = &var
new   1: select code from product where code = FL-208
select code from product where code = FL-208
                                      *
ERROR at line 1:
ORA-06553: PLS-221: 'FL' is not a procedure or is undefined
be here now
Thank you for your answer, but if I include the var in double quotes, I get a `ORA-01008: not all variables bound`.
bitschnau
can you give your full input which leads to this error?
be here now
Sure! `DEFINE num = 1; SELECT ` leads to:`ORA-01008: not all variables bound`
bitschnau
+3  A: 

There are a several ways of declaring variables in SQL*Plus scripts.

The first is to use VAR. The mechanism for assigning values to a VAR is with an EXEC call:

SQL> var name varchar2(20)
SQL> exec :name := 'SALES'

PL/SQL procedure successfully completed.

SQL> select * from dept
  2  where dname = :name
  3  /

    DEPTNO DNAME          LOC
---------- -------------- -------------
        30 SALES          CHICAGO

SQL>

A VAR is particularly useful when we want to call a stored procedure which has OUT parameters or a function.

Alternativley we can use subsitution variables. These are good for interactive mode:

SQL> accept p_dno prompt "Please enter Department number: " default 10
Please enter Department number: 20
SQL> select ename, sal
  2  from emp
  3  where deptno = &p_dno
  4  /
old   3: where deptno = &p_dno
new   3: where deptno = 20

ENAME             SAL
---------- ----------
CLARKE            800
ROBERTSON        2975
RIGBY            3000
KULASH           1100
GASPAROTTO       3000

SQL>

When we're writing a script which calls other scripts it can be useful to DEFine the variables upfront:

SQL> def p_dno = 40
SQL> select ename, sal
  2  from emp
  3  where deptno = &p_dno
  4  /
old   3: where deptno = &p_dno
new   3: where deptno = 40

no rows selected

SQL>

Then finally there's the good old anonymous PL/SQL block. As you see, we can still assign values to declared variables interactively:

SQL> set serveroutput on size unlimited
SQL> declare
  2      n pls_integer;
  3      l_sal number := 3500;
  4      l_dno number := &dno;
  5  begin
  6      select count(*)
  7      into n
  8      from emp
  9      where sal > l_sal
 10      and deptno = l_dno;
 11      dbms_output.put_line('top earners = '||to_char(n));
 12  end;
 13  /
Enter value for dno: 10
old   4:     l_dno number := &dno;
new   4:     l_dno number := 10;
top earners = 1

PL/SQL procedure successfully completed.

SQL>
APC
All good, except for your use of the term "bind variable". The VAR declaration creates a bind variable, while ACCEPT or DEFINE creates a substitution variable.
Dave Costa
@DaveCosta - corrected. Thanks
APC