How to update a Sql Server table column while join two tables? - sql-server

I'm trying to update a column while joining it to another table. I've used below query, but it gave me error.
UPDATE TABLE_2
INNER JOIN
TABLE_1
ON (T2.ID=T1.ID)
SET TABLE_2.DEPT='HUMAN RESOURCE'
WHERE TABLE_2.DEPT='HR'
AND T1.COMPANY =1
Can anyone help me on this?
Thanks.

For one thing, you're using table aliases that aren't defined anywhere (T2, T1 etc) and that may very well solve your problem. If not, the correct syntax very much depends on SQL flavor.
For example, in SQL Server the syntax is
UPDATE T2
SET T2.dept = 'HUMAN RESOURCE'
FROM Table2 T2
INNER JOIN Table1 T1
ON T1.[ID] = T2.[ID]
Although you don't even need a join here really, you just want
UPDATE Table2 T2
SET T2.dept = 'HUMAN RESOURCE'
WHERE EXISTS(SELECT * FROM Table1 T1
ON T1.[ID] = T2.[ID])
In MySQL the syntax is
UPDATE FROM TABLE2 AS T2
INNER JOIN TABLE1 as T1
ON T2.id = T1.id
SET T2.Dept = 'Human Resources'
Of Course, the WHERE EXISTS approach also works for MySQL
UPDATE FROM Table2 AS T2
SET Dept="Human Resources"
WHERE EXISTS (SELECT * FROM Table1 T1
ON T1.[ID] = T2.[ID]);

If it is MSSQL, then the query should be
UPDATE TABLE_2 SET DEPT='Human Resource'
FROM TABLE_1
WHERE TABLE_2.ID = TABLE_1.ID
AND TABLE_2.DEPT = 'HR'
AND TABLE_1.COMPANY = 1

UPDATE TABLE_2 SET DEPT='Human Resource'
FROM TABLE_1,Table2
WHERE TABLE_2.ID = TABLE_1.ID
AND TABLE_2.DEPT = 'HR'
AND TABLE_1.COMPANY = 1
Because when we update a table in joining then we use both tables in from close

Related

SQL get counts using subqueries from multiple linked tables

Suppose I have tables 1-4, all the other tables are linked to table1. For what its worth, table1, table2 and table3 are relatively small but table4 contains a lot of data.
Now I have the following query:
SELECT t1.id
, (SELECT COUNT(*) FROM table2 WHERE table1_id = t1.id) AS t2_count
, (SELECT COUNT(*) FROM table3 WHERE table1_id = t1.id) AS t3_count
, (SELECT COUNT(*) FROM table4 WHERE table1_id = t1.id) AS t4_count
FROM table1 t1
Due to the fact that the subqueries are dependent/correlated I assumed that there must be a better way (performance wise) to get the data.
I tried to do the following but it drastically increased the execution time (from about 2s to 35s). I'm guessing that the multiple left joins creates a very big data set?!
SELECT t1.id
, COUNT(t2.id) AS t2_count
, COUNT(t3.id) AS t3_count
, COUNT(t4.id) AS t4_count
FROM table1 t1
LEFT JOIN table2 t2 ON t2.table1_id = t1.id
LEFT JOIN table3 t3 ON t3.table1_id = t1.id
LEFT JOIN table4 t4 ON t4.table1_id = t1.id
GROUP BY t1.id
Is there better way to get the counts? I don't need the data from the other tables.
UPDATE:
Bart's answer got me thinking that the table1_id columns are nullable. I added a IS NOT NULL check to the WHERE clauses and this brought the time down to 1s.
SELECT t1.id
, (SELECT COUNT(*) FROM table2 WHERE table1_id IS NOT NULL AND table1_id = t1.id) AS t2_count
, (SELECT COUNT(*) FROM table3 WHERE table1_id IS NOT NULL AND table1_id = t1.id) AS t3_count
, (SELECT COUNT(*) FROM table4 WHERE table1_id IS NOT NULL AND table1_id = t1.id) AS t4_count
FROM table1 t1
I guess not. If you execute a SELECT COUNT(*) FROM [table], it should perform a count on the table's PK. That should be pretty fast, even for very large tables.
Is your table4 a real table (and not a view, or a table-valued function, or something else that looks like a table)? And does it have a primary key? If so, I don't think that the performance of a SELECT COUNT(*) FROM [table4] query can be increased significantly.
It may also be the case, that your table4 is heavily targeted (in concurrent transactions over multiple connections), or perhaps your SQL Server is doing some heavy IO or computations. I cannot assume anything about that, however. You may try to check if your query is also slow on a restored database backup on a physically separate test server.

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

sql server join for nonmatch

what ever BU is present in table-2 that should be present in table-1 BU so write a query to find out the records available in table-2 whose BU is not available in table-1 BU.
Try to use left join where Table2 is left table so that you will get all the records from Table2 and matching records from Table1. And for those records from Table2 there is no match, result will shoe NULL entries for Table1. That's it-- that's what you are looking for --
SELECT T2.BU FROM Table2 T2
LEFT OUTER JOIN Table1 T1
ON T2.BU = T1.BU
WHERE T1.BU IS NULL
SELECT DISTINCT T2.BU FROM Table2 T2
LEFT OUTER JOIN Table1 T1
ON T2.BU = T1.BU
WHERE T1.BU IS NULL

The column "xxxx" was specified multiple times for "CTE_YYYY"

I am new to Microsoft SQL Server. I am trying to join two tables that has common key named CampaignID using LEFT OUTER JOIN. I need to reuse the result in a different query, so I decided to capture the result set using CTE_Results. For example,
-- This is my CTE script
WITH CTE_Results AS
(
SELECT t1.CampaignID, t2.CampaignID, t1.Name, t2.Vendor
FROM CampaignDetails AS t1
LEFT OUTER JOIN CampaignOnlineDetails AS t2
ON t1.CampaignID = t2.CampaignID
)
-- This is the script I want to use to compare the resulting table. For example,
SELECT Vendor
FROM CTE_Results
However, when I ran above, I get:
The column `CampaignID` was specified multiple times for `CTE_Results`.
From reading through old StackOverflow questions and answers, it seems like since CampaignID is in both tables that are being joined, I must use table aliases to specify whose (which table's) CampaignID I want to SELECT. But I think I did that and even that it seems like the error still occurs.
Is there a way for me to select and keep BOTH CampaignID's in my CTE? If so, what should be changed? Thank you for the answers!
You have CampaignID selected twice in CTE, use different alias name to fix the problem
WITH CTE_Results
AS (SELECT t1.CampaignID AS cd_CampaignID,
t2.CampaignID AS cod_CampaignID,
t1.NAME,
t2.Vendor
FROM CampaignDetails AS t1
LEFT OUTER JOIN CampaignOnlineDetails AS t2
ON t1.CampaignID = t2.CampaignID)
-- This is the script I want to use to compare the resulting table. For example,
SELECT Vendor
FROM CTE_Results
or use this
WITH CTE_Results(cd_CampaignID, cod_CampaignID, NAME, Vendor)
AS (SELECT t1.CampaignID,
t2.CampaignID,
t1.NAME,
t2.Vendor
FROM CampaignDetails AS t1
LEFT OUTER JOIN CampaignOnlineDetails AS t2
ON t1.CampaignID = t2.CampaignID)
-- This is the script I want to use to compare the resulting table. For example,
SELECT Vendor
FROM CTE_Results
You need to Alias the CampaignID Columns in your CTE or define the returned column names in the CTE declaration. Otherwise it would be like creating a table with two columns with the same name.
Example Column Alias:
WITH CTE_Results AS
(
SELECT t1.CampaignID as 'CampaignID1', t2.CampaignID as 'CampaignID2', t1.Name, t2.Vendor
FROM CampaignDetails AS t1
LEFT OUTER JOIN CampaignOnlineDetails AS t2
ON t1.CampaignID = t2.CampaignID
)
Or In CTE declaration:
WITH CTE_Results (CampaignID1, CampaignID2, [Name], Vendor) AS
(
SELECT t1.CampaignID, t2.CampaignID , t1.Name, t2.Vendor
FROM CampaignDetails AS t1
LEFT OUTER JOIN CampaignOnlineDetails AS t2
ON t1.CampaignID = t2.CampaignID
)

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