Update a Column with multiple values for different where conditon - Hibernate - sql

I need to UPDATE a column in multiple rows(around 2000) with a different value for each WHERE condition. I've List of values for Where condition and another list of values to update the column.
My updates are being done as individual queries like this:
UPDATE tablename SET widget='xxx' WHERE widget='zamu';
UPDATE tablename SET widget='yyy' WHERE widget='flabu';
Is there any way to do something like this in a single query?
Thanks.

Use CASE statement and IN operator
UPDATE tablename SET widget = CASE WHEN widget='zamu' THEN 'xxx' ELSE 'yyy' END
WHERE widget IN ('zamu', 'flabu')

UPDATE tablename
SET widget = CASE
WHEN widget = 'zamu' THEN 'xxx'
ELSE 'yyy'
END
WHERE widget IN ('zamu', 'flabu')

Related

I want to update a column of the table if value is not null in oracle update statement?

I want to update a column of the table if the value is not null and if value is null then do not update that particular column and other columns should be updated in oracle update statement . Is there any simple way to achieve this in sql query ?
Looks like CASE expression might help.
update your_table set
that_column = case when that_column is not null then 'new value'
else null
end,
another_column = 123,
yet_another = 'ABC'
where ...

Update columns in same table

I want to update BRCD_NEW column in table branches with the condition applied on another column BRCD in the same table, here is my code, but it returns error
single-row subquery returns more than one row
update branches set brcd_new=(
select
case
when BRCD between '5000' and '5999' then CONCAT('PK001',BRCD)
else CONCAT('PK002',BRCD)
end
from branches);
You don't need the subquery to achieve what you are doing. Use CASE statement to get the value you need and assign in to your column in SET statement:
update branches
set brcd_new =
case
when BRCD between '5000' and '5999' then CONCAT('PK001',BRCD)
else CONCAT('PK002',BRCD)
end
-- WHERE <your filters (if needed)>

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

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,

How can I update the value of multiple rows to a different specified value in the same column?

Say I have a table where there are product IDs, product desc., and the language of each desc. I would like it so that if there was a description with a NULL value for American-English, it would update to the British-English version of that the description for that product. Is there a way to do this using the update command in SQL?
I normally prefer this syntax for updating values in one table from values in another (or in this case the same) table, b/c it is easy to change the UPDATE...SET to a SELECT for testing and to quickly see what values would be updated.
UPDATE p_US
SET p_US.product_desc = p_GB.product_desc
FROM dbo.product p_US
INNER JOIN dbo.product p_GB ON p_US.productID = p_GB.productID
WHERE p_US.language_code = 'US'
AND p_GB.language_code = 'GB'
AND p_US.product_desc IS NULL
;
and then you can swap out the first two lines above with this for quick testing to see what would be updated:
SELECT p_US.productID, p_US.product,
oldDesc = p_US.product_desc, newDesc = p_GB.product_desc
update [table] set [column]= case [change factor] when '1' then 'X' else 'Y' end where [where clause]
Maybe:
UPDATE my_table SET desc=(SELECT desc from my_table WHERE my_table.id=id AND my_table.lang='british') WHERE lang='american' and desc is NULL;

How to update more than one column in SQL?

I have a SQL which I am using for updating many rows at the same time using a complex case condition. Currently, I am setting 2 column using the same CASE condition.
For example, I need to do something like:
UPDATE MyTable
SET([MyColumn1], [MyColumn2]) = ('','')
What I am doing now is:
UPDATE MyTable
SET [MyColumn1] = COMPLEX CASE RETURN STRING,
[MyColumn2] = 100% SAME COMPLEX CASE RETURN A Different STRING
I tried but getting error. Is this is possible in SQL SERVER?
It is supported in IBM Db, http://publib.boulder.ibm.com/infocenter/idshelp/v111/index.jsp?topic=/com.ibm.sqls.doc/sqls919.htm
But it seems there is no support in SQL Server
Update command syntax is:
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
update mytable
SET [MyColumn1] = COMPLEX CASE RETURN STRING,
[MyColumn2] = 100% SAME COMPLEX CASE RETURN A Different STRING
WHERE some_column=some_value;// please have the where condition
then only we can update the corresponding raw.
updating means we are changing some existing values so we need to provide the location where we want this change,for that we can use the where condition..
example
update author
set name="onv kurup"
set book="oralude"
where authorid=112;
if we are not giving the where condition all the data of the table will be get updated with same value in the update query we have given
After searching a lot, I have found the answer,
update table1
set col1 = a.col1, col2 = a.col2, col3 = a.col3 from
table1 as a Join on tablefunction
where table1.col1 <expression>
http://geekswithblogs.net/phoenix/archive/2009/10/13/update-multiple-columns-on-sql-server.aspx