Register now and start sharing your code snippets.
-->

Simplify and make SQL scripts run faster by using Oracle variables

SQL posted about 1 year ago by christian

Oracle variables are a great way of removing duplicate SQL statements from your Oracle SQL scripts. By using a variable, you can store the return value of a query and use it later in another, as this example illustrates:

   1  COLUMN your_column new_value your_variable;
   2  
   3  SELECT DISTINCT (your_column)
   4                                   FROM ...
   5                                  WHERE ...;
   6  
   7  SELECT &your_variable FROM dual;

The above example stores the value of the SELECT DISTINCT query in a variable named your_variable. You declare the variable with new_value, and set the value by executing an SQL statement that returns one row and column.

Tagged sql, variables, oracle