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?
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.
You can define variables, called substitution variables, for repeated use in a single script by using the SQL*Plus DEFINE command.
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.
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.
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
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) ) );
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With