Find multiple SQL columns and update based on defined data listed in query - sql

I have an update query in which I am trying to locate data in a column from a single table. All while taking other defined data listed in the query to update another column in the same table once a match has been found with that original search. Below is an example of my update statement. My end goal is to find '003447710' then update AltId to '540112'
UPDATE Site
SET AltId = ('540112'
'540129'
'540142'
'540143')
WHERE CCMFStatus in ('003447710',
'002754540',
'003564370',
'005942870')
I am sure there may already be something like this out there but I am really having trouble on an easy method on how to do this quickly and accurately.

Try this
update site
set altid = a.altid
from
(select altid,CCMFstatus from site) as a
where site.CCMFstatus = a.CCMFstatus

The best way might be multiple update statements:
UPDATE Site
SET AltId = '540112'
WHERE CCMFStatus = '003447710';
And so on.
If not, you can do this with a giant case statement or a join:
WITH values as (
SELECT '003447710' as oldstatus, '540112' as newaltid UNION ALL
SELECT '002754540', '540129' UNION ALL
SELECT '003564370', '540142' UNION ALL
SELECT '005942870', '540143'
)
UPDATE s
SET AltId = va.newaltid
FROM site s JOIN
values v
ON s.CCMFStatus = v.oldstatus;
If you already have the values in a table, then you don't need the WITH statement. You can just use the table.

Have you tried using CASE statement?
UPDATE SITE SET AltID = (CASE
WHEN CCMFStatus = '003447710' THEN '540112'
WHEN CCMFStatus = '002754540' THEN '540129'
END)
WHERE
CCMFStatus in ('003447710', '002754540', '003564370', '005942870');
BR,

Related

How to improve simple SQL query by using where exists statement?

Could you please help me out on improving simple query below.
Based on a last modified date (ODS_CHANGE_DATE), I set a 'ODS_CHANGE_FLAG' to 'Y' if it was modified in the last two months (done in previous query not shown here).
Then I want to set a RELOAD_INDICATOR flag to 'Y' for all records that were created in those months (based on CREATION_DATE). This way I can send all records of those months where at least 1 record was modified in the past two months.
The query below does the trick but I believe it can be optimised using a where exist statement but I am not familiar with it and I can not get it to work for the example below. Would you be willing to help me out so I can do it in one statement without using temp table?
Thanks!
use ODS
update ODS_DCF_OUTPUT set reload_indicator = 'N'
drop table #dcf_output_changed
select concat(DATEPART(year, creation_date),DATEPART(month, creation_date)) as RELOAD_MONTH_CODES
into #dcf_output_changed
from ODS_DCF_OUTPUT
where ODS_CHANGE_FLAG = 'Y'
update ODS_DCF_OUTPUT
set ODS_DCF_OUTPUT.reload_indicator = 'Y'
FROM ODS_DCF_OUTPUT as a
inner join #dcf_output_changed as b on b.RELOAD_MONTH_CODES = concat(DATEPART(year, a.CREATION_DATE),DATEPART(month, a.CREATION_DATE))
I believe your query could be simplified without the temporary table - as long as 'ODS_CHANGE_FLAG' is set to 'Y' in another query - as follows:
update ODS_DCF_OUTPUT set reload_indicator = 'N' ;
UPDATE ODS_DCF_OUTPUT
SET a.reload_indicator = 'Y'
FROM ODS_DCF_OUTPUT a
WHERE
concat(DATEPART(year, a.CREATION_DATE),DATEPART(month, a.CREATION_DATE))
IN (
SELECT DISTINCT
concat(DATEPART(year, b.CREATION_DATE),DATEPART(month, b.CREATION_DATE))
FROM ODS_DCF_OUTPUT b WHERE b.ODS_CHANGE_FLAG = 'Y'
)
Make sure to take backup before messing with your data.

sql server if statement not working

Can anyone advise me as to what is wrong with the following SQL server update statement:
IF (SELECT * FROM TBL_SystemParameter WHERE code='SOUND_WRONG_GARMENT') = ''
GO
UPDATE TBL_SystemParameter
SET [Value] = 'Ping.wav'
WHERE ID = (SELECT ID
FROM TBL_SystemParameter
WHERE code = 'SOUND_WRONG_GARMENT')
You don't need an if statement - you can just run the update statement, and if the subquery returns no rows, no rows will be updated. The if won't really save anything - you're performing two queries instead of one.
You either want
UPDATE TBL_SystemParameter
SET [Value] = 'Ping.wav'
WHERE ID In (SELECT ID
FROM TBL_SystemParameter
WHERE code = 'SOUND_WRONG_GARMENT')
if there are multiple ID's with that code OR use
UPDATE TBL_SystemParameter
SET [Value] = 'Ping.wav'
WHERE code = 'SOUND_WRONG_GARMENT'
either way and lose the IF statement as #Mureinik said.
Although Mureinik's answer is the logical solution to this, I will answer why this isn't actually working. Your condition is wrong, and this approach will work instead using IF EXISTS:
IF EXISTS (SELECT * FROM TBL_SystemParameter WHERE code='SOUND_WRONG_GARMENT')
BEGIN
UPDATE TBL_SystemParameter
SET [Value] = 'Ping.wav'
WHERE ID IN (SELECT ID
FROM TBL_SystemParameter
WHERE code = 'SOUND_WRONG_GARMENT')
END
As a side note, you're using an = sign instead of IN, which means you'll be matching to an arbitrary singular ID and only update 1 row based on this. To use a set based operation, use the IN clause.
You could actually 'golf' this by doing away with the derived query altogether, and using a simple WHERE code='SOUND_WRONG_GARMENT' on the table you're updating on.

Update a table from another table on multiple columns in Oracle 11g

Oracle 11g SQL & both tables have the same column definitions:
VARCHAR2(11)
NUMBER
DATE
DATE
I tried to find a solution to this problem, and this is what I ended up with, which fails:
update jjjTable
set [fourthCol] = B.[fourthOtherCol]
from jjjTable, otherTable B
where jjjTable.[firstCol] = B.[firstOtherCol]
and jjjTable.[secondCol] = B.[secondOtherCol]
and jjjTable.[thirdCol] = B.[thirdOtherCol]
I'm under the impression that I need to have the from in this was based on this article:
SQL update from one Table to another based on a ID match and the edited response from Shivkant
I'm under the impression that I may need to use a join based on this article:
How do I UPDATE from a SELECT in SQL Server? and the response from Robin Day
but as I understand it, joins are only on one column match per row. I'm interested in matching on 3 elements, and I'm not finding a clear path for solution.
Any direction would be well received.
This is what I ended up needing to do as a solution:
DECLARE
CURSOR j_CUR IS
SELECT A.[fourthCol]
FROM JJJtable A, otherTable B
WHERE A.[firstCol] = B.[firstOtherCol]
and A.[secondCol] = B.[secondOtherCol]
and A.[thirdCol] = B.[thirdOtherCol]
FOR UPDATE OF B.[fourthOtherCol];
SOME_DATE DATE;
BEGIN
FOR IDX IN j_CUR LOOP
SOME_DATE :=(IDX.[fourthCol]);
UPDATE otherTable
SET [fourthOtherCol] = SOME_DATE
WHERE CURRENT OF j_CUR;
END LOOP;
END;
Thank you for your efforts and guidance.
This is the close best I was able to get it to work on my similar use case. Try this out.
update jjjTable
SET jjjTable.[fourthCol] = (SELECT distint otherTable.fourthOtherCol from otherTable
WHERE otherTable.firstOtherCol = jjjTable.firstCol and
otherTable.secondOtherCol = jjjTable.secondCol and
otherTable.thirdOtherCol = jjjTable.thirdCol)
WHERE EXISTS (SELECT distint otherTable.fourthOtherCol from otherTable
WHERE otherTable.firstOtherCol = jjjTable.firstCol and
otherTable.secondOtherCol = jjjTable.secondCol and
otherTable.thirdOtherCol = jjjTable.thirdCol);

Oracle SQL update

I've tried searching for this particular topic here, but haven't found the answer... Anyway, my aim is to update table (let's call it t_item), specifically column owner_id with values depending on another table (t_item_geo which is in turn linked to t_geo).
I'm not entirely sure whether the syntax below is actually valid for update statements.
UPDATE t_item SET owner_id= 6993 WHERE t_item.owner_id in
(SELECT t_item.owner_id FROM
t_item,
t_item_geo,
t_geo
WHERE
t_item.id = t_item_geo.item_id and
t_item_geo.geo_id = t_geo.id and
t_item.owner_id in (SELECT id FROM t_user WHERE network_id='fffffff') and
t_geo.id in (SELECT id FROM t_geo WHERE full_name = 'yyyyyyy')
);
Anyway, my problem with this query is that it updates far more rows than it should - if I separate just the select statement Oracle returns ~750 rows but the udpate itself updates more than 4000 rows. It's almost as if the condition was completely ignored - which would point me to perhaps incorrect syntax.
I need to update specific value in the table based on the select from few other 'joined' tables. Hope it makes sense.
Thanks for any contribution!
UPDATE: sorry - maybe it wasn't clear from the question itself, but the correct number of edited items should be ~750 and not ~4000. Thanks!
try this
MERGE INTO t_item
USING
(
SELECT t_item.owner_id FROM
t_item,
t_item_geo,
t_geo,
t_item.rowid rowid_sub
WHERE
t_item.id = t_item_geo.item_id and
t_item_geo.geo_id = t_geo.id and
t_item.owner_id in (SELECT id FROM t_user WHERE network_id='fffffff') and
t_geo.id in (SELECT id FROM t_geo WHERE full_name = 'yyyyyyy')
) on (rowid = rowid_sub)
WHEN MATCHED THEN
UPDATE SET owner_id= 6993;

Writing a single UPDATE statement that prevents duplicates

I've been trying for a few hours (probably more than I needed to) to figure out the best way to write an update sql query that will dissallow duplicates on the column I am updating.
Meaning, if TableA.ColA already has a name 'TEST1', then when I'm changing another record, then I simply can't pick a value for ColA to be 'TEST1'.
It's pretty easy to simply just separate the query into a select, and use a server layer code that would allow conditional logic:
SELECT ID, NAME FROM TABLEA WHERE NAME = 'TEST1'
IF TableA.recordcount > 0 then
UPDATE SET NAME = 'TEST1' WHERE ID = 1234
END IF
But I'm more interested to see if these two queries can be combined into a single query.
I am using Oracle to figure things out, but I'd love to see a SQL Server query as well. I figured a MERGE statement can work, but for obvious reasons you can't have the clause:
..etc.. WHEN NOT MATCHED UPDATE SET ..etc.. WHERE ID = 1234
AND you can't update a column if it's mentioned in the join (oracle limitation but not limited to SQL Server)
ALSO, I know you can put a constraint on a column that prevents duplicate values, but I'd be interested to see if there is such a query that can do this without using constraint.
Here is an example start-up attempt on my end just to see what I can come up with (explanations on it failed is not necessary):
ERROR: ORA-01732: data manipulation operation not legal on this view
UPDATE (
SELECT d.NAME, ch.NAME FROM (
SELECT 'test1' AS NAME, '2722' AS ID
FROM DUAL
) d
LEFT JOIN TABLEA a
ON UPPER(a.name) = UPPER(d.name)
)
SET a.name = 'test2'
WHERE a.name is null and a.id = d.id
I have tried merge, but just gave up thinking it's not possible. I've also considered not exists (but I'd have to be careful since I might accidentally update every other record that doesn't match a criteria)
It should be straightforward:
update personnel
set personnel_number = 'xyz'
where person_id = 1001
and not exists (select * from personnel where personnel_number = 'xyz');
If I understand correctly, you want to conditionally update a field, assuming the value is not found. The following query does this. It should work in both SQL Server and Oracle:
update table1
set name = 'Test1'
where (select count(*) from table1 where name = 'Test1') > 0 and
id = 1234