Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1015761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:26:57+00:00 2026-05-16T10:26:57+00:00

I want to write reusable code and need to declare some variables at the

  • 0

I want to write reusable code and need to declare some variables at the beginning and reuse them in the script, such as:

DEFINE stupidvar = 'stupidvarcontent';

SELECT stupiddata
FROM stupidtable
WHERE stupidcolumn = &stupidvar;

How can I declare a variable and reuse it in statements that follow such as in using it SQLDeveloper.


Attempts

  • 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.

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:26:58+00:00Added an answer on May 16, 2026 at 10:26 am

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

    The first is to use VAR, to declare a bind variable. 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.

    Alternatively we can use substitution 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. This snippet runs without prompting me to enter a value:

    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>
    

    Finally there’s the 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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.