Splitting a postgres SQL insert statement into 2 tables using triggers - sql

Is it possible to use postgres triggers to split an INSERT statement into 2 tables? So if you do insert into testtable (col1, col2) values (val1a, val1b), (val2a, val2b), can it be translated with triggers to something like
insert into testtable (col1) values (val1a), (val1b)
insert into anothertable (col2) values (val2a), (val2b)
Basically is it possible for testtable to not have a col2 even though the original SQL INSERT looks like col2 should exist on testtable?
How can this be accomplished using triggers?

You can have a VIEW with either triggers or rules to redirect the INSERT.
INSERT ...RETURNING .. comes up empty when BEFORE trigger cancels statement
Or you can do it in a single SQL statement with data-modifying CTEs.
WITH input(col1, col2) AS (
VALUES
(text 'val1a', text 'val1b') -- explicit type cast in first row
, ('val2a', 'val2b')
)
, ins1 AS (
INSERT INTO testtable (col1)
SELECT col1 FROM input
)
INSERT INTO anothertable (col2)
SELECT col2 FROM input;
Typically, one would also store the connections between 'val1a' and 'val1b' of your input row somehow.
You might want to use a RETURNING clause to get a serial PK from the first table and store that in the second table.
Related:
PostgreSQL multi INSERT...RETURNING with multiple columns
Combining INSERT statements in a data-modifying CTE with a CASE expression
INSERT rows into multiple tables in a single query, selecting from an involved table

Related

How to insert some values in a SQL server table without select statement (e.g from my own created varibales)

I couldn't find any way to insert some new values for some columns in a table in SQL Server without a select statement.
For example I might have some values like #var1 and #var2 and I want to insert them in a new row in the table on the columns COL4 and COL5 and leave the other columns of the new row Null.
The following code is absolutely wrong but I wish I could write something like this:
Insert into myTable
-- add a new row under the lowest row of the table
Values
COL4=#var1
COL5=#var2
-- and leave the other columns empty
Use below query to insert data into myTable in selected columns through variables -
Insert into myTable
(COL4, COL5)
Values
(#var1, #var2)
you can use SELECT clause or VALUES clause
Insert into myTable(Col4,Col5)
SELECT #var1,#var2
Insert into myTable(Col4,Col5)
VALUES(#var1,#var2)

Save return values from INSERT...RETURNING into temp table (PostgreSQL)

I have a table table1 with columns id,value1 and value2.
Also I have a query
INSERT INTO table1(value1,value2) SELECT value3,value4 FROM table2 RETURNING id
that returns set of ids.
I want to store return values (these ids) in some temp table. Something like that:
INSERT INTO TEMP temp1 INSERT INTO table1(value1,value2) SELECT value3,value4 FROM table2 RETURNING id
How can I do it?
DBMS is PostgreSQL
with inserted as (
INSERT INTO table1 (value1,value2)
SELECT value3,value4
FROM table2
RETURNING id
)
insert into temp
select id
from inserted;
This requires Postgres 9.2 or later.
Two options.
If you need it just for one follow-up query, a with statement (see the horse's answer) is the easiest.
If you need it for more than one follow-up query, the other option is to not use insert ... returning, but rather create table as:
CREATE TEMPORARY TABLE foo AS
SELECT value3,value4 FROM table2
Caveats: if necessary, create the indexes you need on the table -- and analyze it if you do.

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

SQLite : insert multiple value (select from) with insert specified value

i'm trying to make sqlite query that can insert multiple values , here's my query that i try :
insert into table1(idy_table1,idx_table1)
values ('1', //specified value insert to idy_table1
(select id_table2 from table2)) //insert value from select id_table2
i'm having some trouble, it just only insert one value,
and my question is how to make a proper query? so i can make it work.
The VALUES clause always adds one row.
(Except when you're using multiple tuples, but this does not work with queries.)
The easiest way to add multiple rows from a query is to use the SELECT form of the INSERT statement:
INSERT INTO Table1(idy_table1, idx_table1)
SELECT '1', id_table2 FROM table2;

Insert multiple rows into single column

I'm new to SQL, (using SQL 2008 R2) and I am having trouble inserting multiple rows into a single column.
I have a table named Data and this is what I am trying
INSERT INTO Data ( Col1 ) VALUES
('Hello', 'World')
That code was taken from this question, but it, like many other examples I have found on the web uses 2 columns, I just want to use 1. What am I doing wrong?
Thanks
To insert into only one column, use only one piece of data:
INSERT INTO Data ( Col1 ) VALUES
('Hello World');
Alternatively, to insert multiple records, separate the inserts:
INSERT INTO Data ( Col1 ) VALUES
('Hello'),
('World');
to insert values for a particular column with other columns remain same:-
INSERT INTO `table_name`(col1,col2,col3)
VALUES (1,'val1',0),(1,'val2',0),(1,'val3',0)
I believe this should work for inserting multiple rows:
INSERT INTO Data ( Col1 ) VALUES
('Hello'), ('World'),...
Another way to do this is with union:
INSERT INTO Data ( Col1 )
select 'hello'
union
select 'world'
If your DBMS supports the notation, you need a separate set of parentheses for each row:
INSERT INTO Data(Col1) VALUES ('Hello'), ('World');
The cross-referenced question shows examples for inserting into two columns.
Alternatively, every SQL DBMS supports the notation using separate statements, one for each row to be inserted:
INSERT INTO Data (Col1) VALUES ('Hello');
INSERT INTO Data (Col1) VALUES ('World');
INSERT INTO Data ( Col1 ) VALUES ('Hello'), ('World')
In that code you are inserting two column value.
You can try this
INSERT INTO Data ( Col1 ) VALUES ('Hello'),
INSERT INTO Data ( Col1 ) VALUES ('World')
Kindly ensure, the other columns are not constrained to accept Not null values, hence while creating columns in table just ignore "Not Null" syntax. eg
Create Table Table_Name(
col1 DataType,
col2 DataType);
You can then insert multiple row values in any of the columns you want to.
For instance:
Insert Into TableName(columnname)
values
(x),
(y),
(z);
and so on…
Hope this helps.
INSERT INTO hr.employees (location_id) VALUE (1000) WHERE first_name LIKE '%D%';
let me know if there is any problem in this statement.