How to insert multiple rows in the same table-Oracle 10g - sql

I created a table in Oracle SQL :
create table t1
(
empno number(6) PRIMARY KEY,
empname varchar(30),
hiredate date,
basic number(8),
deptno number(4)
);
And now I am inserting values into the table using a single query:
insert into t1 values((131309,'HP','20-FEB-04',2000000,1235)
(131310,'HT','20-APR-14',120020,1234));
But this shows error:
insert into t1 values((131309,'HP','20-FEB-04',2000000,1235),
*
ERROR at line 1:
ORA-00907: missing right parenthesis
How do I correct this?

An INSERT VALUES statement always inserts exactly 1 row. If you want to insert multiple rows with hard-coded values, the most common approach would simply be to execute two separate INSERT statements.
insert into t1 values(131309,'HP','20-FEB-04',2000000,1235);
insert into t1 values(131310,'HT','20-APR-14',120020,1234);
If you really wanted to, you could select your hard-coded values from dual and then do an INSERT SELECT
insert into t1
select 131309, 'HP', '20-FEB-04',2000000,1235 from dual
union all
select 131310,'HT','20-APR-14',120020,1234 from dual
Or you could do an INSERT ALL
insert all
into t1 values(131309,'HP','20-FEB-04',2000000,1235)
into t1 values(131310,'HT','20-APR-14',120020,1234)
select * from dual
Personally, I'd just use two statements.
Although this isn't related to your question, a couple of comments
Always, always list out the columns in your insert statement. You'll make your SQL much more robust so that if you add new columns in the future that allow NULL values your statements will still work. And you'll avoid lots of bugs when the column list is right there rather than hoping that someone remembers the order of columns in the table.
If you're inserting a value into a date column, use a date not a string literal that represents a date. Relying on implicit data type conversion is a source of many bugs. Use an explicit to_date or use ANSI date literals. And use 4-digit years.

Related

Does Oracle allow an SQL INSERT INTO using a SELECT statement for VALUES if the destination table has an GENERATE ALWAYS AS IDENTITY COLUMN

I am trying to insert rows into an Oracle 19c table that we recently added a GENERATED ALWAYS AS IDENTITY column (column name is "ID"). The column should auto-increment and not need to be specified explicitly in an INSERT statement. Typical INSERT statements work - i.e. INSERT INTO table_name (field1,field2) VALUES ('f1', 'f2'). (merely an example). The ID field increments when typical INSERT is executed. But the query below, that was working before the addition of the IDENTITY COLUMN, is now not working and returning the error: ORA-00947: not enough values.
The field counts are identical with the exception of not including the new ID IDENTITY field, which I am expecting to auto-increment. Is this statement not allowed with an IDENTITY column?
Is the INSERT INTO statement, using a SELECT from another table, not allowing this and producing the error?
INSERT INTO T.AUDIT
(SELECT r.IDENTIFIER, r.SERIAL, r.NODE, r.NODEALIAS, r.MANAGER, r.AGENT, r.ALERTGROUP,
r.ALERTKEY, r.SEVERITY, r.SUMMARY, r.LASTMODIFIED, r.FIRSTOCCURRENCE, r.LASTOCCURRENCE,
r.POLL, r.TYPE, r.TALLY, r.CLASS, r.LOCATION, r.OWNERUID, r.OWNERGID, r.ACKNOWLEDGED,
r.EVENTID, r.DELETEDAT, r.ORIGINALSEVERITY, r.CATEGORY, r.SITEID, r.SITENAME, r.DURATION,
r.ACTIVECLEARCHANGE, r.NETWORK, r.EXTENDEDATTR, r.SERVERNAME, r.SERVERSERIAL, r.PROBESUBSECONDID
FROM R.STATUS r
JOIN
(SELECT SERVERSERIAL, MAX(LASTOCCURRENCE) as maxlast
FROM T.AUDIT
GROUP BY SERVERSERIAL) gla
ON r.SERVERSERIAL = gla.SERVERSERIAL
WHERE (r.LASTOCCURRENCE > SYSDATE - (1/1440)*5 AND gla.maxlast < r.LASTOCCURRENCE)
) )
Thanks for any help.
Yes, it does; your example insert
INSERT INTO table_name (field1,field2) VALUES ('f1', 'f2')
would also work as
INSERT INTO table_name (field1,field2) SELECT 'f1', 'f2' FROM DUAL
db<>fiddle demo
Your problematic real insert statement is not specifying the target column list, so when it used to work it was relying on the columns in the table (and their data types) matching the results of the query. (This is similar to relying on select *, and potentially problematic for some of the same reasons.)
Your query selects 34 values, so your table had 34 columns. You have now added a 35th column to the table, your new ID column. You know that you don't want to insert directly into that column, but in general Oracle doesn't, at least at the point it's comparing the query with the table columns. The table has 35 columns, so as you haven't said otherwise as part of the statement, it is expecting 35 values in the select list.
There's no way for Oracle to know which of the 35 columns you're skipping. Arguably it could guess based on the identity column, but that would be more work and inconsistent, and it's not unreasonable for it to insist you do the work to make sure it's right. It's expecting 35 values, it sees 34, so it throws an error saying there are not enough values - which is true.
Your question sort of implies you think Oracle might be doing something special to prevent the insert ... select ... syntax if there is an identity column, but in facts it's the opposite - it isn't doing anything special, and it's reporting the column/value count mismatch as it usually would.
So, you have to list the columns you are populating - you can't automatically skip one. So you statement needs to be:
INSERT INTO T.AUDIT (IDENTIFIER, SERIAL, NODE, ..., PROBESUBSECONDID)
SELECT r.IDENTIFIER, r.SERIAL, r.NODE, ..., r.PROBESUBSECONDID
FROM ...
using the actual column names of course if they differ from the query column names.
If you can't change that insert statement then you could make the ID column invisible; but then you would have to specify it explicitly in queries, as select * won't see it - but then you shouldn't rely on * anyway.
db<>fiddle

ora-01722 - i know what is means but its still not the problem

I'm trying to insert data from an old table to a new one but i have a error ora-01722
I try to do: to_number(trav_no) because in the old table trav_no is varchar and in the new one its number(3,0)
What should I do?
Select trav_no,
edit_cod,
amount,
user_id
from t1;
insert into t2 (trip_id, type_id,f_amount, upd_by)
select to_number(trav_no),
to_number(edit_cod),
amount,
user_id
from t1;
Obviously, your old table contains values that cannot be converted to a number. You need to identfiy the affected rows to either manually fix those values or exclude them in your INSERT statement.
The easiest way to identify them is to select them using regular expressions:
SELECT *
FROM T1
WHERE NOT REGEXP_LIKE(trav_no, '^[0-9]+$')
OR NOT REGEXP_LIKE(edit_cod, '^[0-9]+$');
The query assumes that the numbers must be positive integer numbers. If not, refine the regular expression accordingly.

JOOQ: extract value from Field

I have a table with PK and another column for other id. In some cases i need to insert record with equal values in both columns. For primary key values i'm using sequence, which gives a Field<Long> from Sequences.MY_SEQ.nextval().
How can i extract value from a Field<Long> for guaranteed insert same ids in both columns? Using Field<Long> in insert clause generates 2 different ids in columns.
Here is the solution:
Long id = dsl.select(Sequences.MY_SEQ.nextval()).fetchOne().value1();
Your own solution works, of course, but it will generate two round trips to the database. One for fetching the sequence value and another one for the insert. If that's not a problem, perfect. Otherwise, you can still do it in one single query using INSERT .. SELECT:
In SQL:
(using Oracle syntax. Your SQL syntax may vary...)
INSERT INTO my_table (col1, col2, val)
SELECT t.id, t.id, 'abc'
FROM (
SELECT my_seq.nextval AS id
FROM dual
) t
With jOOQ
Table<?> t = table(select(MY_SEQ.nextval().as("id"))).as("t");
dsl.insertInto(MY_TABLE)
.columns(MY_TABLE.COL1, MY_TABLE.COL2, MY_TABLE.VAL)
.select(
select(t.field("id"), t.field("id"), val("abc"))
.from(t))
.execute();

SELECT * FROM NEW TABLE equivalent in Postgres

In DB2 I can do a command that looks like this to retrieve information from the inserted row:
SELECT *
FROM NEW TABLE (
INSERT INTO phone_book
VALUES ( 'Peter Doe','555-2323' )
) AS t
How do I do that in Postgres?
There are way to retrieve a sequence, but I need to retrieve arbitrary columns.
My desire to merge a select with the insert is for performance reasons. This way I only need to execute one statement to insert values and select values from the insert. The values that are inserted come from a subselect rather than a values clause. I only need to insert 1 row.
That sample code was lifted from Wikipedia Insert Article
A plain INSERT ... RETURNING ... does the job and delivers best performance.
A CTE is not necessary.
INSERT INTO phone_book (name, number)
VALUES ( 'Peter Doe','555-2323' )
RETURNING * -- or just phonebook_id, if that's all you need
Aside: In most cases it's advisable to add a target list.
The Wikipedia page you quoted already has the same advice:
Using an INSERT statement with RETURNING clause for PostgreSQL (since
8.2). The returned list is identical to the result of a SELECT.
PostgreSQL supports this kind of behavior through a returning clause in a common table expression. You generally shouldn't assume that something like this will improve performance simply because you're executing one statement instead of two. Use EXPLAIN to measure performance.
create table test (
test_id serial primary key,
col1 integer
);
with inserted_rows as (
insert into test (c1) values (3)
returning *
)
select * from inserted_rows;
test_id col1
--
1 3
Docs

Inserting into One Field Select statement Results

I am using Oracle databases. I have an sql table PS_Z_STAGE_TEST_JE that has three fields (EMPLID, LAST_NAME, FIRST_NAME).
I am trying to do a select statement that will pull many EMPLIDs from sql table:ps_vc_plan_mem and insert them into the EMPLID column while leaving the other two fields (LAST_NAME and FIRST_NAME) null.
Below is my SQL but it will say
Cannot insert null Value into LAST_NAME
when I try to run it.
INSERT INTO sysadm.PS_Z_STAGE_TEST_JE (EMPLID)
SELECT DISTINCT(emplid)
FROM ps_vc_plan_mem
WHERE vc_plan_id IN ('PNC-RS','PNC-SO','PNC-ESPP');
The only obvious problem that I see with your query is the asterisk at the end:
INSERT INTO sysadm.PS_Z_STAGE_TEST_JE(EMPLID)
SELECT DISTINCT emplid
FROM ps_vc_plan_mem
WHERE vc_plan_id IN ('PNC-RS', 'PNC-SO', 'PNC-ESPP');
Note that distinct is not a function. It is a modifier on select, so the parentheses don't mean anything.
The error message is exactly what it says it is. Your last_name column must be defined as not null. Therefore, you can't insert a null into it. Since you didn't define what to insert into the column in your insert, it tries to insert null by default and fails.
You must insert something into last name. I would suggest either a default string or an empty string if you can't get an actual last name to insert.
INSERT INTO sysadm.PS_Z_STAGE_TEST_JE (EMPLID, LAST_NAME)
SELECT DISTINCT(emplid), 'N/A'
FROM ps_vc_plan_mem
WHERE vc_plan_id IN ('PNC-RS','PNC-SO','PNC-ESPP');
Alternatively, you could alter your table so that last_name is nullable.
There is a asterisk at the end of your SQL statement. Please remove and retry.
If [sysadm].[PS_Z_STAGE_TEST_JE] table has a PK, you might want truncate the table before running the statement.