Tsql table variable to Oracle PLSql - oracle

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)

Related

Generate insert statement using a stored procedure for all tables in a database

Using a a stored procedure I want to generate insert scripts for all the tables available in database and the table name and the columns must be passed as a parameter, how to do this?

How to change lots table columns datatypes from one to another in oracle

I have a table with a lots of columns with BLOB type and I need to change it to nvarchar2.
So, to change type I can use following script:
alter table AUDIT_LOG
modify
(
column_name type_name,
column_name2 type_name2
-- etc
);
And to get all columns with given datatype I can use the following:
select column_name, 'NVARCHAR2(4000)'
from all_tab_columns
where table_name = 'TAB_NAME' and data_type = 'BLOB';
But how to join this two scripts into one?
You cannot do DML and DDL operation together in same query. You have to use dynamic SQL in a PL/SQL block
Create a variable and generate the whole alter table query in it.
Execute Immidiate
Refer this and I am sure you will be able to add rest of the logic as per your requirement.
http://www.java2s.com/Tutorial/Oracle/0440__PL-SQL-Statements/EXECUTEIMMEDIATEdynamicsqltoaltersession.htm

Convert ntext to clob

I have to copy data from one table to another which one table is in Oracle and one is in MSSQL Server. I want to copy the data from MSSQL Server table to Oracle table. The problem is that the MSSQL Server table has one column which is of data type ntext and the destination column in Oracle table is clob.
When I use the query
insert into oracle.table select * from sqlserver.table#mssql; I get the following error:
SQL Error: ORA-00997: illegal use of LONG datatype
Can anyone advice on this please?
I tried it through a PL/SQL Procedure and it worked. I created a cursor, passed in the values to my variables declared in VARCHAR2 and then run an EXECUTE IMMEDIATE for the INSERT INTO....SELECT * FROM <TABLE_NAME>#MSSQL.

Temporary table in Oracle like SQL Server

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
/

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