Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use variables in an Oracle PL/SQL where clause

I can't seem to get variables to work in an Oracle PL/SQL where clause. I come from a Microsoft SQL Server background and there it was easy. For example, what would be all steps needed to do something similar to the following?

declare @var int set @var = 1

select * from SomeTable where SomeField = @var

This doesn't seem like it should be hard in PL/SQL, but evidently it is. :-/ I hear I need to use cursors and the like in PL/SQL?

Any help would be greatly appreciated. Thanks.

like image 519
MattB Avatar asked Apr 14 '11 16:04

MattB


People also ask

Can we use variable in WHERE clause in SQL?

The variable may be used in subsequent queries wherever an expression is allowed, such as in a WHERE clause or in an INSERT statement. A common situation in which SQL variables come in handy is when you need to issue successive queries on multiple tables that are related by a common key value.

How can you handle variables in PL SQL?

PL/SQL variables must be declared in the declaration section or in a package as a global variable. When you declare a variable, PL/SQL allocates memory for the variable's value and the storage location is identified by the variable name.

Can you use variables in Oracle SQL?

There are two types of variable in SQL-plus: substitution and bind. This is substitution (substitution variables can replace SQL*Plus command options or other hard-coded text):

Can we use WHERE clause in case statement in Oracle?

Introduction to Oracle CASE expression You can use a CASE expression in any statement or clause that accepts a valid expression. For example, you can use the CASE expression in statements such as SELECT , UPDATE , or DELETE , and in clauses like SELECT , WHERE , HAVING , and ORDDER BY .


2 Answers

declare

  type t_rec is record
  (
  col1 number,
  col2 myTable.col2%type
  );
  v_rec t_rec;

  type t_tab is table of v_rec%type index by binary_integer;
  v_tab t_tab;

begin

  select col1, col2
  bulk collect into v_tab
  from myTable
  where col3 = 'BLAH';

  -- do something great with v_tab...

end;

Also know that if you try to select into (or bulk collect into) a variable and no rows are returned, you'll get a no_data_found exception, so you may want to handle that situation.

See more here on pl/sql collections. Above uses an associative array, but there are nested tables and varrays as well. Again, see the link.

Hope that helps.

like image 184
tbone Avatar answered Oct 19 '22 19:10

tbone


What do you want to do with the data that the SELECT returns? If you just want to see it you don't need PL/SQL at all, just do this in SQL Plus:

variable var number
exec :var := 1

select * from SomeTable where SomeField = :var;

Or in a tool like SQL Developer or Toad, just do this:

select * from SomeTable where SomeField = :var;

and it will prompt you to enter the value for :var.

like image 25
Tony Andrews Avatar answered Oct 19 '22 18:10

Tony Andrews