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

  • SEARCH
  • Home
  • 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 976353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:44:03+00:00 2026-05-16T03:44:03+00:00

I’m attempting to write a simple query where I declare some variables and then

  • 0

I’m attempting to write a simple query where I declare some variables and then use them in a select statement in Oracle. I’ve been able to do this before in SQL Server with the following:

DECLARE @date1   DATETIME
SET @date1 = '03-AUG-2010'

SELECT U.VisualID
FROM Usage u WITH(NOLOCK)
WHERE U.UseTime > @Date1

From the searching I’ve done it appears you can not declare and set variables like this in Select statements. Is this right or am I mssing something?

  • 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-16T03:44:03+00:00Added an answer on May 16, 2026 at 3:44 am

    From the searching I’ve done it appears you can not declare and set variables like this in Select statements. Is this right or am I missing something?

    Within Oracle PL/SQL and SQL are two separate languages with two separate engines. You can embed SQL DML within PL/SQL, and that will get you variables. Such as the following anonymous PL/SQL block. Note the / at the end is not part of PL/SQL, but tells SQL*Plus to send the preceding block.

    declare 
        v_Date1 date := to_date('03-AUG-2010', 'DD-Mon-YYYY');
        v_Count number;
    begin
        select count(*) into v_Count
        from Usage
        where UseTime > v_Date1;
        dbms_output.put_line(v_Count);
    end;
    /
    

    The problem is that a block that is equivalent to your T-SQL code will not work:

    SQL> declare 
      2      v_Date1 date := to_date('03-AUG-2010', 'DD-Mon-YYYY');
      3  begin
      4      select VisualId
      5      from Usage
      6      where UseTime > v_Date1;
      7  end;
      8  /
        select VisualId
        *
    ERROR at line 4:
    ORA-06550: line 4, column 5:
    PLS-00428: an INTO clause is expected in this SELECT statement
    

    To pass the results of a query out of an PL/SQL, either an anonymous block, stored procedure or stored function, a cursor must be declared, opened and then returned to the calling program. (Beyond the scope of answering this question. EDIT: see Get resultset from oracle stored procedure)

    The client tool that connects to the database may have it’s own bind variables. In SQL*Plus:

    SQL> -- SQL*Plus does not all date type in this context
    SQL> -- So using varchar2 to hold text
    SQL> variable v_Date1 varchar2(20)
    SQL>
    SQL> -- use PL/SQL to set the value of the bind variable
    SQL> exec :v_Date1 := '02-Aug-2010';
    
    PL/SQL procedure successfully completed.
    
    SQL> -- Converting to a date, since the variable is not yet a date.
    SQL> -- Note the use of colon, this tells SQL*Plus that v_Date1
    SQL> -- is a bind variable.
    SQL> select VisualId
      2  from Usage
      3  where UseTime > to_char(:v_Date1, 'DD-Mon-YYYY');
    
    no rows selected
    

    Note the above is in SQLPlus, may not (probably won’t) work in Toad PL/SQL developer, etc. The lines starting with variable and exec are SQLPlus commands. They are not SQL or PL/SQL commands. No rows selected because the table is empty.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 545k
  • Answers 545k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • added an answer Well, start off by thinking of which bits of data… May 17, 2026 at 9:17 am
  • added an answer For this task it is a good idea to use… May 17, 2026 at 9:15 am
  • added an answer This is exactly how the Skyhook database (built into many… May 17, 2026 at 9:15 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.