SQL Update after INNER JOIN two tables - sql

I would like Update two tables on the same Update statement with INNER JOIN but I cant attach second table
UPDATE T1 SET T1.status='test1', T2.status='test1'
FROM mytable1 T1
INNER JOIN table2 T2 ON T1.id=T2.id
WHERE parameters.....
But I cant use T2.status='test1'
error I am getting
The multi-part identifier "T2.status" could not be bound.

You can't update 2 tables in a single update statement, even if using join clause. The join clause can be used for "filtering" purposes only. Only FROM table can be updated.

Related

Oracle sql MERGE INTO with a single where clause

I have the following SQL code (this is how much I've got so far):
MERGE INTO SCHEMA1.TABLE_1 table1 USING
(
SELECT DISTINCT table2.column1,
view1.column2
FROM SCHEMA2.TABLE_2 table2
LEFT JOIN SCHEMA2.VIEW_1 view1
ON table2.column2 = view1.column3
) t2 ON (table1.column3 = t2.column1 )
WHEN MATCHED THEN
UPDATE
SET table1.column4 = t2.column2;
The following is the definition of VIEW_1 :
CREATE VIEW SCHEMA_2.VIEW_1
AS (SELECT
SCHEMA_2.TABLE_1.COLUMN_1,
SCHEMA_2.TABLE_2.COLUMN_1,
SCHEMA_2.TABLE_2.COLUMN_2,
SCHEMA_2.TABLE_2.COLUMN_3,
SCHEMA_2.TABLE_5.COLUMN_1,
SCHEMA_2.TABLE_6.COLUMN_1,
SCHEMA_2.TABLE_6.COLUMN_2,
SCHEMA_2.TABLE_6.COLUMN_3,
SCHEMA_2.TABLE_6.COLUMN_4,
SCHEMA_2.TABLE_7.COLUMN_1,
SCHEMA_2.TABLE_7.COLUMN_2,
SCHEMA_2.TABLE_8.COLUMN_1
FROM SCHEMA_2.TABLE_1
INNER JOIN SCHEMA_2.TABLE_2
ON SCHEMA_2.TABLE_1.COLUMN_1 = SCHEMA_2.TABLE_2.COLUMN_2
INNER JOIN SCHEMA_2.TABLE_5
ON SCHEMA_2.TABLE_1.COLUMN_4 = SCHEMA_2.TABLE_5.COLUMN_3
LEFT OUTER JOIN SCHEMA_2.TABLE_6
ON SCHEMA_2.TABLE_2.COLUMN_2 = SCHEMA_2.TABLE_6.COLUMN_4
LEFT OUTER JOIN SCHEMA_2.TABLE_7
ON SCHEMA_2.TABLE_2.COLUMN_1 = SCHEMA_2.TABLE_8.COLUMN_5
);
But I'm getting the below error message:
Error report -
SQL Error: ORA-30926: unable to get a stable set of rows in the source tables
30926. 00000 - "unable to get a stable set of rows in the source tables"
*Cause: A stable set of rows could not be got because of large dml
What causes the error? Where to change in the code to make it work?
Thanks for helping out!
For this example your problem is definitely in the USING subquery. This query produces more than one value of table2.column1:
SELECT DISTINCT table2.column1,
view1.column2
FROM SCHEMA2.TABLE_2 table2
LEFT JOIN SCHEMA2.VIEW_1 view1
ON table2.column2 = view1.column3
So the ON clause will match the same row(s) in table1 more than once:
ON (table1.column3 = t2.column1 )
Oracle cannot figure out which value of t2.column2 should be used in the UPDATE, so it hurls ORA-30926.
Using distinct in the subquery doesn't help because that gives permutations of all the columns. You need to write a subquery which will produce unique values of t2.column1 across all rows, or add another identifying column(s) to generate a unique key you can join to table1.
In my experience, this error is returned, not only when the USING clause returns more than one row for a row in the MATCH table, but also frequently when it cannot be sure that only one row will be returned (even if there are no actual cases of multiple rows being returned). To force the parser to accept the query in cases like this, I usually resort to using a GROUP BY on the MATCH..ON column(s).
MERGE INTO SCHEMA1.TABLE_1 table1 USING
(
SELECT table2.column1,
MAX(view1.column2) as column2
FROM SCHEMA2.TABLE_2 table2
LEFT JOIN SCHEMA2.VIEW_1 view1
ON table2.column2 = view1.column3
GROUP BY table2.column1
) t2 ON (table1.column3 = t2.column1 )
WHEN MATCHED THEN
UPDATE
SET table1.column4 = t2.column2;

inner join after insert into temp table

I can run two separate SQL statement where the first INSERT INTO a temp table and the second SQL statement runs an INNER JOIN between the temp table and another table.
I was trying to run the two statements in a single SQL statement but I am getting syntax error (using access).
first statement:
INSERT INTO temp SELECT id from t1 where a_column='Yes'
second statement:
SELECT * from t2 INNER JOIN t2.id = temp.id
Is there a way to run the two in a single statement?
These are two very different operations. You can't insert and output data all at once (at least not in Access).
If you are doing this through queries you'll need to make a second query. Each query in Access can only execute one statement.
You also need to specify the two tables you are joining together. Each statement is independent and has not bearing of reference for what came before.
SELECT *
FROM t2
INNER JOIN temp on t2.id = temp.id
Though depending on what you wish to accomplish (I'm not sure why you need a temp table) you might get away with this
SELECT *
FROM t2
INNER JOIN t1 ON t2.id = t1.id
WHEREt1.a_column='Yes'

SQL-Duplicate column names

I am using SQL developer and I am trying an outer join on two tables. The error it is showing is "Duplicate column names". I have used the table nameswhile comparison but still it is giving error. Code is as following:
CREATE VIEW OECD_VIEW AS
SELECT * FROM DM_OECD_GDP FULL OUTER JOIN DM_OECD_DOCTORS
ON DM_OECD_GDP.DATA_YEAR = DM_OECD_DOCTORS.DATA_YEAR;
I have heard that this error can be resolved by aliasing but I don't know how to alias while comparing. Can this be done?
Thanks
You have to explicitly name returned fields from both tables and alias fields having the same name, like:
CREATE VIEW OECD_VIEW AS
SELECT DM_OECD_GDP.DATA_YEAR AS GDP_DATA_YEAR,
DM_OECD_DOCTORS.DATA_YEAR AS DOC_DATA_YEAR,
... rest of the fields here
FROM DM_OECD_GDP
FULL OUTER JOIN DM_OECD_DOCTORS
ON DM_OECD_GDP.DATA_YEAR = DM_OECD_DOCTORS.DATA_YEAR;
This is the generalised query to create views with alias and full outer join :
create view v6 as select t1.c1, t2.c2 from t1 FULL OUTER JOIN t2 on t1.id = t2.id;
If both tables have same column names, may be the foreign keys, dont use * , Manually select the column names
table_one - ID,Name
table_two - ID,SecondName
select table_one.ID,table_one.Name,table_two.ID as ID2,
table_two.SecondName full outer join on table_one.ID=table_two.ID

Update multiple tables in SQL Server using INNER JOIN [duplicate]

This question already has answers here:
How to update two tables in one statement in SQL Server 2005?
(10 answers)
Closed 10 years ago.
I'm using SQL Server and trying to use SQL to update multiple tables at once with one query:
The following query:
update table1
set A.ORG_NAME = #ORG_NAME, B.REF_NAME = #REF_NAME
from table1 A, table2 B
where B.ORG_ID = A.ORG_ID
and A.ORG_ID = #ORG_ID
Gives the error message:
The multi-part identifier "A.ORG_NAME" could not be bound.
What does the error message mean?
You can't update more that one table in a single statement, however the error message you get is because of the aliases, you could try this :
BEGIN TRANSACTION
update A
set A.ORG_NAME = #ORG_NAME
from table1 A inner join table2 B
on B.ORG_ID = A.ORG_ID
and A.ORG_ID = #ORG_ID
update B
set B.REF_NAME = #REF_NAME
from table2 B inner join table1 A
on B.ORG_ID = A.ORG_ID
and A.ORG_ID = #ORG_ID
COMMIT
You can update with a join if you only affect one table like this:
UPDATE table1
SET table1.name = table2.name
FROM table1, table2
WHERE table1.id = table2.id
AND table2.foobar ='stuff'
But you are trying to affect multiple tables with an update statement that joins on multiple tables. That is not possible.
However, updating two tables in one statement is actually possible but will need to create a View using a UNION that contains both the tables you want to update. You can then update the View which will then update the underlying tables.
But this is a hacky parlor trick, use the transaction and multiple updates, it's much more intuitive.

sql access inner join of 4 tables

I am new to sql and I am trying to join 4 tables together but just cant get the hang of it.
I am trying to do this with an inner join but I always get an syntax error with access.
SELECT *
from kdst,aufpos
inner join( artst inner join vert on kdst.vertreter = vert.vertnr)
on aufpos.artnr = artst.artnr;
This is my code but it does not work. I dont know what to do anymore, I hope someone can help me.
Build using the query design window
Then switch to sql view
Select *
From table1 t1
Inner join table2 t2 on t1.id = t2.fkid
Inner join table3 t3 on t1.id = t3.fkid
...
This is if you want to join multiple tables to the same parent table (table1). Fkid is the column of the foreign key field that refers to the primary key of the parent table.