Temporary table in Oracle like SQL Server - oracle

In SQL Server one could do something like the following
declare #t as table(id int)
insert into #t values(1)
insert into #t values(2)
delete from #t where id=1
Is there an equivalence of this in Oracle without creating a physical table. Now, I used to create physical table to do this and delete later.
I have gone to this links How to create a temporary table in Oracle but that's 2010 and the reference link mentioned Oracle 8i. Is this still the situation with Oracle 10g and 11g? Another link I have visited is Constructing a temporary table in Oracle SQL
Thanks

CREATE GLOBAL TEMPORARY TABLE admin_work_area
(startdate DATE,
enddate DATE,
class CHAR(20))
ON COMMIT DELETE ROWS;
This statement creates a temporary table that is transaction specific.
For details use below link:
http://docs.oracle.com/cd/B28359_01/server.111/b28310/tables003.htm#i1006400

In most cases you do not need it. In Oracle when you need temporary table then "your design is wrong". Do not try to rewrite MS SQL pattern into Oracle which exact wording. Where you use temporary table in MS SQL you use in Oracle CTE(nested subquery, query factoring) a CURSOR or some PL/SQL construct.
The temporary table is not what you need. It's just a tool you use to achieve some goal. In Oracle you should use other tools.

Use an Associative Array :)
declare
type temp_rec is record(v integer);
type temp_table is table of temp_rev indexed by pls_integer;
my_temp_table temp_table;
begin
-- Here you can do do your stuff :)
end
/

Related

Tsql table variable to Oracle PLSql

I am working on TSql to Plsql conversion.
There are several table variables declared and used in tsql, like
DECLARE #table_var table( id_ int ......)
WHILE ...
begin
insert into #table_var select ...
...
select * from #table_var..
I think in oracle global temporary table can be used, but the data will be maximum 10k rows.
How efficient global temporary tables in this case?
Is there any other way other than global temporary table and table types in Oracle to convert similar sql query?
Oracle version: oracle11g or oracle12c
Depending on your size of the data and task that you want to achieve-- you can use
Pl/sql record/table and bulk processing
hold data in to cursor and loop through it
if you want kind of query update -- you can use in-line view
(preserving primary key)

Moving dependencies (PK, FK and indexes) from one table to another within the same database in Oracle

Please tell me how can I move dependencies (such as PK, FK and indexes) from one table to another within the same database in Oracle? The second table is a copy of the first, only created later for partition reasons. Thank you in advance! :)
You could look at using the dictionary views in oracle, specifically the USER_CONSTRAINTS view. Then either construct a SQL statement dynamically or use DBMS_METADATA.get_ddl procedure to get the ddl for the constraint. You could do a REPLACE on the SQL to replace the original table name and constraint name with a new constraint name and the name of the new table.

TYPE v_id IS table of table#dblink.column%type;

I have table 'test' in schema A and from schema B I want to run the test query.
so created DBlink to A.test and dblink name 'dbl_test' .
now, I am able to query as
select * from test#dbl_test;
but when I try to use TYPE v_id IS TABLE OF test#dbl_test.id%TYPE; in plsql procedure block with cursor, it is giving errors as must declare and dbl_test is another database and not able to access.
When I query the select statement it is working why not for TYPE?
Put the column before the #:
TYPE v_id IS TABLE OF test.id#dbl_test%TYPE;
Database links are used to link two databases, not schemas.
In your case it is unclear if you really need a database link. Are your two schemas in the same database? If they are, you need only give the relevant rights to schema B and he will be able to reference schema A's datatype. I Think GRANT SELECT ON A.TEST TO B should be sufficient to reference its datatype.
Edit: it is actually possible to reference a remote datatype (I didn't know!) See #jonearles' answer.

ORACLE PL/SQL - SCHEDULE CREATE TABLE

I need to create a table every morning based on overnight generated data from a massive table. This will then be accessed by a handful of users form Excel.
My initial approach was to use a materilazed view and when this was rejected (for political reasons) to used Managed XLL but this was rejected for other reasons. I don't want to get messed up with Temporary tables and so really just need to know how to schedule an Oracle Create Table statement as our DBA says it can't be done.
My faith in SO users sasy otherwise though!
I don't see why you have to create a new table every morning and not use an existing one?
This creates your table from PL/SQL. Is this what you want?
CREATE OR REPLACE PROCEDURE make_table AS
BEGIN
EXECUTE IMMEDIATE 'CREATE TABLE your_table ( column_1 INT PRIMARY KEY, column_2 VARCHAR2(10) )';
END make_table;
/
EXEC make_table;
Your user needs to have the necessary grants, grants given by role don't apply to compiled PL/SQL code.

Stored Procedure for copying data from one table to another

I have pairs of tables in the format TABLE and TABLE_TWIN now
TABLE is the main table with lots of data
TABLE_TWIN is a table with the exact same fields with a little data (different data)
Now I would like to copy all rows from TABLE_TWIN to TABLE using a stored procedure. I have many such tables and could like the stored procedure to take the table name(s) as parameter(s) so that I can use the same procedure for each table pair. I do not want to write long INSERT statements because these tables have around 50 attributes each.
I am not good with PL/SQL so I need some help here.
Thanks!
SQL is not so long... But if you prefer a procedure, here it is:
create or replace procedure table_copy(
p_tab_from varchar2,
p_tab_to varchar2)
is
begin
execute immediate 'insert into '||p_tab_to||' (select * from '||p_tab_from||')';
end;
insert into table_twin (select * from table)
should do it

Resources