What is the macros of multiple values for INSERT? - sql

INSERT INTO reports (id, date, message)
VALUES ($1, $2, $3)
RETURNING id
Is it possible to replace ($1, $2, $3) with something simpler, like ? or *? It's a bit frustrating - to type in all values.

You could use INSERT INTO SELECT FROM VALUES() syntax:
INSERT INTO reports (id, date, message)
SELECT col1, col2, 'const_value'
FROM (VALUES (...,...),
(...,...)) s(col1, col2, ...,coln)
RETURNING id;
Sample demo:
CREATE TABLE reports(id SERIAL, daten DATE, message TEXT);
INSERT INTO reports ( daten, message)
SELECT col2, 'const_value'
FROM (VALUES ('2019-01-01'::date),
('2019-01-02'::date)) s(col2)
RETURNING id;
SELECT * FROM reports;
db<>fiddle demo

Related

Insert multiple rows in a single query using results of a select statement

I am looking for a compact way to do this - insert multiple rows into a table with values from multiple columns of a row from another table. My destination table is really a list with a single column:
declare #stringList table
(
val nvarchar(100)
)
This is how we can insert multiple rows:
INSERT INTO #stringList ( val ) VALUES
Val1, Val2, Val3, ...
This is how we insert from a select:
INSERT INTO #stringList
SELECT col1 FROM table1 where id=something
But I cannot seem to find a way to use both at the same time.
I can select from one column:
insert into #stringList (val)
select col1 from table1 where id=something
But it doesn't extend to multiple columns:
insert into #stringList (val)
select col1, col2 from table1 where id=something
--The select list for the INSERT statement contains more items than the insert list. The number of SELECT values must match the number of INSERT columns.
I have tried various ways including using parentheses, but the syntax is not accepted:
insert into #stringList (val)
(select col1 from table1 where id=something,
select col2 from table1 where id=something
Any idea if what I want is doable?
You can unpivot using cross apply:
insert into #stringList (val)
select v.col
from table1 t1 cross apply
(values (t1.col1), (t1.col2)) v(col)
where t1.id = something;

How to insert same random value into two columns within one SQL Statement (Oracle)?

My example doesn't work:
INSERT INTO test_table
(column_1, column_2)
VALUES (DBMS_CRYPTO.RANDOMBYTES(16), column_1);
I'm not interested in PL/SQL solutions.
One option is to use scalar subquery caching:
INSERT INTO test_table (column_1, column_2)
SELECT random, random FROM
(SELECT (SELECT dbms_crypto.randombytes(16) FROM dual) random FROM dual);
Or using PL/SQL:
DECLARE
random RAW(16) := dbms_crypto.randombytes(16);
BEGIN
INSERT INTO test_table (column_1, column_2) VALUES (random, random);
END;
Use a subquery:
INSERT INTO test_table(column_1, column_2)
SELECT val, val
FROM (SELECT DBMS_CRYPTO.RANDOMBYTES(16) as val FROM dual) x;
Or, this can be written as:
INSERT INTO test_table(column_1, column_2)
WITH x AS (SELECT DBMS_CRYPTO.RANDOMBYTES(16) as val FROM dual)
SELECT val, val
FROM x;
You could create a temp table to store it?
set echo on
create table test_table (column_1 varchar2(100), column_2 varchar2(100));
create global temporary table rando_val (c1 varchar2(100));
insert
into rando_val
values ( dbms_crypto.randombytes(16) );
INSERT INTO test_table (column_1, column_2)
select c1,
c1
from rando_val;
commit;
select *
from test_table;
Not all that elegant, but it should work.
Try this weird query
select /*+ NO_XML_QUERY_REWRITE */ UTL_RAW.CAST_TO_RAW(VAL1),UTL_RAW.CAST_TO_RAW(VAL2) from
xmltable('for $c in . return <r><val1>{$c}</val1><val2>{$c}</val2></r>'
passing (SELECT UTL_RAW.CAST_TO_VARCHAR2(DBMS_CRYPTO.RANDOMBYTES(16)) as val FROM dual)
columns
"VAL1" varchar2(300) path '/r/val1',
"VAL2" varchar2(300) path '/r/val2' )
If you want then don't covert varchar2 to raw again in last step. Mean this
UTL_RAW.CAST_TO_RAW(VAL1)
This solution works for me:
INSERT INTO test_table(column_1, column_2)
WITH x AS (SELECT /*+ MATERIALIZE */ DBMS_CRYPTO.RANDOMBYTES(16) as val FROM dual)
SELECT val, val
FROM x;
Thanks to Husqvik & Gordon Linoff.

Inserting data into Oracle table (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;

Insert multiple rows of data in a single SQL statement [duplicate]

This question already has answers here:
Inserting multiple rows in a single SQL query? [duplicate]
(4 answers)
Closed 7 years ago.
I have multiple set of data to insert at once
INSERT INTO MyTable VALUES ("John", "Doe", 1234567890, "employee", "");
INSERT INTO MyTable VALUES ("Susen", "Gupta", 1234567890, "leander");
INSERT INTO MyTable VALUES ("Karn", "Share", 1234567890, "employee", "home");
I want to insert multiple rows in a single SQL statement. And can it possible to do it with different number of values.
Multi-row insert has been part of the SQL standard since SQL-92, and many of the modern DBMS' support it. That would allow you to do something like:
insert into MyTable ( Name, Id, Location)
values ('John', 123, 'Lloyds Office'),
('Jane', 124, 'Lloyds Office'),
('Billy', 125, 'London Office'),
('Miranda', 126, 'Bristol Office');
You'll notice I'm using the full form of insert into there, listing the columns to use. I prefer that since it makes you immune from whatever order the columns default to.
If your particular DBMS does not support it, you could do it as part of a transaction which depends on the DBMS but basically looks like:
begin transaction;
insert into MyTable (Name,Id,Location) values ('John',123,'Lloyds Office');
insert into MyTable (Name,Id,Location) values ('Jane',124,'Lloyds Office'),
insert into MyTable (Name,Id,Location) values ('Billy',125,'London Office'),
insert into MyTable (Name,Id,Location) values ('Miranda',126,'Bristol Office');
commit transaction;
This makes the operation atomic, either inserting all values or inserting none.
Yes you can, but it depends on the SQL taste that you are using :) , for example in mysql, and sqlserver:
INSERT INTO Table ( col1, col2 ) VALUES
( val1_1, val1_2 ), ( val2_1, val2_2 ), ( val3_1, val3_2 );
But in oracle:
INSERT ALL
INTO Table (col1, col2, col3) VALUES ('val1_1', 'val1_2', 'val1_3')
INTO Table (col1, col2, col3) VALUES ('val2_1', 'val2_2', 'val2_3')
INTO Table (col1, col2, col3) VALUES ('val3_1', 'val3_2', 'val3_3')
.
.
.
SELECT 1 FROM DUAL;
In SQL Server, you can do this:
INSERT INTO MyTable VALUES ("John", 123, "Lloyds Office"),
("Jane", 124, "Lloyds Office"),
("Billy", 125, "London Office"),
("Miranda", 126, "Bristol Office")

Insert with select max fail in sql for Oracle

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?