I'm trying to write a simple insert into statement using multiple values. The solutions I've seen say to separate each set of values with a comma, however, I am still being met with an error. Here is my SQL statement.
INSERT INTO DIVISION (DIVISION_ID, DIVISION_NAME)
VALUES ('20', 'Quebec'), ('30', 'Ontario');
You can also use this one:
INSERT ALL
INTO DIVISION (DIVISION_ID, DIVISION_NAME) VALUES (20, 'Quebec')
INTO DIVISION (DIVISION_ID, DIVISION_NAME) VALUES (30, 'Ontario')
SELECT * FROM dual;
If DIVISION_ID is a numeric data type:
INSERT INTO DIVISION (DIVISION_ID, DIVISION_NAME)
SELECT 20, 'Quebec' FROM DUAL
UNION ALL
SELECT 30, 'Ontario' FROM DUAL;
Otherwise:
INSERT INTO DIVISION (DIVISION_ID, DIVISION_NAME)
SELECT '20', 'Quebec' FROM DUAL
UNION ALL
SELECT '30', 'Ontario' FROM DUAL;
Related
I have a table like this:
enter image description here
Now, I have get all rows formatnbr = 101 using regexp_like(nbr, expression). I'm using regexp_like (nbr, '^\d((\d)1(\1)2(\1)3)') but I have to write 7 expression with 'OR' in where clause:
select * from tbl where regexp(nbr, '^\d((\d)1(\1)2(\1)3)') or regexp(nbr, '^\d((\d)2(\1)3(\1)4)') or ...
How do I using 1 format likely in description column: regexp_like(nbr, '^\dxyx(y+1)x(y+2)') to select? Help me please!
Output:
enter image description here
create table tbl
(
formatnbr NUMBER not null,
nbr VARCHAR2(7),
description VARCHAR2(50)
);
insert into tbl(formatnbr, nbr, description) values (100, '8123456', 'Dx(x+1)(x+2)(x+3)(x+4)(x+5)');
insert into tbl(formatnbr, nbr, description) values (101, '8111213', 'Dxyx(y+1)x(y+2)');
insert into tbl(formatnbr, nbr, description) values (100, '7456789', 'Dx(x+1)(x+2)(x+3)(x+4)(x+5)');
insert into tbl(formatnbr, nbr, description) values (101, '9232425', 'Dxyx(y+1)x(y+2)');
insert into tbl(formatnbr, nbr, description) values (101, '5565758', 'Dxyx(y+1)x(y+2)');
insert into tbl(formatnbr, nbr, description) values (100, '0456789', 'Dx(x+1)(x+2)(x+3)(x+4)(x+5)');
insert into tbl(formatnbr, nbr, description) values (101, '1454647', 'Dxyx(y+1)x(y+2)');
commit;
Thanks,
For:
Dx(x+1)(x+2)(x+3)(x+4)(x+5) you can list the possible values as:
012345|123456|234567|345678|456789
Dxyx(y+1)x(y+2) you can list the possible y values and use backreferences to match the x values as
(\d)(0(\1)1(\1)2|1(\1)2(\1)3|2(\1)3(\1)4|3(\1)4(\1)5|4(\1)5(\1)6|5(\1)6(\1)7|6(\1)7(\1)8|7(\1)8(\1)9)
So your inserts would be:
create table tbl
(
formatnbr NUMBER not null,
nbr VARCHAR2(7),
description VARCHAR2(110)
);
insert into tbl(formatnbr, nbr, description)
SELECT 100, '8123456', '012345|123456|234567|345678|456789' FROM DUAL UNION ALL
SELECT 101, '8111213', '(\d)(0(\1)1(\1)2|1(\1)2(\1)3|2(\1)3(\1)4|3(\1)4(\1)5|4(\1)5(\1)6|5(\1)6(\1)7|6(\1)7(\1)8|7(\1)8(\1)9)' FROM DUAL UNION ALL
SELECT 100, '7456789', '012345|123456|234567|345678|456789' FROM DUAL UNION ALL
SELECT 101, '9232425', '(\d)(0(\1)1(\1)2|1(\1)2(\1)3|2(\1)3(\1)4|3(\1)4(\1)5|4(\1)5(\1)6|5(\1)6(\1)7|6(\1)7(\1)8|7(\1)8(\1)9)' FROM DUAL UNION ALL
SELECT 101, '5565758', '(\d)(0(\1)1(\1)2|1(\1)2(\1)3|2(\1)3(\1)4|3(\1)4(\1)5|4(\1)5(\1)6|5(\1)6(\1)7|6(\1)7(\1)8|7(\1)8(\1)9)' FROM DUAL UNION ALL
SELECT 100, '0456789', '012345|123456|234567|345678|456789' FROM DUAL UNION ALL
SELECT 101, '1454647', '(\d)(0(\1)1(\1)2|1(\1)2(\1)3|2(\1)3(\1)4|3(\1)4(\1)5|4(\1)5(\1)6|5(\1)6(\1)7|6(\1)7(\1)8|7(\1)8(\1)9)' FROM DUAL;
db<>fiddle here
This question already has answers here:
Best way to do multi-row insert in Oracle?
(9 answers)
Closed 1 year ago.
INSERT into L5_DIRECTORS (director_id, first_name, last_name)
Values
(1010, 'Rob', 'Minkoff'),
(1020, 'Bill', 'Condon'),
(1050, 'Josh', 'Cooley'),
(2010, 'brad', 'bird'),
(3020, 'lake', 'bell');
EDIT: I figured it out, I had to write each row individually and run it, I do not why it would not work all together but thanks for the help anyway.
Oracle does not allow you to insert multiple rows using the VALUES syntax. I recommend just using insert . . select:
INSERT into L5_DIRECTORS (director_id, first_name, last_name)
SELECT 1010, 'Rob', 'Minkoff' FROM DUAL UNION ALL
SELECT 1020, 'Bill', 'Condon' FROM DUAL UNION ALL
SELECT 1050, 'Josh', 'Cooley' FROM DUAL UNION ALL
SELECT 2010, 'brad', 'bird' FROM DUAL UNION ALL
SELECT 3020, 'lake', 'bell' FROM DUAL;
Why do I keep getting this error message
ORA-00928: missing SELECT keyword
This is the query I have...
INSERT ALL
INTO ACADEMIC_SESSION (SESSIONID, SESSIONNAME) VALUES (200, 'FALL SESSION'),
INTO ACADEMIC_SESSION (SESSIONID, SESSIONNAME) VALUES (300, 'SUMMER SESSION')
SELECT * from DUAL;
I can't see what is wrong with it.
Wrong syntax; use one of these:
SQL> create table academic_session
2 (session_id number,
3 session_name varchar2(20));
Table created.
SQL> insert into academic_session (session_id, session_name)
2 select 200, 'fall session' from dual union all
3 select 300, 'summer session' from dual;
2 rows created.
SQL> insert all
2 into academic_session (session_id, session_name)
3 values (400, 'spring session')
4 into academic_session (session_id, session_name)
5 values (500, 'winter session')
6 select * from dual;
2 rows created.
SQL>
There is no comma , between the INTO statements
INSERT ALL
INTO ACADEMIC_SESSION (SESSIONID, SESSIONNAME) VALUES (200, 'FALL SESSION')
INTO ACADEMIC_SESSION (SESSIONID, SESSIONNAME) VALUES (300, 'SUMMER SESSION')
SELECT * from DUAL;
If you need to insert two rows into the table, you may do so like this:
INSERT INTO ACADEMIC_SESSION (SESSIONID, SESSIONNAME)
VALUES (200, 'FALL SESSION'), (300, 'SUMMER SESSION');
Hope this helps.
I an trying to INSERT multiple rows into an SQL, Oracle table though SQL Developer v3.0.04
the Database was set-up by Uni so I don't know what version it is.
after looking on-line I have come up with the code below but it will not INSERT any data. I have tested the insert with just one row and that is OK. What am I missing?
Insert all
Into Patient Values
('101', '1 house', Null, 'Kingston', 'Surrey', 'KT1 1XX', '10/jan/1980', 'm', 01452987456)
Into Patient Values
('102', '2 egg rd', 'vail', 'guildford', 'Surrey', 'GU1 1LL', '05/dec/1985', 'm', 01452987456)
Into Patient Values
('103', '6 station rd', Null, 'guildford', 'Surrey', 'GU1 2XX', '15/may/1990', 'f', 01452987456)
Select * from Patient;
INSERT ALL has two different uses. One is to insert different sub-sets of selected columns into a table. The other is to direct rows into different according to certain criteria. In both cases the data comes from a SELECT clause rather than from VALUES. See the examples in the documentation.
Normally, you would simply write multiple INSERT statements potentially in a single PL/SQL block. Something like
begin
Insert Into Patient Values
('101', '1 house', Null, 'Kingston', 'Surrey', 'KT1 1XX', '10/jan/1980', 'm', 01452987456);
Insert Into Patient Values
('102', '2 egg rd', 'vail', 'guildford', 'Surrey', 'GU1 1LL', '05/dec/1985', 'm', 01452987456);
Insert Into Patient Values
('103', '6 station rd', Null, 'guildford', 'Surrey', 'GU1 2XX', '15/may/1990', 'f', 01452987456);
end;
/
If you really want to do this in a single SQL statement, you can do an INSERT ... SELECT but that's generally going to be more complex than using three separate statements.
insert into patient
select *
from (select '101' id, '1 house' addr, null col1, 'Kingston' city, ...
from dual
union all
select '102', '2 egg rd', 'vail', 'guildford', 'Surrey', 'GU1 1LL', '05/dec/1985', 'm', 01452987456
from dual
union all
select '103', '6 station rd', Null, 'guildford', 'Surrey', 'GU1 2XX', '15/may/1990', 'f', 01452987456
from dual)
I would also caution you to use proper data types and to specify the column names in your INSERT statement. I'm guessing, for example, that the first column of Patient table is some sort of PatientID that is defined as a NUMBER. If so, you'd really want to insert a number rather than a character string. Similarly, the seventh column with values like '15/may/1990' is probably defined as a DATE in the table. If so, your INSERT should insert a DATE not a character string by either explicitly calling TO_DATE with a particular format mask or by using the ANSI date format, i.e. date '1980-01-10'. And if you want the last column to retain the leading 0, you'll need to ensure that the column in the database is defined as a VARCHAR2 and that you insert a character string rather than a number.
Interesting. It is possible to use insert all to insert multiple rows as you are attempting. Not that I would recommend doing so over Justin's suggested solutions.
The syntax diagram for multi-table insert indicates that a subquery is always required. However, you don't have to use any of the results of the subquery:
SQL> drop table t;
Table dropped.
SQL> create table t (c1 number, c2 varchar2(10));
Table created.
SQL> insert all into t (c1, c2) values (1, 'one')
2 into t (c1, c2) values (2, 'two')
3 select * from dual;
2 rows created.
SQL> select * from t;
C1 C2
---------- ----------
1 one
2 two
The two into ... values(...) will cause two rows to be inserted per row in the the subquery:
SQL> insert all into t (c1, c2) values (1, 'one')
2 into t (c1, c2) values (2, 'two')
3 select * from dual
4 connect by level <= 10;
20 rows created.
Replace this query [ Select * from Patient; ] with
Select * from dual;
dual is a virtual table which is not existing in our schema but it can be existed as a part of Oracle perspective in RAM not in our storage
Insert all
Into Patient Values
('101', '1 house', Null, 'Kingston', 'Surrey', 'KT1 1XX', '10/jan/1980', 'm', 01452987456)
Into Patient Values
('102', '2 egg rd', 'vail', 'guildford', 'Surrey', 'GU1 1LL', '05/dec/1985', 'm', 01452987456)
Into Patient Values
('103', '6 station rd', Null, 'guildford', 'Surrey', 'GU1 2XX', '15/may/1990', 'f', 01452987456)
Select * from dual;
I am using to using SQL Server. I'm trying to figure out how to insert multiple rows with one query.
In MySQL the query would be like this:
Code:
INSERT INTO Mytable (Name, Number) VALUES ('Joe', 18), ('Bob', 25), ('Mike', 7);
I tried a query like the one above in SQL Server and it gave me an error that said:
Incorrect syntax near ','.
Is there a way to do this in SQL Server?
That syntax will work in SQL 2008; in SQL 2005, you have to do SELECTs and UNIONs
INSERT INTO Mytable (Name, Number)
SELECT 'Joe', 18
UNION ALL SELECT 'Bob', 25
UNION ALL SELECT 'Mike', 7
INSERT INTO sample (ID, Name)
VALUES (001, 'happy'),
(002, 'sunny'),
(125, 'rajesh')