Inserting data into Oracle table (SQL) - sql

I already have a table built in oracle.
Im trying to insert some data like this:
INSERT INTO movies_actor('name','id')
VALUES ('Nuno','2'), ('Pedro','3'), ('Jose','1');
select * from movies_actor;
I always get this error
ORA-00928: missing SELECT keyword
What am I doing wrong?

I don't think you need the single quote around your field names.

You need to do:
INSERT INTO TableName(Column1, Column2)
VALUES('Nuno', '2');
In your example, it would be:
INSERT INTO movies_actor(name, id)
VALUES ('Nuno','2');
INSERT INTO movies_actor(name, id)
VALUES ('Pedro','3');
INSERT INTO movies_actor(name, id)
VALUES ('Jose','1');
select * from movies_actor;

Another way.
insert into table
(field1, field2)
select value1, value2
from dual
union
select value3, value4
from dual
etc

You cannot insert multiple records in one statement using VALUES. You can either use Tenzin's solution or use INSERT ALL :
INSERT ALL
INTO movies_actor(name, id) VALUES ('Nuno', '2')
INTO movies_actor(name, id) VALUES ('Pedro', '3')
INTO movies_actor(name, id) VALUES ('Jose', '1')
SELECT * FROM dual;

Related

Insert multiple records in Oracle Database

Example:
INSERT INTO TABLE ( USERID, USERNAME) VALUES (1,"ok1"),(2,"ok2");
As i understood ,Multiple rows insertion not allowed in Oracle Database.
Please confirm me if any other alternatives for inserting multiple records into oracle DB with above format.
Oracle only allows one row to be inserted at a time. So use two inserts:
INSERT INTO TABLE ( USERID, USERNAME)
VALUES (1, 'ok1');
INSERT INTO TABLE ( USERID, USERNAME)
VALUES (2, 'ok2');
Or use INSERT . . . SELECT:
INSERT INTO TABLE ( USERID, USERNAME)
SELECT 1 'ok1' FROM DUAL UNION ALL
SELECT 2, 'ok2' FROM DUAL;

Insert using IF/ELSE statements

I am sorry if my question is not clear or my query is not sufficient to help. I have a procedure that has multiple if/else statement. My goal is to insert one row if that if statements meets the criteria else go further. Something like this:
create or replace procedure abc.xyz
( i_name varchar2
,number number,
sections varchar2)
...
max_date date;
min_date date;
...
if(sum=0)
insert into abc_table
(id,name,number,sections,description,date,amount,price,source,latest_date)
select user_seq.nextval,name,number,max_date,amount,0
,'xyz',trunc(sysdate))
from abc_table x
where x.name=i_name
and x.number=i_name
and x.section=i_section;
elseif (sum>0)
insert into abc_table
(id,name,number,sections,description,date,amount,price,source,latest_date)
select user_seq.nextval,name,number,max_date,amount,0
,'xyz',trunc(sysdate))
from abc_table x
where x.name=i_name
and x.number=i_name;
and x.section=i_section;
when i run my procedure, to insert the calculated value , the values are correct but so many rows were inserted. How can I prevent from multiple insert and make only one row insert ?
You are doing it wrong.
According to the Oracle documentation, The syntax for the Oracle INSERT statement when inserting a single record using the VALUES keyword is:
INSERT INTO table
(column1, column2, ... column_n )
VALUES
(expression1, expression2, ... expression_n );
But the syntax for the Oracle INSERT statement when inserting multiple records using a SELECT statement is:
INSERT INTO table
(column1, column2, ... column_n )
SELECT expression1, expression2, ... expression_n
FROM source_table
[WHERE conditions];
reference: https://www.techonthenet.com/oracle/insert.php
from the link i shared:
If you don't want to insert duplicate:
INSERT INTO clients
(client_id, client_name, client_type)
SELECT 10345, 'IBM', 'advertising'
FROM dual
WHERE NOT EXISTS (SELECT *
FROM clients
WHERE clients.client_id = 10345);

How to insert conditionally in Oracle?

I've read here that the syntax looks like this:
INSERT
WHEN ([Condition]) THEN
INTO [TableName] ([ColumnName])
VALUES ([VALUES])
ELSE
INTO [TableName] ([ColumnName])
VALUES ([VALUES])
SELECT [ColumnName] FROM [TableName];
But I don't want to provide values from another table. I just want to type them, so I've got:
INSERT
WHEN EXISTS (SELECT 1 FROM FOO WHERE NAME = 'JOE')
THEN
INTO BAR (NAME, AGE)
VALUES ('JOE', 50)
and this produces exception: ORA-00928: missing SELECT keyword.
I want to perform an insert if given value is found in another table.
Using with select works. Your query wasn't working because there is a problem with values keyword when inserting conditionally.
INSERT
WHEN EXISTS (SELECT 1 FROM FOO WHERE NAME = 'JOE')
THEN
INTO BAR (NAME, AGE)
SELECT 'JOE', 50 FROM DUAL
So, I've found an indirect way here and solution for my question would be:
INSERT INTO BAR (NAME, AGE)
SELECT 'JOE', 50
FROM DUAL
WHERE EXISTS (SELECT 1 FROM FOO WHERE NAME = 'JOE')
but it doesn't explain why I have to use SELECT statement in INSERT WHEN

Why this oracle query returning empty result set?

I have two database tables with some demo data like shown below
Create table demo(uuid int, addressname varchar(50));
insert into demo values(1, 'intersportprofi');
insert into demo values(2, 'intersportprofi');
insert into demo values(3, 'intersportprofi');
insert into demo values(4, 'intersportmarket');
insert into demo values(5, 'intersportmarket');
insert into demo values(6, 'intersportmarket');
create table demo_av(uuid int, testid int, name varchar(50), value varchar(50));
insert into demo_av values(1, 1, 'sport','football basketball cricket');
insert into demo_av values(2, 1, 'brand','reebok addidas nike');
insert into demo_av values(3, 2, 'sport','football basketball ');
insert into demo_av values(4, 2, 'brand','reebok addidas ');
I wrote the following query to get the results from those tables, but oracle returning empty result set.
SELECT d.addressname FROM demo d, demo_av dv
WHERE d.uuid = dv.testid AND d.addressname='intersportprofi'
AND REGEXP_LIKE( dv.value, 'reebok') AND REGEXP_LIKE( dv.value, 'cricket')
Why? where i am doing wrong ? Any help will be greatly appriciated
Change this:
AND REGEXP_LIKE( dv.value, 'reebok') AND REGEXP_LIKE( dv.value, 'cricket')
To this:
AND (REGEXP_LIKE( dv.value, 'reebok') OR REGEXP_LIKE( dv.value, 'cricket'))
Because:
You have no record in the "demo_av" table that matches with "reebok" AND "cricket". The operator you need is "OR" and the parantheses are necessary because of existing of the first condition.
UPDATE
Here is the capture screen of the results:
Cheers
Based on your comments, I think you want a query that will search over multiple rows with same testid. This can be done with joins or like this:
SELECT DISTINCT d.addressname
FROM demo AS d
WHERE d.addressname = 'intersportprofi'
AND EXISTS
( SELECT *
FROM demo_av AS dv
WHERE d.uuid = dv.testid
AND dv.value LIKE '%reebok%'
)
AND EXISTS
( SELECT *
FROM demo_av AS dv
WHERE d.uuid = dv.testid
AND dv.value LIKE '%cricket%'
) ;

inserting multiple rows with one insert command

Is it possible to insert more than one row in a table with one insert statement?
I know this will happen if I do:
insert into table ( fields ) select values from another_table
But what if I want to insert:
row 1 - ( a1, b1, c1 )
row 2 - ( a2, b2, c2 )
...
row n - ( an, bn, cn )
with just one insert command?
Two solutions (source : http://appsfr.free.fr/spip.php?article21 ):
INSERT ALL
INTO table (column1, column2)
VALUES (value1, value2)
INTO table (column1, column2)
VALUES (value1, value2)
...etc...
SELECT * FROM DUAL ;
or
INSERT INTO table (column1, column2)
SELECT value1, value2 FROM DUAL UNION ALL
SELECT value1, value2 FROM DUAL UNION ALL
...etc...
SELECT value1, value2 FROM DUAL ;
Insert All
INSERT ALL
INTO mytable (column1, column2, column3) VALUES ('val1.1', 'val1.2', 'val1.3')
INTO mytable (column1, column2, column3) VALUES ('val2.1', 'val2.2', 'val2.3')
INTO mytable (column1, column2, column3) VALUES ('val3.1', 'val3.2', 'val3.3')
SELECT * FROM dual;
INSERT INTO products (product_no, name, price) VALUES
(1, 'Cheese', 9.99),
(2, 'Bread', 1.99),
(3, 'Milk', 2.99);
INSERT INTO College (CustomerID, FirstName, MiddleName, LastName)
SELECT 7, N'Charles', N'Simmons', N'Burns'
UNION ALL
SELECT 9, N'Dominic', N'Fred', N'Einsten'
UNION ALL
SELECT 12, N'Dave', N'William, N'Bryan';
NOTE:
Letter N before each hard coded string value converts string to an NVARCHAR value to match the datatype of the column.
No, this is not possible. As you already stated yourself, it is only possible with a select clause providing the insert values and rows.