Update and left outer join statements - sql-server

I have two tabels with a relation and I want to update a field in table A. Is it possible to combine update and join in the same query? I googled it but didnt find any working solution?
UPDATE md SET md.status = '3'
FROM pd_mounting_details AS md
LEFT OUTER JOIN pd_order_ecolid AS oe ON md.order_data = oe.id
I'm using MS SQL

UPDATE
t
SET
t.Column1=100
FROM
myTableA t
LEFT JOIN
myTableB t2
ON
t2.ID=t.ID
Replace myTableA with your table name and replace Column1 with your column name.
After this simply LEFT JOIN to tableB. t in this case is just an alias for myTableA. t2 is an alias for your joined table, in my example that is myTableB. If you don't like using t or t2 use any alias name you prefer - it doesn't matter - I just happen to like using those.

If what you need is UPDATE from SELECT statement you can do something like this:
UPDATE suppliers
SET city = (SELECT customers.city FROM customers
WHERE customers.customer_name = suppliers.supplier_name)

Just another example where the value of a column from table 1 is inserted into a column in table 2:
UPDATE Address
SET Phone1 = sp.Phone
FROM Address ad LEFT JOIN Speaker sp
ON sp.AddressID = ad.ID
WHERE sp.Phone <> ''

In mysql the SET clause needs to come after the JOIN. Example:
UPDATE e
LEFT JOIN a ON a.id = e.aid
SET e.id = 2
WHERE
e.type = 'user' AND
a.country = 'US';

The Left join in this query is pointless:
UPDATE md SET md.status = '3'
FROM pd_mounting_details AS md
LEFT OUTER JOIN pd_order_ecolid AS oe ON md.order_data = oe.id
It would update all rows of pd_mounting_details, whether or not a matching row exists in pd_order_ecolid. If you wanted to only update matching rows, it should be an inner join.
If you want to apply some condition based on the join occurring or not, you need to add a WHERE clause and/or a CASE expression in your SET clause.

Related

SQL Server : update old ID to new ID (SELF JOIN in UPDATE)

I have a SQL Server table, and column category IDs (kategori) needs to be updated.
I need to find id_old first and update as new id instead:
How can I do this in batch with a T-SQL statement?
use SELF JOIN. You need to use a different alias on the table.
UPDATE t2
SET kategori = t1.id
FROM yourtable t1
INNER JOIN yourtable t2 ON t1.id_old = t2.kategori
WHERE t2.kategory <> 0 -- not sure but you might need this condition

Filter based on two databases

I have the same query on two databases, but in the first database it finds me data, while in the second one it does not. Is it possible to build on these two bases so that I can filter all those that exist in the first base and not the second. These are my sql query:
use firstDB;
SELECT A_ANSPRECHPARTNER.AAS_ID FROM A_ANSPRECHPARTNER LEFT OUTER JOIN A_ADRESSEN ON A_ANSPRECHPARTNER.AAS_ADR_ID = A_ADRESSEN.ADR_ID WHERE ADR_Nr = 106740
use secondDB;
SELECT A_ANSPRECHPARTNER.AAS_ID FROM A_ANSPRECHPARTNER LEFT OUTER JOIN A_ADRESSEN ON A_ANSPRECHPARTNER.AAS_ADR_ID = A_ADRESSEN.ADR_ID WHERE ADR_Nr = 106740
the current result
If these databases are on the same server you can put each of your queries in a subquery specifying the database name when referencing the table. Then left join the subquery on firstDB to the subquery on secondDB filtering the records where there is a record in firstDB, but not in secondDB.
SELECT DB_1.*
FROM (
SELECT A_ANSPRECHPARTNER.AAS_ID
FROM firstDB.dbo.A_ANSPRECHPARTNER
LEFT OUTER JOIN firstDB.dbo.A_ADRESSEN ON A_ANSPRECHPARTNER.AAS_ADR_ID = A_ADRESSEN.ADR_ID
) DB_1
LEFT JOIN (
SELECT A_ANSPRECHPARTNER.AAS_ID
FROM secondDB.dbo.A_ANSPRECHPARTNER
LEFT OUTER JOIN secondDB.dbo.A_ADRESSEN ON A_ANSPRECHPARTNER.AAS_ADR_ID = A_ADRESSEN.ADR_ID
) DB_2 ON DB_1.ADR_Nr = DB_2.ADR_Nr
WHERE DB_2.ADR_Nr IS NULL
AND DB_1.ADR_Nr = 106740
You can just remove the AND DB_1.ADR_Nr = 106740 in order to find all records in firstDB that are not in secondDB. If these databases are on different servers you would have to set up a linked server and add that to the beginning of your table reference.

Update a column of a table with a column of another table in SQL Server

I am trying to update column from one table to another table of different database.
I searched on stack but found answers for PostgreSQL and SQLite. Those code ain't worked for me properly in SQL Server.
Say,
D1,D2 are the two different databases
T1 is the table
C1,C2,C3 are the columns in T1
I want to update like
UPDATE T1
SET D2.T1.C1 = D1.T1.C1
WHERE D2.T1.C2 = D1.T1.C2
Everything except the where clause works fine.
Here is some code I tried:
use DB2
GO
UPDATE TABLE1
SET COL1 = (SELECT COL1 FROM DB1.dbo.TABLE1)
WHERE COL2 = DB1.dbo.TABLE1.COL2
How shall I write this query?
Note: D1 and D2 are identical. Both have the exact same schema. Just the name and data are different. Both databases are on the same server.
SQL Server supports an update join syntax:
UPDATE t1
SET COL1 = t2.COL1
FROM TABLE1 D1.t1
INNER JOIN TABLE2 D2.t2
ON t1.COL2 = t2.COL2;
Actually, your current approach might work, but you should try changing it to this:
UPDATE TABLE1 D1.t1
SET COL1 = (SELECT t2.COL1 FROM TABLE2 D2.t2 WHERE T1.COL2 = T2.COL2);
You can simply use an INNER JOIN cause sql-server support Cross-Database Queries, so you can do as:
UPDATE T1
SET T1.Col1 = T2.Col1
FROM DataBase1.Schema.TableName T1 INNER JOIN DataBase2.Schema.TableName T2
ON T1.ID = T2.ID;
Please, visit and read Cross-Database Queries.
Or update tableName set column Name = (select columnName from second table where 1=1) i.e condition is either true or false optional though

Where statement with conditions is multiple tables

I need my code to set values of a column based on two criteria: the value of a different column in the same table and a value of a column in a different table. Is there a way to use the where statement to look at criteria in different tables?
Below is pseudo code that doesn't work but captures what I am trying to do.
UPDATE Table1
SET CustomerStatus_2003 = 'New'
FROM Table1, Table2
WHERE Table1.Column1 = 'New'
AND Table2.Column1 = '2003'
AND Table1.Column2= Table2.Column2
Thanks for your help!
You should always alias your tables and use a JOIN when you are using more than one table in a statement. Here is some code that does what you are looking for
UPDATE t
SET myColumn = 'New'
FROM MyTable t
JOIN OtherTable t2 ON t.Column = t2.Column
WHERE t.SomeCondition
AND t2.SomeOtherCondition

Trying to update 3 tables with 1 query [in vain]

When i want to populate my grid I use this Select command :
SELECT t1.Id, t1.emertimi, kredite, pershkrim, Viti, t2.Emertimi as Expr1, emri, mbiemri
FROM Kurse as t1 INNER JOIN
Deget as t2 ON t1.degID = t2.Id INNER JOIN
Pedgoge ON t1.Id = Pedgoge.kurs_id
Now when I try to Update the grid with an Update command that is fired after an EditCommand finished ( you pres Update as a link) I use it this way :
UPDATE Kurse
SET t1.emertimi=#emertimi,
kredite=#kredite,
pershkrim=#pershkrim,
Viti=#Viti,
t2.Emertimi as Expr1 = #Expr1, emri=#emri, mbiemri=#mbiemri
FROM Kurse as t1 INNER JOIN
Deget as t2 ON t1.degID = t2.Id INNER JOIN
Pedgoge ON t1.Id = Pedgoge.kurs_id
But this doesn't work. Please as I am a student and non english native is a little hard to understand what should I do in this case. Any help is much appreciated
UPDATE statement changes only one table or view
It fails because there are two different aliases (t1 and t2 ) in the SET clause
As Vadim already said, UPDATE command can only change one table. So you'll have to write 3 different Update statements.
UPDATE Kurse
SET emertimi=#emertimi,
kredite=#kredite,
pershkrim=#pershkrim,
Viti=#Viti
WHERE.......
UPDATE Deget
SET Emertimi = #Expr1,
emri=#emri,
mbiemri=#mbiemri
WHERE ......
From your query it is not clear what exactly you would like to update and where, so you'll have to sort out where conditions yourself.

Resources