insert 1000+ long numbers into oracle temp table - oracle

I was doing a select operation on my table in oracle but was getting ORA-01795 so,
then I try inserting my values in the list of order 1000+ (890623250,915941020,915941021,....1000+ times) into temp table and I can't figure it out how to do it so that later I can do a select from a temp table
So basically my objective is to insert those 1000 id into the temp table of schema
TEMP_L{ID INTEGER} like INSERT INTO TEMP_LINK SELECT(890623254,915941020,1000+ values )

Use a collection. SYS.ODCINUMERLIST is a built-in VARRAY:
INSERT INTO TEMP_LINK ( value )
SELECT COLUMN_VALUE
FROM TABLE( SYS.ODCINUMBERLIST( 890623254,915941020,1000 /* + values */ ) );
Or you can define your own collection:
CREATE TYPE NumberList IS TABLE OF NUMBER;
INSERT INTO TEMP_LINK ( value )
SELECT COLUMN_VALUE
FROM TABLE( NumberList( 890623254,915941020,1000 /* + values */ ) );
However, if you are going to use collections then you don't need to load them into a temp table:
SELECT *
FROM your_table
WHERE your_id MEMBER OF NumberList( 890623254,915941020,1000 /* + values */ )
or
SELECT *
FROM your_table
WHERE your_id IN (
SELECT COLUMN_VALUE
FROM TABLE( 890623254,915941020,1000 /* + values */ )
);

Preferably use SQL* Loader for bulk inserts. One other option would be to construct a query using Excel or notepad++ for all the ids.
INSERT INTO mytable(id)
select 890623250 FROM DUAL UNION ALL
select 915941020 FROM DUAL UNION ALL
...
..

Related

INSERT … SELECT : missing SELECT keyword

I am experimenting with a recursive CTE to split a string into multiple values. The data is then inserted into another table.
The following works in PostgreSQL:
CREATE TABLE test(data varchar(255));
WITH RECURSIVE
cte(genres) AS (SELECT 'apple,banana,cherry,date'),
split(genre,rest,genres) AS (
SELECT '', genres||',',genres FROM cte
UNION ALL
SELECT
substring(rest,0,position(',' IN rest)),
substring(rest,position(',' IN rest)+1),
genres
FROM split WHERE rest<>''
)
INSERT INTO test(data)
SELECT genre
FROM split;
However, the Oracle version:
CREATE TABLE test(data varchar(255));
WITH
cte(genres) AS (SELECT 'apple,banana,cherry,date' FROM dual),
split(genre,rest,genres) AS (
SELECT '', genres||',',genres FROM cte
UNION ALL
SELECT
substr(rest,1,instr(rest,',')-1),
substr(rest,instr(rest,',')+1),
genres
FROM split WHERE rest IS NOT NULL
)
INSERT INTO test(data)
SELECT genre
FROM split WHERE genre IS NOT NULL;
gives me the error message:
ORA-00928: missing SELECT keyword.
Now I’m pretty sure that I’ve got one, there between the INSERT and the following FROM.
If you comment out the INSERT, the rest of it will give the results. Elsewhere, I know that a simple INSERT … SELECT does work.
Is it something to do with the recursive CTE? How can I get this to work properly using a standard recursive CTE?
Using Oracle 18c in a Docker image. There is a fiddle here: https://dbfiddle.uk/?rdbms=oracle_18&fiddle=d24f3105634933af1b7072bded1dacdf .
An INSERT-SELECT means "INSERT" command first, then the SELECT part, and the WITH is part of the SELECT not the insert.
SQL> create table t ( x int );
Table created.
SQL> with blah as ( select 1 c from dual)
2 insert into t
3 select * from blah;
insert into t
*
ERROR at line 2:
ORA-00928: missing SELECT keyword
SQL> insert into t
2 with blah as ( select 1 c from dual)
3 select * from blah;
1 row created.

Avoid using same view multiple times inside oracle procedures

I am using a view named V_ENT_MSG in my oracle procedure "TEST PROC" as shown below. The same view is used in multiple places inside the procedure and the procedure execution time is very high. The view returns more than 5 Million records every time. How can I improve procedure execution time. [ Note that I cannot change the view V_ENT_MSG].
create or replace PROCEDURE "TESTPROC"
AS
BEGIN
INSERT
INTO OUTBOUND1
(
DELIVERY_EVENT_ID,
MESSAGE_ID
)
SELECT
V2.DELIVERY_EVENT_ID,PS.MESSAGE_ID
FROM V_ENT_MSG V2,
R_SOURCE PS
WHERE V2.primary_source = PS.SOURCE;
COMMIT;
INSERT
INTO OUTBOUND2
(
DELIVERY_EVENT_ID,
MESSAGE_BODY
)
SELECT
V2.DELIVERY_EVENT_ID,PS2.MESSAGE_BODY
FROM V_ENT_MSG V2,
R_SOURCE2 PS2
WHERE V2.primary_source = PS2.SOURCE;
COMMIT;
INSERT
INTO OUTBOUND3
(
DELIVERY_EVENT_ID,
SUBJECT
)
SELECT
V2.DELIVERY_EVENT_ID,PS3.SUBJECT
FROM V_ENT_MSG V2,
R_SOURCE3 PS3
WHERE V2.primary_source = PS3.SOURCE;
COMMIT;
END TESTPROC;
You may be able to rewrite this as a multitable insert. To do this, you need to:
(Outer) join the view to each table
insert all the result of this query
Use the when clause to control which rows go in which tables
Which looks something like:
insert all
when ps.source is not null then
into outbound1 ( delivery_event_id, message_id )
values ( delivery_event_id, message_id )
when ps2.source is not null then
into outbound2 ( delivery_event_id, message_body )
values ( delivery_event_id, message_body )
when ps3.source is not null then
into outbound3 ( delivery_event_id, subject )
values ( delivery_event_id, subject )
select v.delivery_event_id,ps3.subject
from v_ent_msg v
left join r_source ps
on v.primary_source = ps.source
left join r_source2 ps2
on v.primary_source = ps2.source
left join r_source3 ps3
on v.primary_source = ps3.source;
This assumes that each primary_source joins to at most one row in r_sourceX. If it's 1:M and each value of primary_source can join to a different number of rows in each of the other tables it gets trickier.
You'll need to assign a row_number for each source value in each r_sourceX table and only insert those rows where this is one.
Additionally whether you have opted for GTT as suggested by #Littlefoot or not ,you can still use this approach.
How about leveraging INSERT ALL with WITH clause to achieve the same ,
INSERT ALL
/* inserting to OUTBOUND1 based on table_name = R_SOURCE defined during union */
WHEN table_name = 'R_SOURCE' THEN
INTO outbound1(delivery_event_id, message_id)
VALUES(delivery_event_id, message_type)
/* inserting to OUTBOUND2 based on table_name = R_SOURCE2 defined during union */
WHEN table_name = 'R_SOURCE2' THEN
INTO outbound2(delivery_event_id, message_body)
VALUES (delivery_event_id, message_type)
/* inserting to OUTBOUND3 based on table_name = R_SOURC3 defined during union */
WHEN table_name = 'R_SOURCE3' THEN
INTO outbound3(delivery_event_id, subject)
VALUES (delivery_event_id, message_type)
WITH ent_msg
AS
(SELECT v2.delivery_event_id,v2.primary_source
FROM v_ent_msg v2),
src_data
AS
( SELECT v2.delivery_event_id delivery_event_id,ps.message_id message_type,'R_SOURCE' table_name
FROM r_source ps
JOIN ent_msg v2
ON v2.primary_source = ps.source
UNION ALL
SELECT v2.delivery_event_id,ps2.message_body,'R_SOURCE2' table_name
FROM r_source2 ps2
JOIN ent_msg v2
ON v2.primary_source = ps2.source
UNION ALL
SELECT v2.delivery_event_id,ps3.subject,'R_SOURCE3' table_name
FROM r_source3 ps3
JOIN ent_msg v2
ON v2.primary_source = ps3.source
)
SELECT delivery_event_id,message_type,table_name
FROM src_data
I couldn't validate SQL because of tables are missing.
There are other ways too in case of performance issue where we can go for BULK COLLECT with LIMIT. I am not aware of the data set you are dealing with.
Hope this guides you proceeding with your question.

Delete data returned from subquery in oracle

I have two tables. if the data in table1 is more than a predefined limit (say 2), i need to copy the remaining contents of table1 to table2 and delete those same contents from table1.
I used the below query to insert the excess data from table1 to table2.
insert into table2
SELECT * FROM table1 WHERE ROWNUM < ((select count(*) from table1)-2);
Now i need the delete query to delete the above contents from table1.
Thanks in advance.
A straightforward approach would be an interim storage in a temporary table. Its content can be used to determine the data to be deleted from table1 as well as the source to feed table 2.
Assume (slightly abusing notation) to be the PK column (or that of any candidate key) of table1 - usually there'll be some key that comprises only 1 column.
create global temporary table t_interim as
( SELECT <pk> pkc FROM table1 WHERE ROWNUM < ((select count(*) from table1)-2 )
;
insert into table2
select * from table1 where <pk> IN (
select pkc from t_interim
);
delete from table1 where <pk> IN (
select pkc from t_interim
);
Alternative
If any key of table1 spans more than 1 column, use an EXISTS clause instead as follows ( denoting the i-th component of a candidate key in table1):
create global temporary table t_interim as
( SELECT <ck_1> ck1, <ck_2> ck2, ..., <ck_n> ckn FROM table1 WHERE ROWNUM < ((select count(*) from table1)-2 )
;
insert into table2
select * from table1 t
where exists (
select 1
from t_interim t_i
where t.ck_1 = t_i.ck1
and t.ck_2 = t_i.ck2
...
and t.ck_n = t_i.ckn
)
;
delete from table1 t where
where exists (
select 1
from t_interim t_i
where t.ck_1 = t_i.ck1
and t.ck_2 = t_i.ck2
...
and t.ck_n = t_i.ckn
)
;
(Technically you could try to adjust the first scheme by synthesizing a key from the components of any CK, eg. by concatenating. You run the risk of introducing ambiguities ( (a bc, ab c) -> (abc, abc) ) or run into implementation limits ( max. varchar length ) using the first method)
Note
In case the table doesn't have a PK, you can apply the technique using any candidate key of table1. There will always be one, in the extreme case it's the set of all columns.
This situation may be the right time to improve the db design and add a (synthetic) pk column to table1 ( and any other tables in the system that lack it).

BULK COLLECT into a table of objects

When attempting to use a BULK COLLECT statement I got error ORA-00947: not enough values.
An example script:
CREATE OR REPLACE
TYPE company_t AS OBJECT (
Company VARCHAR2(30),
ClientCnt INTEGER );
/
CREATE OR REPLACE
TYPE company_set AS TABLE OF company_t;
/
CREATE OR REPLACE
FUNCTION piped_set (
v_DateBegin IN DATE,
v_DateEnd IN DATE
)
return NUMBER /*company_set pipelined*/ as
v_buf company_t := company_t( NULL, NULL);
atReport company_set;
sql_stmt VARCHAR2(500) := '';
begin
select * BULK COLLECT INTO atReport
from (
SELECT 'Descr1', 1 from dual
UNION
SELECT 'Descr2', 2 from dual ) ;
return 1;
end;
The error occurs at the line select * BULK COLLECT INTO atReport.
Straight PL/SQL works fine by the way (so no need to mention it as a solution). Usage of BULK COLLECT into a user table type is the question.
Your company_set is a table of objects, and you're selecting values, not objects comprised of those values. This will compile:
select * BULK COLLECT INTO atReport
from (
SELECT company_t('Descr1', 1) from dual
UNION
SELECT company_t('Descr2', 2) from dual ) ;
... but when run will throw ORA-22950: cannot ORDER objects without MAP or ORDER method because the union does implicit ordering to identify and remove duplicates, so use union all instead:
select * BULK COLLECT INTO atReport
from (
SELECT company_t('Descr1', 1) from dual
UNION ALL
SELECT company_t('Descr2', 2) from dual ) ;

Oracle: how to INSERT if a row doesn't exist

What is the easiest way to INSERT a row if it doesn't exist, in PL/SQL (oracle)?
I want something like:
IF NOT EXISTS (SELECT * FROM table WHERE name = 'jonny') THEN
INSERT INTO table VALUES ("jonny", null);
END IF;
But it's not working.
Note: this table has 2 fields, say, name and age. But only name is PK.
INSERT INTO table
SELECT 'jonny', NULL
FROM dual -- Not Oracle? No need for dual, drop that line
WHERE NOT EXISTS (SELECT NULL -- canonical way, but you can select
-- anything as EXISTS only checks existence
FROM table
WHERE name = 'jonny'
)
Assuming you are on 10g, you can also use the MERGE statement. This allows you to insert the row if it doesn't exist and ignore the row if it does exist. People tend to think of MERGE when they want to do an "upsert" (INSERT if the row doesn't exist and UPDATE if the row does exist) but the UPDATE part is optional now so it can also be used here.
SQL> create table foo (
2 name varchar2(10) primary key,
3 age number
4 );
Table created.
SQL> ed
Wrote file afiedt.buf
1 merge into foo a
2 using (select 'johnny' name, null age from dual) b
3 on (a.name = b.name)
4 when not matched then
5 insert( name, age)
6* values( b.name, b.age)
SQL> /
1 row merged.
SQL> /
0 rows merged.
SQL> select * from foo;
NAME AGE
---------- ----------
johnny
If name is a PK, then just insert and catch the error. The reason to do this rather than any check is that it will work even with multiple clients inserting at the same time. If you check and then insert, you have to hold a lock during that time, or expect the error anyway.
The code for this would be something like
BEGIN
INSERT INTO table( name, age )
VALUES( 'johnny', null );
EXCEPTION
WHEN dup_val_on_index
THEN
NULL; -- Intentionally ignore duplicates
END;
I found the examples a bit tricky to follow for the situation where you want to ensure a row exists in the destination table (especially when you have two columns as the primary key), but the primary key might not exist there at all so there's nothing to select.
This is what worked for me:
MERGE INTO table1 D
USING (
-- These are the row(s) you want to insert.
SELECT
'val1' AS FIELD_A,
'val2' AS FIELD_B
FROM DUAL
) S ON (
-- This is the criteria to find the above row(s) in the
-- destination table. S refers to the rows in the SELECT
-- statement above, D refers to the destination table.
D.FIELD_A = S.FIELD_A
AND D.FIELD_B = S.FIELD_B
)
-- This is the INSERT statement to run for each row that
-- doesn't exist in the destination table.
WHEN NOT MATCHED THEN INSERT (
FIELD_A,
FIELD_B,
FIELD_C
) VALUES (
S.FIELD_A,
S.FIELD_B,
'val3'
)
The key points are:
The SELECT statement inside the USING block must always return rows. If there are no rows returned from this query, no rows will be inserted or updated. Here I select from DUAL so there will always be exactly one row.
The ON condition is what sets the criteria for matching rows. If ON does not have a match then the INSERT statement is run.
You can also add a WHEN MATCHED THEN UPDATE clause if you want more control over the updates too.
Using parts of #benoit answer, I will use this:
DECLARE
varTmp NUMBER:=0;
BEGIN
-- checks
SELECT nvl((SELECT 1 FROM table WHERE name = 'john'), 0) INTO varTmp FROM dual;
-- insert
IF (varTmp = 1) THEN
INSERT INTO table (john, null)
END IF;
END;
Sorry for I don't use any full given answer, but I need IF check because my code is much more complex than this example table with name and age fields. I need a very clear code. Well thanks, I learned a lot! I'll accept #benoit answer.
In addition to the perfect and valid answers given so far, there is also the ignore_row_on_dupkey_index hint you might want to use:
create table tq84_a (
name varchar2 (20) primary key,
age number
);
insert /*+ ignore_row_on_dupkey_index(tq84_a(name)) */ into tq84_a values ('Johnny', 77);
insert /*+ ignore_row_on_dupkey_index(tq84_a(name)) */ into tq84_a values ('Pete' , 28);
insert /*+ ignore_row_on_dupkey_index(tq84_a(name)) */ into tq84_a values ('Sue' , 35);
insert /*+ ignore_row_on_dupkey_index(tq84_a(name)) */ into tq84_a values ('Johnny', null);
select * from tq84_a;
The hint is described on Tahiti.
you can use this syntax:
INSERT INTO table_name ( name, age )
select 'jonny', 18 from dual
where not exists(select 1 from table_name where name = 'jonny');
if its open an pop for asking as "enter substitution variable" then use this before the above queries:
set define off;
INSERT INTO table_name ( name, age )
select 'jonny', 18 from dual
where not exists(select 1 from table_name where name = 'jonny');
You should use Merge:
For example:
MERGE INTO employees e
USING (SELECT * FROM hr_records WHERE start_date > ADD_MONTHS(SYSDATE, -1)) h
ON (e.id = h.emp_id)
WHEN MATCHED THEN
UPDATE SET e.address = h.address
WHEN NOT MATCHED THEN
INSERT (id, address)
VALUES (h.emp_id, h.address);
or
MERGE INTO employees e
USING hr_records h
ON (e.id = h.emp_id)
WHEN MATCHED THEN
UPDATE SET e.address = h.address
WHEN NOT MATCHED THEN
INSERT (id, address)
VALUES (h.emp_id, h.address);
https://oracle-base.com/articles/9i/merge-statement
CTE and only CTE :-)
just throw out extra stuff. Here is almost complete and verbose form for all cases of life. And you can use any concise form.
INSERT INTO reports r
(r.id, r.name, r.key, r.param)
--
-- Invoke this script from "WITH" to the end (";")
-- to debug and see prepared values.
WITH
-- Some new data to add.
newData AS(
SELECT 'Name 1' name, 'key_new_1' key FROM DUAL
UNION SELECT 'Name 2' NAME, 'key_new_2' key FROM DUAL
UNION SELECT 'Name 3' NAME, 'key_new_3' key FROM DUAL
),
-- Any single row for copying with each new row from "newData",
-- if you will of course.
copyData AS(
SELECT r.*
FROM reports r
WHERE r.key = 'key_existing'
-- ! Prevent more than one row to return.
AND FALSE -- do something here for than!
),
-- Last used ID from the "reports" table (it depends on your case).
-- (not going to work with concurrent transactions)
maxId AS (SELECT MAX(id) AS id FROM reports),
--
-- Some construction of all data for insertion.
SELECT maxId.id + ROWNUM, newData.name, newData.key, copyData.param
FROM copyData
-- matrix multiplication :)
-- (or a recursion if you're imperative coder)
CROSS JOIN newData
CROSS JOIN maxId
--
-- Let's prevent re-insertion.
WHERE NOT EXISTS (
SELECT 1 FROM reports rs
WHERE rs.name IN(
SELECT name FROM newData
));
I call it "IF NOT EXISTS" on steroids. So, this helps me and I mostly do so.

Resources