Is there any RDBMS which supports inserting into joined tables? (multi-table inserts) - sql

Example:
INSERT INTO table1 inner join table2 on table2.parent=table1.id values(...)

at least one. http://www.oracle-developer.net/display.php?id=209
..excerpt...
1 INSERT ALL
2 INTO t1
3 INTO t2
4 INTO t3
5 INTO t4
6 SELECT owner
7 , object_type
8 , object_name
9 , object_id
10 , created
11 FROM all_objects;
Use of views and triggers can do it as well. http://www.dbforums.com/microsoft-sql-server/663921-update-multiple-tables-via-view.html
....Excerpt from above...
CREATE TRIGGER trgInsteadOfUpdate ON dbo.Someview
INSTEAD OF UPDATE
AS
UPDATE Person
SET Person = inserted.Person_Name
FROM inserted
UPDATE Company
SET Company = inserted.Company_Name
FROM inserted
GO
There may be other ways; but that's the two I was aware of.

Make sure what you know is true. PostgreSQL supports writable multi-table views via its rule system.

Related

How to create the links between tables using dynamic sql?

I need to update a field from a table. In order to get to that specific table, I need to go through multiple other tables.
To store the relationship between the tables, I have created a metadata table and defined there a hierarchical structure as following:
Table
level
Tree
join condition
tab1
1
2
tab2
2
3
tab2.fk = tab1.pk
tab3
3
4
tab3.fk = tab2.pk
tab4
2
5
tab4.fk - tab1.pk
tab5
5
6
tab5.fk = tab4.pk
tab6
6
7
tab6.fk = tab5.pk
Using a CTE, I construct a temporary table which stores all the final tables in which I need to update the fields plus its path and it looks like this:
TableToUpdate
Path
Tab1
tab1
Tab3
tab1\tab2\tab3
Tab6
tab1\tab4\tab5\tab6
Now, I need to create the update statement, in such way, that the statement knows the joins and the path for each final table to be updated.
Do you have any suggestion how should I implement it in wherescape - snowflake? To create dynamic queries in wherescape, should I use java language or is it possible to do it using sql?
Many thanks!

Update 2 joined tables simultaneously on HANA?

Can I update 2 columns from 2 tables joined by foreign key with one statement in SAP HANA
No, SAP HANA up to its current version HANA 2 SPS 05 does not support multi-table updates (or inserts/deleted for that matter).
Depending on the use-case, you may be able to emulate the behavior via SQLScript, like e.g. so:
DO BEGIN
data_to_insert = SELECT id, stuff FROM DB;
-- update table 1
UPDATE tab1 t FROM t, :data_to_insert d
SET t.one_stuff = d.stuff
WHERE t.id = d.id;
-- update table 2
UPDATE tab2 t FROM t, :data_to_insert d
SET t.two_stuff = d.stuff
WHERE t.id = d.id;
END;

Oracle SQL | Single-row Subquery Update returns more than one row?

this question has been asked a lot it seems, but I can't find any dealing with updates and the ones dealing with other stuff haven't helped. I feel like I'm missing something obvious but I can't put my finger on it.
I have the following query to update my table with IDs from another table to enable matching to a spreadsheet:
update TABLE3 set ITM_CD2 =
(select pi.ITM_CD2
from schema1.PI_TABLE pi,
TABLE3 tb3
where pi.OTHER_ITM_CD = tb3.OTHER_ITM_CD)
I can't actually go through with the update because I keep getting "needs a single-row subquery" issues.
EDIT: I should have mentioned that the pi table is from a separate schema.
EDIT 2: For more detail; this is an example of what I'm trying to obtain:
TABLE 3 currently has this data, for instance:
NAME ----- PRODUCT ----- ITM_CD1 ----- ITM_CD2
X Y 11 NULL
A B 12 NULL
C Y 11 NULL
I'm trying to attach data from this item table so I can get the 2nd itm_cd which will allow me to compare it to a table that has ITM_CD2 but NOT ITM_CD1. The NULLs in TABLE 3 would be replaced with the matching ITM_CD2.
The table I'm trying to take the ITM_CD2 from would look like this:
PRODUCT ----- ITM_CD1 ----- ITM_CD2
A 10 90
Y 11 98
B 12 87
Perhaps you want the main TABLE3 in the sub-query:
update TABLE3 set ITM_CD2 =
(select pi.ITM_CD2
from PI_TABLE pi
where pi.OTHER_ITM_CD = TABLE3.OTHER_ITM_CD)
I like using a merge rather than update for this kind of scenario. It makes the connection between the table you are reading from and the table you are writing to more clear:
MERGE INTO table3 t3
USING pi_table pi
ON (pi.other_itm_cd = t3.other_itm_cd)
WHEN MATCHED THEN
UPDATE SET t3.itm_cd2 = pi.itm_cd2
Try this query
UPDATE ( SELECT t1.Item_CD2,pi.ITM_CD2
FROM table3 t1
join pi_table pi on pi.OTHER_ITM_CD = t1.OTHER_ITM_CD
)
SET t1.Item_CD2=pi.ITM_CD2

Multiple columns updation in single query

Can I update two columns in different tables with single update query.
I stuck in a situation where I have :
Table 1:
case id client id created by
1 a john
2 b Mike
3 c Raghu
4 b Mike
Table 2:
case id client id case cost
2 b 5,000
3 j 10,000
4 b 6,000
The problem is , I want to update client id=d in both the tables of those cases created by Mike.
I can fetch out those cases through join which is created by mike but is it possible to update them also by using sql joins??
The answer to more updates in one statement is no. While you can enter multiple tables in the from clause of an update statement, you can only specify a single table after the update keyword. Even if you do write a "updatable" view (which is simply a view that follows certain restrictions), updates like this will fail.
To fix this
Use transaction block:
Example :
BEGIN TRANSACTION
update A
set A.client_id = 'd'
from table1 A inner join table2 B
on B.client_id = A.client_id
where a.created_by='mike'
update B
set B.client_id = 'd'
from table2 B inner join table1 A
on B.client_id = A.client_id
where a.created_by='mike'
COMMIT
Probably something like this will work
UPDATE table1,table2 SET table1.client_id='d',table2.client_id='d'
WHERE table1.client_id=table2.client_id;
Since you mentioned RDBMS as MSSQL 2012; you can have only single update statement.
You can either have multiple in transaction block as mentioned in another answer (OR)
Create a procedure and wrap both your update statement there like
create procedure update_table1_table2
as
begin
UPDATE table1 SET client_id='d' WHERE client_id='b';
UPDATE table2 SET client_id='d' WHERE client_id='b';
end
Then call your procedure like
exec update_table1_table2

2 Left join for 2 tables?

I am on MySQL:
I have 2 table, one is the main table, the other is an accessory table that contains some information supporting the records of the main table.
Example:
table portal:
id title desc
12 "aaa" "desc"
13 "bbb" "desc"
[etc]
secondary table (omitting the primary id field)
type portalid
x 12
2 12
3 12
4 12
5 12
1 13
2 13
4 13
I need to select every record in the table portal that got a record in the secondary table with type = 4 but != 5.
Example:
SELECT *
FROM portal,secondary_table s
WHERE portal.id=s.portalid
AND type of secondary_table is 4 and is not 5
Results:
In this case only the record 13 of portal should be returned because the record 12 got both type 4 and 5.
Please note I asked a similar question but considering only one table, and with that query took over 50 secs to be elaborated.
Thanks for any help
You should consider rephrasing it using NOT EXISTS clauses. If all you want are records from portal, then a double EXISTS clause will work and very clearly reveal the query intentions
SELECT *
FROM portal
WHERE EXISTS (select * from secondary_table s1
where portal.id=s1.portalid
and s1.type=4)
AND NOT EXISTS (select * from secondary_table s2
where portal.id=s2.portalid
and s2.type=5)
However, due to how MySQL process EXISTS clauses (even though it is clearer), you can trade off clarify for performance using LEFT JOIN / IS NULL. Please read the following link, however the performance of each query may vary with specific data distribution, so try both and use whichever works better for your data.
NOT IN vs. NOT EXISTS vs. LEFT JOIN / IS NULL: MySQL
The LEFT JOIN / IS NULL form would be written
SELECT *
FROM portal
JOIN secondary_table s1 ON portal.id=s1.portalid and s1.type=4
LEFT JOIN secondary_table s2 ON portal.id=s2.portalid and s2.type=5
WHERE s2.portalid IS NULL
The order of the tables (portal, inner, left) is to allow processing the first two tables (portal + secondary/type=4) and trimming the result set early before launching into the LEFT (outer) JOIN (that retains everything from the left side) for the existential test.
This is why you should avoid the older FROM A,B syntax - it's less powerful with respect to certain things. Use explicit join types (LEFT/RIGHT/INNER/FULL/CROSS) instead.
SELECT <columns>
FROM portal p
LEFT JOIN secondary s1 ON p.id=s1.portalid AND s1.type = 5
INNER JOIN secondary s2 ON p.id=s2.portalid AND s2.type = 4
WHERE s1.type IS NULL
I will use this query that's very similar to EXISTS of richard:
SELECT * FROM portal
WHERE id IN (SELECT portalid FROM sec WHERE type=4)
AND id NOT IN (SELECT portalid FROM sec WHERE type=5)
imo it's even more readable.