SQL insert query combined with select - sql

I want to insert a dataset in a table. The insert query should insert the data into two tables.
Here are the tables:
Table A:
ID, customerId, ..[some other columns]...
Table B:
customerId, name
The query should insert into both Table A and Table B. How can I insert values into Table B with the relation customerId?
EDIT: the DBMS is sqlite.
The defintions:
Adaption:
id -> integer not null
customerId -> integer not null
some text columns....
Customer:
customerId -> integer not null
name -> text not null

Im not sure, I fully understand your problem, but i suggest you to see into the technology called :
Transaction (https://en.wikipedia.org/wiki/Database_transaction)
Technically that will be still two (or more) insert queries, but it will let you keep the integrity of your dataset. Transactions are implemented in the most DBMS.

Related

How to insert list of data to single column row in Azure SQL/SQL Server?

I have tables like this:
Select CustomerName, CustomerSales, SalesDate
From Customer
Select Month, WeeklyMessage
From WeeklyReport
I would like to insert list of CustomerName and Sales to single row of WeeklyMessage like:
Tesla 1200 000\n
Toyota 500 000\n
I would next use this row for monthly notification.
Is this possible? What is Insert query that is able to store list to single row?
You could store it as a json.
This query results a json formatted string:
select CustomerName, CustomerSales from Customer for json auto
That could be used to store the values into your WeeklyReport table.
You can query json and get its values as explained in this MS doc JSON data in SQL Server.
i dont know why you want to insert into the same colum the 2 values (name and number), i think you can do it simple if you do a relation with CustomerName and Sales.
create table WeeklyMessage (
name varchar(30),
number integer,
FOREIGN KEY (name) REFERENCES CustomerName(name),
FOREIGN KEY (number) REFERENCES Sales(number),
PRIMARY KEY (name,number)
);
and then insert it:
insert into WeeklyMessage values (Tesla, 1200 000);

Trying to insert into Table A and link Table B and C but add to all if not exist

I have 4 tables:
Table A:
LogID (unique identifier),
UserID (bigint),
LogDate (date/time),
LogEventID (int),
IPID (varchar(36)),
UserAgentID (varchar(36))
Table B:
IPID (unique identifier),
IPAddress (varchar(255))
Table C:
UserAgentID (unique identifier),
UserAgent (varchar(255))
Table D:
LogEventID (int),
LogEvent (varchar(255))
I am trying to write the to Table A but need to check Table B, Table C and Table D contain data so I can link them. If they don’t contain any data, I would need to create some. Some of the tables may contain data, sometimes none of them may.
Pretty much everything, really struggling
first, you do a insert into table B, C, D WHERE NOT EXISTS
example
INSERT INTO TableB (IPID, IPAddress)
SELECT #IPPD, #IPAddress
WHERE NOT EXISTS
(
SELECT *
FROM TableB x
WHERE x.IPID = #IPID
)
then you insert into table A
INSERT INTO TableA ( . . . )
SELECT . . .
SQL Server doesn't let you modify multiple tables in a single statement, so you cannot do this with a single statement.
What can you do?
You can wrap the multiple statements in a single transaction, if your goal is to modify the database only once.
You can write the multiple statements in stored procedure.
What you really probably want is a view with insert triggers on the view. You can define a view that is the join of the tables with the values from the reference tables. An insert trigger can then check if the values exist and replace them with the appropriate ids. Or, insert into the appropriate table.
The third option does exactly what you want. I find that it is a bit of trouble to maintain triggers, so for an application, I would prefer wrapping the logic in a stored procedure.

Insert into table sum of different columns with same schema

I´ve the following tables
I want to insert the sum of the columns [01],][02],[03]...etc. Into another table with the same schema, even if in a specific table exist only 1 record.
INSERT INTO TABLE1 VALUES AS SELECT * FROM
TABLE;
ALTER TABLE TABLE1
ADD COLUMN SUM_VALUES NUMBER(20)
DEFAULT( SUM(01,02,...N))
Is this what you are looking for or provide extra detail

using the ids returned from insert into, for record insertion with foreign key

I have a table
monster(id serial, name varchar, primary key(id))
and i have another table
ranged_monster(id_monster integer, distance integer, foreign key(id_monster) references monster)
I want to insert two ranged monsters: one is called 'archer goblin' with a range attack distance of 10, and the another is called 'dragon' with a range attack distance of 50. How can i do this in one instruction?
so far, i have tried this:
the worst way
insert into monster(name) values ('archer goblin'), ('dragon');
insert into ranged_monster(distance) values (10) where name='archer goblin';
insert into ranged_monster(distance) values (50) where name='dragon';
it is bad because the name column allows repetitions, and the retrieved records might be more than one... also having to write the name of the monster twice does not seems like a good habit.
insert into ... returning
if the table ranged_monster only had the column (foreign key) of the id_monster, then i could use this solution:
with the_ids as (
insert into monster(name)
values ('archer goblin'), ('dragon')
returning id
)
insert into ranged_monster(id_monster)
select * from the_ids;
however it does not work, because ranged_monster also has the column distance. Doing so, will insert the ids of the monsters without distance.
possible solutions
create a temporal table with the distances, and then combine this temporal table sequentially with the insert into ... returning's the_ids, and then insert this combined records into the ranged_monster table.
How can i combine two tables as asked in here https://stackoverflow.com/questions/31171253/sql-combine-two-tables ? (it was marked as duplicated, linking to this What is the difference between JOIN and UNION? , but that question is not related to that another question.)
with s(name, distance) as (
values ('archer goblin', 10), ('dragon', 50)
), the_ids as (
insert into monster(name)
select name
from s
returning id, name
)
insert into ranged_monster (id_monster, distance)
select id, distance
from
s
inner join
the_ids using (name)

How to fire two triggers for two tables at different time to insert some values from both the tables to third table?

I have 3 tables in SQL server with following fields.
Table 1- id, name, age.
Table 2- id,email, Address.
Table 3- id, name, email.
I wish to use two triggers like, when I insert values on Table 1, id and name should insert in Table 3. When I insert values in Table 2, Email should insert in Table 3 and it should insert at id and name position means it should not show NULL values. Name,id and email should insert in one row.
You should make the View for inserting data from two tables to a single table
You should fix the data model so that it is normalized. You wouldn't need a trigger to replicate data into Table 3 if the name and email columns weren't in 2 tables.