39

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?

1
  • Oracle is not my forte, but table variables appear to be a subset of the Oracle Collection Variable Commented Mar 22, 2009 at 2:56

4 Answers 4

23

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

Sign up to request clarification or add additional context in comments.

4 Comments

How can i declare a table with more than one column? In example: table with 2 columns. Number and Varchar2.
By creating a table of records where the record contains each row
@MikeT tables - in SQL products - have rows, not records. Please, don't confuse others by using the term "records".
A record is a collection of data that pertains to an item, as such is can be a single row in a single table or many linked rows across many tables. so the term record is correct
3

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)
            )
          );

1 Comment

What is the purpose in casting vNumbers to the type it already has?
2

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

Comments

0

You can use Global Temporary Table of Oracle.

create global temporary table TempDemo(
 Name varchar2(200),
 Address varchar2(200)
)
on commit preserve rows;

insert into TempDemo values('Shahriar','Vatapara Rajshahi, Bangladesh');
insert into TempDemo values('Zainab','Laxmipur Rajshahi, Bangladesh');
insert into TempDemo values('Sujon','Mohisbathan Rajshahi, Bangladesh');

select * from TempDemo;

delete from TempDemo;
drop table TempDemo;

commit;
rollback;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.