In this example can UPDATE statements be combined? - sql

I quite often have rows of code like the following:
UPDATE my_table SET name = 'x' WHERE Original = 'a'
UPDATE my_table SET name = 'y' WHERE Original = 'b'
UPDATE my_table SET name = 'z' WHERE Original = 'c'
UPDATE my_table SET name = 'k' WHERE Original = 'd'
UPDATE my_table SET name = 'm' WHERE Original = 'e'
UPDATE my_table SET name = 'n' WHERE Original = 'f'
Can I combine/shorten this code into one UpDate statement - or are they best just left as they are?

UPDATE my_table
SET name =
CASE
WHEN Original = 'a' THEN 'x'
WHEN Original = 'b' THEN 'y'
...
END
That will update EVERY row. So if there's an Original value you haven't specified, it will be set to NULL. So you might want to limit the update to just those you want to update, with a WHERE clause, like so:
WHERE Original IN ('a', 'b', ...)
OR, as an alternative, you could use an ELSE statement, which leaves the name value as is, if it doesn't have a match in the WHEN statements, like so:
CASE
WHEN Original = 'a' THEN 'x'
WHEN Original = 'b' THEN 'y'
...
ELSE name
END

You could use a case statement:
UPDATE my_table
SET name =
case Original
when 'a' then 'x'
when 'b' then 'y'
...
else name -- Preserve original
end
The else clause makes sure you're not modifying a name if it's not matched in the case.

You could use a table value constructor and a from clause, if these values aren't already in a table:
update mt set name = t.name
from
my_table mt
inner join
(values
('a','x'),
('b','y'),
('c','z'),
('d','k'),
('e','m'),
('f','n')
) t(original,name)
on
mt.Original = t.original

Related

Get dynamic column names from another table for SQL Update

I have a temp table like below:
And a main table with the below columns:
ACC_NO, MTH1, MTH2, MTH3, MTH4, MTH5, MTH6, MTH7, MTH8, MTH9, MTH10, MTH11, MTH12, SELECTED
I would like to update the main table for only the column names exists in the temp table. In this case, my desired output would be:
Update table_name
Set MTH2 = some_value,
MTH4 = some_value,
MTH5 = some_value,
MTH6 = some_value,
MTH7 = some_value
Where selected = 'Y'
For the revised question, you can use case expressions:
Update table_name
Set MTH2 = (CASE WHEN EXISTS (SELECT 1 FROM temp tt WHERE tt.monthperiod = 'MTH2') THEN some_value ELSE mth2 END),
MTH4 = (CASE WHEN EXISTS (SELECT 1 FROM temp tt WHERE tt.monthperiod = 'MTH4') THEN some_value ELSE mth4 END),
. . .
Where selected = 'Y'

simplify multiple union in sql

i am trying to simply the following union query .Basically i am trying to get all the possible values from same table different column and i have to take value which are not equal to no then replace them with specific text when they are from respective column.
select 'a' from Mytable where a!='no' and id='1'
union
select 'b' from Mytable where b!='no' and id='1'
union select 'c' from Mytable where c!='no' and id='1'
so my table structure will be
id Acolumn BColumn Ccolumn
1 123a no 345v
so my expected result is
a c
so please suggest me to simplify this query thanks in advance
According to what you've written, this might be OK.
select case when a <> 'no' and id = '1' then 'a'
when b <> 'no' and id = '1' then 'b'
when c <> 'no' and id = '1' then 'c'
end
from mytable
where (a <> 'no' and id = '1')
or (b <> 'no' and id = '1')
or (c <> 'no' and id = '1')

ASP classic - Microsoft SQL and multiple where clauses on an UPDATE

I have the following string but it seems to not like it:
"UPDATE
table
SET a = '', b = '34'
WHERE id = '1000001';
SET a = '1111', b = '11'
WHERE id = '100210';
SET a = '', b = '2'
WHERE id = '8002059';"
In my asp classic code that string is on one line.. Is this the correct way of doing multiple update/where statements?
No, it will give you an error.
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'table'.
And you can't use multiple where in UPDATE statement.
Try this instead:
UPDATE
[table]
set a =
( case when id IN ( '1000001', '8002059') then ''
when id = '100210' then '1111'
end
),
b =
( case when id = '1000001' then '34'
when id = '100210' then '11'
when id = '8002059' then '2'
end
)
where id in ('1000001', '100210', '8002059');
It will like this better (3 statements).
UPDATE table SET a = '', b = '34' WHERE id = '1000001';
UPDATE table SET a = '1111', b = '11' WHERE id = '100210';
UPDATE table SET a = '', b = '2' WHERE id = '8002059';
Alternatively you can use two CASE statements in your SETs. It won't be as readable but will run slightly more quickly having 1 database round trip rather than 3 if this is a 1-off situation where you frequently need exactly 3 conditional updates performed (unlikely).
UPDATE table SET
a = CASE id WHEN '1000001' THEN '' WHEN '100210' THEN '1111' WHEN '8002059' THEN '' ELSE a END,
b = CASE id WHEN '1000001' THEN '34' WHEN '100210' THEN '11' WHEN '8002059' THEN '2' ELSE b END
WHERE id IN ('1000001', '100210', '8002059');

Sql statement for mutually exclusive events

I am trying to run an sql statement on an iSeries that will output retuls based on a type parameter I pass in.
Just say mytable has a field called field1. field1 contains Y,N and NULL values.
A type of 'Y' should return just 'Y' values.
A type of 'N' should return not 'Y' values. (ie. Null, N and any other junk in the field)
I tried this...
select *
from mytable
where field1 in case when :type = 'Y' then 'Y'
else (select field1 from mytable where field1 <> 'Y') end
However, this does not work.
I believe the logic you are looking for is this:
SELECT *
FROM myTable
WHERE (:type = 'Y' AND field1 IS NOT null AND field1 = 'Y')
OR (:type <> 'Y' AND (field1 IS null OR field1 <> 'Y'))
(keep in mind the fact that short-circuit logic is not garuanteed with SQL...)
Remember that null doesn't really compare to anything, and it's best to call out the fact that you actually want it (in the second case).

Using IF..ELSE in UPDATE (SQL server 2005 and/or ACCESS 2007)

I need to set a query like below:
UPDATE XXXXXX
IF column A = 1 then set column B = 'Y'
ELSE IF column A = 2 then set column C = 'Y'
ELSE IF column A = 3 then set column D = 'Y'
and so on and so forth...
I am able to do this using multiple queries but was wondering, if I can do it in just 1 statement instead.
this should work
update table_name
set column_b = case
when column_a = 1 then 'Y'
else null
end,
set column_c = case
when column_a = 2 then 'Y'
else null
end,
set column_d = case
when column_a = 3 then 'Y'
else null
end
where
conditions
the question is why would you want to do that...you may want to rethink the data model. you can replace null with whatever you want.
Yes you can use CASE
UPDATE table
SET columnB = CASE fieldA
WHEN columnA=1 THEN 'x'
WHEN columnA=2 THEN 'y'
ELSE 'z'
END
WHERE columnC = 1