sql server - Update Data from Another Table - sql

My problem is:
I have 2 tables Assignment and Services
I am trying to update one column from another table's column's data like that.
UPDATE [Services]
SET
TeamID = (SELECT [AssignedTeam]
FROM [Assignment]
WHERE [ServiceName] in ([Services].[ServiceName]))
WHERE
EXISTS (
SELECT [ServiceName]
FROM [Assignment])
it does not work. can someone help me about that situation please? Thx

Update using JOIN.
Query
UPDATE t1
SET t1.[TeamID] = t2.[AssignedTeam]
FROM [Services] t1
JOIN [Assignment] t2
ON t1.[ServiceName] = t2.[ServiceName];

UPDATE [Services] SET TeamID = [AssignedTeam] FROM [Assignment] WHERE [Assignment].[ServiceName] = [Services].[ServiceName]

Related

Update employee ID from temp table only if employee exists in both tables

I have a table in a database called T_USR which includes the following fields:-
USRUID (Primary Key)
USRID <- This is the current employee ID
I also have a temp table #TEMP3 populated from a CSV file which includes:
USRUID (the value of this field will match the Primary Key in T_USR)
EMPID <- This is to be the new employee ID
#TEMP3 contains only a subset of the users found in T_USR
What i need is a query that will update the value of T_USR.USRID with the value from #TEMP3.EMPID ONLY if the member of staff (USRUID) exists in both tables.
This is the query I wrote, but for users who appear in T_USR and NOT in #TEMP3, the USRID field was set to NULL which is not the required result. For users who do not appear in both tables, I want their details to remain unchanged.
Update dbo.T_USR
SET dbo.T_USR.USRID =
(SELECT dbo.#TEMP3.EMPID
FROM #TEMP3
Where dbo.T_USR.USRUID = dbo.#TEMP3.USRUID
and exists (
select * from #TEMP3 where dbo.T_USR.USRUID = #TEMP3.USRUID)
)
I'd appreciate any suggestions. Thank you.
Use join. If I understand correctly:
update u
set u.usrid = t.empid
from dbo.T_USR u join
#temp3 t
on u.usruid = t.usruid
use this :
UPDATE t
SET USRID = EMPID
FROM #TEMP3 te
JOIN dbo.T_USR t ON t.USRUID = te.USRUID
Use Following Query, it Will update only rows exists on both table.
Update dbo.T_USR
SET dbo.T_USR.USRID =
(SELECT dbo.#TEMP3.EMPID
FROM #TEMP3
Where dbo.T_USR.USRUID = dbo.#TEMP3.USRUID
)
WHERE exists (
select * from #TEMP3 AS TT where dbo.T_USR.USRUID = TT.USRUID)

Conditional UPDATE for non-related tables in SQLServer 2008

I'm trying to update historicTable (8k records containing employee data) when 2 IDs match (one company id and the other area id) with the tempTable, which is a temporary table with the data I want to update (SUBG_ROT), else (the column stays empty), it should copy data from colx to coly from the same historic table. I was thinking of a JOIN/INNER JOIN but I can't get the structure at all, so I did a stored procedure with a conditional update, but I can't figure out how to call the temporary table. Any hints on the logic/code are appreciated, I'm starting on SQL Server so I'm kinda clueless.
In my mind the code should do:
Update historicTable set SUBG when h.id1 = t.id1 and h.id2 = t.id2 then h.SUBG = t.SUBG else h.id1 = h.SUBG
And this is the code
CREATE PROCEDURE updateHistoric
AS
UPDATE dbo.historicTable
SET SUBG_ROT = CASE
WHEN id1 = temptable.id1 AND id2 = temptable.id2
THEN SUBG_ROT = temptable.SUBG_ROT
ELSE SUBG_ROT = AREA
END
GO
You can try using OUTER APPLY like follownig.
UPDATE t
SET t.subg_rot = CASE
WHEN o.subg_rot IS NOT NULL THEN o.subg_rot
WHEN t.subg_rot IS null then t.area
ELSE t.subg_rot
END
FROM dbo.historictable t
OUTER APPLY (SELECT TOP 1 subg_rot
FROM temptable t2
WHERE t1.id1 = t2.id1
AND t1.id2 = t2.id2)o
Note: Same thing is also possible using LEFT JOIN or sub queries.

Update with Sub Query Derived Table Error

I have the following SQL statement to simply update the #temp temp table with the latest package version number in our Sybase 15 database.
UPDATE t
SET versionId = l.latestVersion
FROM #temp t INNER JOIN (SELECT gp.packageId
, MAX(gp.versionId) latestVersion
FROM Group_Packages gp
WHERE gp.groupId IN (SELECT groupId
FROM User_Group
WHERE userXpId = 'someUser')
GROUP BY gp.packageId) l
ON t.packageId = l.packageId
To me (mainly Oracle & SQL Server experience more than Sybase) there is little wrong with this statement. However, Sybase throws an exception:
You cannot use a derived table in the FROM clause of an UPDATE or DELETE statement.
Now, I don't get what the problem is here. I assume it is because of the aggregation / GROUP BY being used. Of course, I could put the sub query in a temp table and join on it but I really want to know what the 'correct' method should be and what the hell is wrong.
Any ideas or guidance would be much appreciated.
It seems that SYBASE doesn't support nested queries in UPDATE FROM class. Similar problem
Try to use this:
UPDATE #temp
SET versionId = (SELECT MAX(gp.versionId) latestVersion
FROM Group_Packages gp
WHERE gp.packageId=#temp.packageId
and
gp.groupId IN (SELECT groupId
FROM User_Group
WHERE userXpId = 'someUser')
)
And also what if l.latestVersion is NULL? Do you want update #temp with null ? if not then add WHERE:
WHERE (SELECT MAX(gp.versionId)
....
) is not null
I guess this is a limitation of Sybase (not allowing derived tables) in the FROM clause of the UPDATE. Perhaps you can rewrite like this:
UPDATE t
SET t.versionId = l.versionId
FROM #temp t
INNER JOIN
Group_Packages l
ON t.packageId = l.packageId
WHERE
l.groupId IN ( SELECT groupId
FROM User_Group
WHERE userXpId = 'someUser')
AND
l.versionId =
( SELECT MAX(gp.versionId)
FROM Group_Packages gp
WHERE gp.groupId IN ( SELECT groupId
FROM User_Group
WHERE userXpId = 'someUser')
AND gp.packageId = l.packageId
) ;
Your table alias for #temp is called "t" and your original table is called "t".
My guess is that this is the problem.
I think you want to start with:
update #temp
Does this syntax work in Sybase?
update dstTable T
set (T.field1, T.field2, T.field3) =
(select S.value1, S.value2, S.value3
from srcTable S
where S.key = T.Key);
I believe the correlated subquery can be as complicated as you like (including using CTE. etc). It just has to project the right number (and type) of values.

Copy values from one table to another in SQL

I have 2 tables. I need to update all rows of table 1 with the values in specific columns from table 2. They have the same structure.
UPDATE #TempTable
SET [MyColumn] =
(
SELECT [MyColumn]
FROM
[udf_AggregateIDs] (#YearId) AS [af]
INNER JOIN [MyForm] ON
(
[af].[FormID] = [MyForm].[FormID] AND
[af].[FormID] = #MyFormId
)
WHERE [Description] = [MyForm].[Description]
)
I get an error saying Subquery returned more than 1 value. I only added the where clause in because i thought sql is struggling to match the rows, but both tables have the same rows.
It should return multiple values because i'm trying to copy across all rows for MyColumn from the one table to the other.
Ideas?
is Description unique ?
select [Description], count(*) from [MyForm] group by [Description] having count(*)>1
You don't need a sub query..just join the tables..
same type of question has been answered here. Hope it helps.
Have to guess here because your query isn't self-documenting. Does MyColumn come from the function? Does #TempTable have a description column? Who knows, because you didn't prefix them with an alias? Try this. You may have to adjust since you know your schema and we don't.
UPDATE t
SET [MyColumn] = func.MyColumn -- have to guess here
FROM dbo.[udf_AggregateIDs] (#YearId) AS func
INNER JOIN dbo.MyForm AS f
ON func.FormID = f.FormID
INNER JOIN #TempTable AS t
ON t.Description = f.Description -- guessing here also
WHERE f.FormID = #MyFormID;

SQL Query Help, Need some expert opinions

I have this query and it works well, the question is if there is a way to have the query NULL the column value if it doesn't exist in the table?
UPDATE PRD_WOCNT
SET c1 = (
SELECT WO_CNT.RATE FROM WO_CNT
WHERE WO_CNT.CNT = PRD_WOCNT.c1
)
WHERE EXISTS (
SELECT WO_CNT.CNT FROM WO_CNT
WHERE WO_CNT.CNT = PRD_WOCNT.c1
)
Right it uses a second table to update the main table values of sales codes into their commission rates. but sometimes a sales code is entered that doesn't exist in the table so it adds the sales code as a dollar value and makes the added total wrong.
I don't know too much PHP and trying not to bother with it if I can simply alter the query... it's a stab in the dark, have a feeling I'll need to do this on the PHP side but have no idea how I can have my query tell the php it doesn't exist.
UPDATE a
SET c1 = WO_CNT.RATE
FROM PRD_WOCNT a
LEFT JOIN WO_CNT b
ON b.CNT = a.c1
I havn't executed. I think it will work. Try this query:
UPDATE PRD_WOCNT SET c1 = CASE
WHEN EXISTS (
SELECT WO_CNT.CNT FROM WO_CNT
WHERE WO_CNT.CNT = PRD_WOCNT.c1
)
THEN (
SELECT WO_CNT.RATE FROM WO_CNT
WHERE WO_CNT.CNT = PRD_WOCNT.c1
)
ELSE NULL
END