MERGE statement with two WHEN MATCHED - sql-server-2008

I have this MERGE statement working fine:
MERGE INTO product_replenishment PR
USING ( SELECT * FROM traxs_temp..__MinMaxImport WHERE session_id = #session_id ) T
ON T._product = PR.product AND T._branch_no = PR.branch_no
WHEN MATCHED AND T._stock_warehouse IS NOT NULL THEN
UPDATE SET
date_time_updated = GETDATE(),
user_no_updated = #user_no,
stock_warehouse = T._stock_warehouse
WHEN NOT MATCHED BY TARGET AND T._stock_warehouse IS NOT NULL THEN
INSERT
(date_time_created,
date_time_updated,
user_no_created,
user_no_updated,
branch_no,
product,
stock_warehouse,
archive)
VALUES
(GETDATE(),
GETDATE(),
#user_no,
#user_no,
T._branch_no,
T._product,
T._stock_warehouse,
0);
I want to add another WHEN MATCHED statement like this:
WHEN MATCHED AND T._stock_warehouse IS NOT NULL THEN
UPDATE SET
date_time_updated = GETDATE(),
user_no_updated = #user_no,
stock_warehouse = T._stock_warehouse
WHEN MATCHED AND T._stock_warehouse IS NULL THEN
UPDATE SET
date_time_updated = GETDATE(),
user_no_updated = #user_no,
archive = 1
But I get an error: An action of type 'WHEN MATCHED' cannot appear more than once in a 'UPDATE' clause of a MERGE statement.
Is it impossible to achieve what I'm trying to do?

The general case can be emulated using CASE expressions as I've shown in this answer here, or in this blog post here. But your case is more specific, because the difference between the two clauses is simpler than the general case. You can combine them into one clause:
WHEN MATCHED THEN UPDATE SET
-- These are always updated the same way, regardless of the WHEN MATCHED AND predicate
date_time_updated = GETDATE(),
user_no_updated = #user_no,
-- These depend on the relevant WHEN MATCHED AND predicate, so use CASE
stock_warehouse = CASE
WHEN T._stock_warehouse IS NOT NULL THEN T._stock_warehouse
ELSE stock_warehouse
END,
archive = CASE WHEN T._stock_warehouse IS NULL THEN 1 ELSE archive END

Related

Update and append to all items in a specific column

How do I append a character to all items `purchase_id' here is a manual example of what I want...
SELECT *
FROM `loadable_link`
WHERE `product_sku` = '2101-R'
ORDER BY `customer_id` DESC
Then select from purchased_id and append a '0' to all purchased ID's
UPDATE `loadable_link` SET `purchased_id` = '11165690'
WHERE `loadable_link`.`purchased_id` = 1116569;
You can update the table according to the condition in the original select statement.
If purchase_id is a number, you can multiply it by 10:
UPDATE `loadable_link`
SET `purchase_id` = `purchase_id` * 10
WHERE `product_sku` = '2101-R'
If purchase_id is a string, you can concatenate a 0 to it:
UPDATE `loadable_link`
SET `purchase_id` = CONCAT(`purchase_id`, '0')
WHERE `product_sku` = '2101-R'
UPDATE `loadable_link`
SET `purchased_id` = CONCAT(`purchased_id`, "0")
WHERE `product_sku` = "2101-R";
This can be achieved in one UPDATE query, take the original value of each row and CONCAT() to append a 0 to the end of the existing purchase_id.

SQLite Query SET variable on a single row

I have a table called "allarmi_ingressi" in SQLite, with a lot of rows in it.
I want to create a query that changes the variable on my column "visto" to 1, if "visto=0", and to 0, if "visto=1".
This is what i made:
UPDATE allarmi_ingressi SET visto = '1' WHERE visto = '0'
Of course this modify every row in the column "visto";
I want to know if it's possible to modify it "selecting" it by the primary key, in my case "id_allarme".
In a SELECT query, you would use the WHERE clause to find rows with a specific id_allarme value.
The same WHERE clause can be used with UPDATE:
UPDATE allarmi_ingressi
SET visto = 1 - visto
WHERE id_allarme = ?;
Use CASE Expression
Query
update allarmi_ingressi
set visto = (
case visto when '1' then '0'
when '0' then '1'
else visto end
)
where id_allarme = __; -- id here

UPDATE multiple rows with different values in one query in MySQL

I am trying to understand how to UPDATE multiple rows with different values and I just don't get it. The solution is everywhere but to me it looks difficult to understand.
For instance, three updates into 1 query:
UPDATE table_users
SET cod_user = '622057'
, date = '12082014'
WHERE user_rol = 'student'
AND cod_office = '17389551';
UPDATE table_users
SET cod_user = '2913659'
, date = '12082014'
WHERE user_rol = 'assistant'
AND cod_office = '17389551';
UPDATE table_users
SET cod_user = '6160230'
, date = '12082014'
WHERE user_rol = 'admin'
AND cod_office = '17389551';
I read an example, but I really don't understand how to make the query. i.e:
UPDATE table_to_update
SET cod_user= IF(cod_office = '17389551','622057','2913659','6160230')
,date = IF(cod_office = '17389551','12082014')
WHERE ?? IN (??) ;
I'm not entirely clear how to do the query if there are multiple condition in the WHERE and in the IF condition..any ideas?
You can do it this way:
UPDATE table_users
SET cod_user = (case when user_role = 'student' then '622057'
when user_role = 'assistant' then '2913659'
when user_role = 'admin' then '6160230'
end),
date = '12082014'
WHERE user_role in ('student', 'assistant', 'admin') AND
cod_office = '17389551';
I don't understand your date format. Dates should be stored in the database using native date and time types.
MySQL allows a more readable way to combine multiple updates into a single query. This seems to better fit the scenario you describe, is much easier to read, and avoids those difficult-to-untangle multiple conditions.
INSERT INTO table_users (cod_user, date, user_rol, cod_office)
VALUES
('622057', '12082014', 'student', '17389551'),
('2913659', '12082014', 'assistant','17389551'),
('6160230', '12082014', 'admin', '17389551')
ON DUPLICATE KEY UPDATE
cod_user=VALUES(cod_user), date=VALUES(date)
This assumes that the user_rol, cod_office combination is a primary key. If only one of these is the primary key, then add the other field to the UPDATE list.
If neither of them is a primary key (that seems unlikely) then this approach will always create new records - probably not what is wanted.
However, this approach makes prepared statements easier to build and more concise.
UPDATE table_name
SET cod_user =
CASE
WHEN user_rol = 'student' THEN '622057'
WHEN user_rol = 'assistant' THEN '2913659'
WHEN user_rol = 'admin' THEN '6160230'
END, date = '12082014'
WHERE user_rol IN ('student','assistant','admin')
AND cod_office = '17389551';
You can use a CASE statement to handle multiple if/then scenarios:
UPDATE table_to_update
SET cod_user= CASE WHEN user_rol = 'student' THEN '622057'
WHEN user_rol = 'assistant' THEN '2913659'
WHEN user_rol = 'admin' THEN '6160230'
END
,date = '12082014'
WHERE user_rol IN ('student','assistant','admin')
AND cod_office = '17389551';
To Extend on #Trevedhek answer,
In case the update has to be done with non-unique keys, 4 queries will be need
NOTE: This is not transaction-safe
This can be done using a temp table.
Step 1: Create a temp table keys and the columns you want to update
CREATE TEMPORARY TABLE temp_table_users
(
cod_user varchar(50)
, date varchar(50)
, user_rol varchar(50)
, cod_office varchar(50)
) ENGINE=MEMORY
Step 2: Insert the values into the temp table
Step 3: Update the original table
UPDATE table_users t1
JOIN temp_table_users tt1 using(user_rol,cod_office)
SET
t1.cod_office = tt1.cod_office
t1.date = tt1.date
Step 4: Drop the temp table
In php, you use multi_query method of mysqli instance.
$sql = "SELECT COUNT(*) AS _num FROM test;
INSERT INTO test(id) VALUES (1);
SELECT COUNT(*) AS _num FROM test; ";
$mysqli->multi_query($sql);
comparing result to transaction, insert, case methods in update 30,000 raw.
Transaction: 5.5194580554962
Insert: 0.20669293403625
Case: 16.474853992462
Multi: 0.0412278175354
As you can see, multiple statements query is more efficient than the highest answer.
Just in case if you get error message like this:
PHP Warning: Error while sending SET_OPTION packet
You may need to increase the max_allowed_packet in mysql config file.
UPDATE Table1 SET col1= col2 FROM (SELECT col2, col3 FROM Table2) as newTbl WHERE col4= col3
Here col4 & col1 are in Table1. col2 & col3 are in Table2 I Am trying to update each col1 where col4 = col3 different value for each row
I did it this way:
<update id="updateSettings" parameterType="PushSettings">
<foreach collection="settings" item="setting">
UPDATE push_setting SET status = #{setting.status}
WHERE type = #{setting.type} AND user_id = #{userId};
</foreach>
</update>
where PushSettings is
public class PushSettings {
private List<PushSetting> settings;
private String userId;
}
it works fine

MySQL update column which is a value in another column

This is my previous question related to the my query.
MySQL select column which is a value in another column
The problem is that want to do operations on the values extracted and store it back into the original db. I've tried using a update & case but am not able to achieve it.
update msisdn_table
CASE reason
WHEN 'NoAnswer' THEN (case when (NoAnswer>0) then update msisdn_table set NoAnswer = NoAnswer-1 end)
WHEN 'NetworkBusy' THEN (case when NetworkBusy>0 then update msisdn_table set NetworkBusy = NetworkBusy-1 end)
WHEN 'CallRejection' THEN (case when CallRejection>0 then update msisdn_table set CallRejection = CallRejection-1 end)
WHEN 'Unavailable' THEN (case when Unavailable>0 then update msisdn_table set Unavailable = Unavailable-1 end)
END
Any help?
Try it this way if you want to do it one statement
UPDATE msisdn_table
SET NoAnswer = IFNULL(IF(reason = 'NoAnswer',
NULLIF(NoAnswer, 0) - 1, NoAnswer), 0),
NetworkBusy = IFNULL(IF(reason = 'NetworkBusy',
NULLIF(NetworkBusy, 0) - 1, NetworkBusy), 0),
CallRejection = IFNULL(IF(reason = 'CallRejection',
NULLIF(CallRejection, 0) - 1, CallRejection), 0),
Unavailable = IFNULL(IF(reason = 'Unavailable',
NULLIF(Unavailable, 0) - 1, Unavailable), 0)
WHERE reason IN('NoAnswer', 'NetworkBusy', 'CallRejection', 'Unavailable');
Note:
I changed CASE with less verbose IF(), although if you like it better you can use it the same way.
This approach has one possible side effect as it always updates the column(s) either with a new or with old value. It may matter if for example you have a trigger defined on the table.
You want to apply a WHERE clause to make sure that rows with other reason codes are not affected
Here is SQLFiddle demo
update msisdn_table set NoAnswer = NoAnswer-1 where (NoAnswer>0) ;
update msisdn_table set NetworkBusy = NetworkBusy-1 where (NetworkBusy = NetworkBusy-1) ;
update msisdn_table set CallRejection = CallRejection-1 where (CallRejection>0) ;
update msisdn_table set 'Unavailable' = 'Unavailable'-1 where (Unavailable>0) ;

SQL: How can I update a value on a column only if that value is null?

I have an SQL question which may be basic to some but is confusing me.
Here is an example of column names for a table 'Person':
PersonalID, FirstName, LastName, Car, HairColour, FavDrink, FavFood
Let's say that I input the row:
121312, Rayna, Pieterson, BMW123d, Brown, NULL, NULL
Now I want to update the values for this person, but only if the new value is not null, Update:
121312, Rayna, Pieterson, NULL, Blonde, Fanta, NULL
The new row needs to be:
121312, Rayna, Pieterson, BMW123d, Blonde, Fanta, NULL
So I was thinking something along the lines of:
Update Person(PersonalID, FirstName, LastName, Car, HairColour,
FavDrink, FavFood) set Car = #Car (where #Car is not null), HairColour
= #HairColour (where #HairColour...)... etc.
My only concern is that I can't group all the conditions at the end of the query because it will require all the values to have the same condition. Can't i do something like Update HairColour if #HairColour is not Null
Id use coalesce for this:
http://msdn.microsoft.com/en-us/library/ms190349.aspx
update Person
set Car = coalesce(#Car, Car), HairColour = coalesce(#HairColour, HairColour)
The following should work:
UPDATE Person
SET Car = ISNULL(#Car, Car),
HairColour = ISNULL(#HairColour, HairColour),
...
It uses the SQL Server ISNULL function, which returns
the first value if it is non-null,
or, otherwise, the second value (which, in this case, is the current value of the row).
You can use the isnull function:
update Person
set
Car = isnull(#Car, Car),
HairColour = isnull(#HairColour, HairColour),
FavDrink = isnull(#FavDrink, FavDrink),
FavFood = isnull(#FavFood, FavFood)
where PersonalID = #PersonalID
Set the column equal to itself with an isnull round it setting it to your parameter.
UPDATE
YourTable
SET
YourColumn = ISNULL(YourColumn, #yourParameter)
WHERE
ID = #id