Insert with select max fail in sql for Oracle - sql

When use a bunch of INSERT statements as below it takes forever :
INSERT INTO my_table ( col1, col2, id_col) VALUES ('val1', 'val1', (select max(my_table_ID) from my_table)+1);
If I run one by one and commit then it works fine. What is the reason?
I know sequence should be used in production. But I am writing this to insert few rows in toad.

may be
INSERT INTO my_table ( col1, col2, id_col)
VALUES ('val1', 'val1', (select max(my_table_ID)+1 from my_table));
or in pl/sql block
declare
v_max number(10);
begin
select max(my_table_id) + 1 into v_max
from my_table;
insert into my_table ( col1, col2, id_col)
values ('val1', 'val1', v_max);
end;
/
but, i don't know you task...
may be used sequence + trigger before insert?

Related

INSERT INTO tableName SELECT * INTO tabkeName FROM tableName

How will this below query execute and is there a better way to write this query?
INSERT INTO tableName
SELECT *
INTO tableName
FROM tableName
I would use:
INSERT INTO tableName (col1, col2, col3)
SELECT col1, col2, col3
FROM someOtherTable;
Note that you should generally always explicitly specify which columns you want both for the insert and the select. While your code might run as is, it is prone to breaking should the structure for either table change in the future.

How do I insert multiple rows into a table for every row from another table

I have two tables: TB1 and TB2.
For every row from TB1, I want to insert N number of rows into TB2 using some values from TB1. How do I do that?
For example:
For each row in TB1
{
For N in 1-10
{
insert into TB2 (col1, col2, col3, col4)
values (N, TB1.Col1, 'Good job', TB1.Col2)
}
commit;
}
You can use cross join. Set up a table of numbers and then:
with n as (
select 1 as n union all select 2 . . . union all select 10
)
insert into tb2 (col1, col2, col3, col4)
select n.n, TB1.Col1, 'Good job', TB1.Col2
from tb1 cross join
n;
Some things to note:
The syntax for creating n varies by database. For instance, MySQL doesn't support CTEs, so it needs to be a subquery instead. Oracle requires from dual, and so on.
For inserting rows from one table to another, you want insert . . . select, rather than insert . . . values for every row.
Finally, a generic SQL approach uses 10 passes on tb1:
insert into tb2 (col1, col2, col3, col4)
select 1, TB1.Col1, 'Good job', TB1.Col2
from tb1
union all
select 2, TB1.Col1, 'Good job', TB1.Col2
from tb1
union all
. . .
select 10, TB1.Col1, 'Good job', TB1.Col2
from tb1 ;
Create another table (permanent or temporary) with the N numbers that you want to use, then cross join this list with TB1. If that table is named n_list with a column named N, then:
insert into TB2
(col2, col2, col3, col4)
select
N,
TB1.Col1,
'Good job',
TB1.Col2
from
TB1
cross join n_list;
While waiting for suggestions, I decided to poke around and ended up using a cursor and a for loop like this and it works for me...though not sure how sufficient it is. And thanks for all the suggestions.
DECLARE
CURSOR myCursor IS
select col1, col2 from TB1;
tmp_rec myCursor%rowtype;
BEGIN
FOR tmp_rec in myCursor
LOOP
FOR N IN 1 .. 10
LOOP
insert into TB2 (col1, col2, col3, col4)
values (N, TB1.col1, 'Good job', TB1.col2);
END LOOP;
COMMIT;
END LOOP;
END;

Oracle) Insert multiple rows with one fixed value

I'd like to insert these values in the following fashion:
insert into table (name, action-id) values ('user', select action from actions where name='user2');
The result being:
Inserts along the line of, ('user', 1) ('user', 2) ('user', 3)
I'm noticing this isn't correct sql.
How would I go about accomplishing this?
note)
select action from actions where name='user2'
would return: (1, 2, 3)
You can do it with a loop:
BEGIN
FOR x IN (select action from actions where name='user2') LOOP
insert into table (name, action-id) values ('user', x.action)
END LOOP;
END;
or you could use the INSERT/SELECT syntax:
INSERT INTO table (name, action-id)
SELECT 'user', action
FROM actions WHERE name='user2';
Add the fixed value as a column in your query, and use insert-select instead of insert-values:
insert into table (name, action-id)
select 'user', action from actions where name='user2';
Or can be done by procedure
Create that procedure and run it
create or replace procedure set_action
as
cursor c1 is
select * from user;
person c1%rowtype;
username varchar(8);
begin
username:='user';
for person in c1 loop
insert into table(name,action-id)
values (username,person.action);
end loop;
end;
It can be run by execute set_action;
Example:
create table testing(col1 varchar2(10), col2 varchar2(10));
create table testing2(col1 varchar2(10), col2 varchar2(10));
create table testing3(col1 varchar2(10), col2 int);
insert into testing2 (col1, col2) values ('test2_col1', 'test2_col2');
insert into testing3(col1, col2) values ('steve', 1);
insert into testing3(col1, col2) values ('brad', 2);
insert into testing3(col1, col2) values ('chad', 3);
insert into testing3(col1, col2) values ('nick', 1);
insert into testing(col1, col2)
(select col1 ,(select col2 from testing2) from testing3); -- inserts 4 rows
And finally:
select * from testing;
steve test2_col2
brad test2_col2
chad test2_col2
nick test2_col2

how to use multiple insert in sql navigator

i am new to sql and i am trying to make a simple to use insert for every day use,
i have a table(matrix) that holds the connection between 3 variables ,
in most cases i have to insert or update the matrix but each insert is multiplied because of the matrix, i have made a simple example :
SELECT * FROM table_name
where col1='A'
and col2 in ('1G','2F','3Q')
and col3 ='B'
/
INSERT INTO table_name VALUES('A','1G','B');
INSERT INTO table_name VALUES('A','2F','B');
INSERT INTO table_name VALUES('A','3Q','B');
the output table will be
A 1G B
A 2F B
A 3Q B
and in the more complex cases
SELECT * FROM table_name
where col1='A'
and col2 in ('1G','2F','3Q')
and col3 in ('B','C')
/
INSERT INTO table_name VALUES('A','1G','B');
INSERT INTO table_name VALUES('A','2F','B');
INSERT INTO table_name VALUES('A','3Q','B');
INSERT INTO table_name VALUES('A','1G','C');
INSERT INTO table_name VALUES('A','2F','C');
INSERT INTO table_name VALUES('A','3Q','C');
the output table will be
A 1G B
A 2F B
A 3Q B
A 1G C
A 2F C
A 3Q C
is there a way to make an insert that will look like this or have similar functionality
for example A :
INSERT INTO table_name VALUES('A',in ('1G',2F','3Q'),'B');
and for example b :
INSERT INTO table_name VALUES('A',in ('1G',2F','3Q'),in ('B','C'));
i usually use about 100 new values at a time in each column that is multiplied because of the matrix
You can combine a select and an insert statement. For example:
INSERT INTO table_name (col1, col2, col3)
SELECT col1, col2, col3
FROM table_x
WHERE col1='A'
AND col2 in ('1G','2F','3Q')
AND col3 ='B'
To do that table_x will need to contain all of the values that you want. If you are asking for a way to write an insert statement that builds a 'table' of values in line using constants then no you can't do that.

How a function/procedure can be called inside a insert statement

How a function/procedure can be called inside a insert statement without command parameters. e.g.
Insert into myTable (....) values (1,2,mySP_or_mySDF, 3,4)
You can also use INSERT INTO... SELECT
INSERT INTO myTable (col1, col2, col3....)
SELECT 1, 2, mySP_or_mySDF(), 3, 4
If you want to INSERT data from a stored procedure, then you will need to create a temp table and insert the result into the temp table first, then you can use the result to insert into the final table.
try
Insert into myTable (....) values (1,2,dbo.mySP_or_mySDF(), 3,4)
You cannot call a stored procedure within a SELECT/UPDATE/INSERT statement, with the exception of the statement SQL:
insert into <whatever>
exec <some sql statement here>;
(and related constructs).
Stored procedures do not really return values. Well, they do return an integer. So, you could do:
declare #retval int;
exec #retval = mysp;
insert into myTable(col1, col2, col3, col4)
select 1, 2, #retval, 3, 4
You could have the stored procedure "return" all 4 values, by including the following statement in the sp body:
select 1, 2, <whatever>, 3, 4
and then doing
insert into myTable(col1, col2, col3, col4)
exec mysp;