Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Oracle have an equivalent of SQL Server's table variables?

Tags:

oracle

In SQL Server, you can declare a table variable (DECLARE @table TABLE), which is produced while the script is run and then removed from memory.

Does Oracle have a similar function? Or am I stuck with CREATE/DROP statements that segment my hard drive?

like image 429
Margaret Avatar asked Mar 22 '09 02:03

Margaret


People also ask

Does Oracle have table variables?

Table variables are also known as index-by table or array. The table variable contains one column which must be a scalar or record datatype plus a primary key of type BINARY_INTEGER. Although the type is called 'Table' they are more like advanced arrays.

Can you use variables in Oracle SQL?

You can define variables, called substitution variables, for repeated use in a single script by using the SQL*Plus DEFINE command.

What is variables in Oracle SQL?

In PL/SQL, a variable is named storage location that stores a value of a particular data type. The value of the variable changes through the program. Before using a variable, you must declare it in the declaration section of a block.

Is Oracle similar to SQL Server?

In short, both Oracle and SQL Server are powerful RDBMS options. Although there are a number of differences in how they work “under the hood,” they can both be used in roughly equivalent ways. Neither is objectively better than the other, but some situations may be more favorable to a particular choice.


4 Answers

Yes.

Declare TABLE TYPE variables in a PL/SQL declare block. Table variables are also known as index-by table or array. The table variable contains one column which must be a scalar or record datatype plus a primary key of type BINARY_INTEGER. Syntax:

DECLARE TYPE type_name IS TABLE OF (column_type | variable%TYPE | table.column%TYPE [NOT NULL] INDEX BY BINARY INTEGER;

-- Then to declare a TABLE variable of this type: variable_name type_name;

-- Assigning values to a TABLE variable: variable_name(n).field_name := 'some text'; -- Where 'n' is the index value

Ref: http://www.iselfschooling.com/syntax/OraclePLSQLSyntax.htm

You might want to also take a look at Global Temporary Tables

like image 168
Mitch Wheat Avatar answered Sep 25 '22 05:09

Mitch Wheat


The below solution is the closest from SQL Server I can do today.

Objects:

      CREATE OR REPLACE TYPE T_NUMBERS IS TABLE OF NUMBER;      CREATE OR REPLACE FUNCTION ACCUMULATE (vNumbers T_NUMBERS)     RETURN T_NUMBERS     AS        vRet T_NUMBERS;     BEGIN        SELECT SUM(COLUMN_VALUE)        BULK COLLECT INTO vRet        FROM TABLE(CAST(vNumbers AS T_NUMBERS));         RETURN vRet;     END; 

Queries:

      --Query 1: Fixed number list.     SELECT *     FROM TABLE(ACCUMULATE(T_NUMBERS(1, 2, 3, 4, 5)));      --Query 2: Number list from query.     WITH cteNumbers AS     (       SELECT 1 AS COLUMN_VALUE FROM DUAL UNION       SELECT 2 AS COLUMN_VALUE FROM DUAL UNION       SELECT 3 AS COLUMN_VALUE FROM DUAL UNION       SELECT 4 AS COLUMN_VALUE FROM DUAL UNION       SELECT 5 AS COLUMN_VALUE FROM DUAL     )     SELECT *     FROM TABLE(             ACCUMULATE(               (SELECT CAST(COLLECT(COLUMN_VALUE) AS T_NUMBERS)                FROM cteNumbers)             )           );  
like image 29
Paulo Corrêa Avatar answered Sep 22 '22 05:09

Paulo Corrêa


Yes it does have a type that can hold the result set of a query (if I can guess what TABLE does). From ask Tom: your procedure may look like this:

procedure p( p_state in varchar2, p_cursor in out ref_cursor_type )
is
begin
    open p_cursor for select * from table where state = P_STATE;
end;

where p_cursor is like a table type. As has been already answered there are plenty of options for storing result sets in Oracle. Generally Oracle PL/SQL is far more powerful than sqlserver scripts.

like image 30
Martlark Avatar answered Sep 22 '22 05:09

Martlark


the table in variable in oracle not the same as table variables in MS SQLServer. in oracle it's like regular array in java or c#. but in MS SQLserver it is the same as any table, you can call it logical table. but if you want something in oracle that does exactly the same as table variable of SQLserver you can use cursor.

regards

like image 34
mahmoud Avatar answered Sep 22 '22 05:09

mahmoud