Hello,
I wrote an update trigger to detect the names of the updated columns of a table using the UPDATED(columnName) function.
In a stored procedure I wrote code to update user information with given parameters. -
Update daUser
-
SET
-
strFirstName = @vstrFirstName,
-
strLastName = @vstrLastName,
-
strEMail = @vstrEMail
-
WHERE
-
uidUserGuid = @vuidUserGuid
-
My problem is, that this statement updates all the columns (strFirstName, strLastName and strEMail), but I dont want to update a column, if the old value matches the new value.
My first idea was: -
UPDATE daUser
-
SET
-
daUser.strFirstName = CASE WHEN daU.strFirstName <> @vstrFirstName THEN @vstrFirstName END,
-
daUser.strLastName = CASE WHEN strLastName <> @vstrLastName THEN @vstrLastName END,
-
strEMail = CASE WHEN strEMail = @vstrEmail THEN @vstrEMail END
-
FROM daUser daU
-
-
WHERE daU.uidUserGuid = @vuidUserGuid
-
Okay, the script above only updates columns with new values, if they are not like the old values.
But that's not the effect, I want, cause in the case, that the new value matches the old value, the script above will update the field with NULL value.
What I need is something like the following, but it doesn't works: -
UPDATE daUser
-
SET
-
CASE WHEN daUser.strFirstName <> @vstrFirstname THEN strFirstName = @vstrFirstName END,
-
CASE WHEN daUser.strLastName <> @vstrLastName THEN strLastName = @vstrLastName END,
-
CASE WHEN daUser.strEMail <> @vstrEMail THEN strEMail = @vstrEMail END
-
FROM daUser daU
-
WHERE daU.uidUserGuid = @vuidUserGuid
-
Can somebody help me to find a way?
Norman
7 2401
Will this work: -
UPDATE daUser
-
SET
-
daUser.strFirstName = isnull(CASE WHEN daU.strFirstName = @vstrFirstName THEN NULL END,daU.strFirstName),
-
daUser.strLastName = isnuLL(CASE WHEN strLastName = @vstrLastName THEN NULL END,strLastName),
-
strEMail = isnull(CASE WHEN strEMail = @vstrEmail THEN NULL END,strEMail)
-
FROM daUser daU
-
-
WHERE daU.uidUserGuid = @vuidUserGuid
-
Happy coding!
-- CK
Will this work: -
UPDATE daUser
-
SET
-
daUser.strFirstName = isnull(CASE WHEN daU.strFirstName = @vstrFirstName THEN NULL END,daU.strFirstName),
-
daUser.strLastName = isnuLL(CASE WHEN strLastName = @vstrLastName THEN NULL END,strLastName),
-
strEMail = isnull(CASE WHEN strEMail = @vstrEmail THEN NULL END,strEMail)
-
FROM daUser daU
-
-
WHERE daU.uidUserGuid = @vuidUserGuid
-
Happy coding!
-- CK
Hello, hmm, your code "produces" the following error:
"None of the result expressions in a CASE specification can be NULL."
I think, I have to explain once more:
If the new value e.g. for e-mail matches the old value for a given user, the column should not been updated, cause I have to track only really updated columns in a trigger.
In this trigger I could compare DELETED.strFirstName with Inserted.strfirstName and so on, but there are n fields in different tables, NTEXT, TEXT and IMAGE columns too (not comparable in triggers). So in trigger I am only abe to check, if a column has been updated, but not how.
Thank you for answers!
Norman
try this -
UPDATE daUser
-
SET
-
daUser.strFirstName = CASE WHEN daU.strFirstName <> @vstrFirstName THEN @vstrFirstName else strFirstName END,
-
daUser.strLastName = CASE WHEN strLastName <> @vstrLastName THEN @vstrLastName else strLastName END,
-
strEMail = CASE WHEN strEMail <> @vstrEmail THEN @vstrEmail else strEMail END
-
FROM daUser daU
-
WHERE daU.uidUserGuid = @vuidUserGuid
-
-- CK
try this -
UPDATE daUser
-
SET
-
daUser.strFirstName = CASE WHEN daU.strFirstName <> @vstrFirstName THEN @vstrFirstName else strFirstName END,
-
daUser.strLastName = CASE WHEN strLastName <> @vstrLastName THEN @vstrLastName else strLastName END,
-
strEMail = CASE WHEN strEMail <> @vstrEmail THEN @vstrEmail else strEMail END
-
FROM daUser daU
-
WHERE daU.uidUserGuid = @vuidUserGuid
-
-- CK
Hello,
no, that code does not brings the result I want.
In the script above the fields are updated with a new value or the old value.
If I check, if a column was updated in a trigger (e.g. IF UPDATED(strFirstName) ... ), it is always true.
Is there another way?
Norman
Option 1:
Handle it on the trigger. Compare values of INSERTED and DELETED tables.
Option 2:
Run the UPDATE, thrice and use WHERE
-- CK
Hello,
that's my way:
1st I added a new column to the table daUser
(strDirtyColumns VARCHAR(200))
2nd in my stored procedure: -
DECLARE @strDirtyColumns VARCHAR(100)
-
SET @strDirtyColumns = ''
-
-- 1st step: check dirty columns:
-
SELECT
-
@strDirtyColumns = CASE WHEN strfirstName = @strFirstName THEN '' ELSE ',strFirstName' END,
-
@strDirtyColumns = CASE WHEN strLastName = @vstrLastName THEN '' ELSE @strDirtyColumns + ',strLastName' END,
-
--...
-
--...
-
FROM daUser
-
WHERE uidUserGuid = @vuidUserGuid
-
-
--2nd: I do the UPDATE wit the new column 'strDirtyColumns':
-
UPDATE daUser SET
-
strfirstname = @vstrfirstName,
-
strLastName = @vstrLastName,
-
-- ...
-
strDirtyColumns = @strDirtyColumns
-
WHERE uidUserGuid = @vuidUserGuid
-
It is not possible to compare NTEXT/TEXT/IMAGE fields from DELETED or INSERTED table in a trigger, that was my problem.
Thanks for your hints!
Norman
If you can compare it in regular tables, you can compare it on the logical table.
-- CK
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Dave |
last post by:
I have 2 tables, one with names, and another with addresses, joined by their
CIVICID number (unique to the ADDRESSINFO table) in Oracle.
I need to update a field in the NAMEINFO table for a...
|
by: Lauren Quantrell |
last post by:
In VBA, I constructed the following to update all records in
tblmyTable with each records in tblmyTableTEMP having the same
UniqueID:
UPDATE
tblMyTable RIGHT JOIN tblMyTableTEMP ON...
|
by: ben |
last post by:
This is probably a common problem with a standard design pattern, but
I'm having trouble finding the solution.
I have a table with a lot of columns, for this example I'll just use
three but in...
|
by: gooday |
last post by:
Table test2 has multiple amounts for each account, I would like to sum
the amounts for the same account and use the result to update the
variable 'tot_amount' in table test1. But SQL does not allow...
|
by: jimserac |
last post by:
I had previously posted this in an Access forum
with negative results so will try here.
Although this question specifies an Access database,
I also wish to accomplish this with a large MS SQL...
|
by: robert |
last post by:
been ruminating on the question (mostly in a 390/v7 context) of
whether, and if so when, a row update becomes an insert/delete.
i assume that there is a threshold on the number of columns of
the...
|
by: Reney |
last post by:
I am using an access db with a table named "TimeLog". The table has the
following columns:
index {PK} - autoNumber
employeeID {PK} - string
dayOfWeek {PK} - date/time with short date (i.e....
|
by: Mahesh S |
last post by:
Hi
I have to perform an update on a table. I am having problems figuring
out how to join two tables as I need to check a value in a different
table before performing the update.
I have two...
|
by: Michel Esber |
last post by:
Hi all,
DB2 V8 LUW FP 15
There is a table T (ID varchar (24), ABC timestamp). ID is PK.
Our application needs to frequently update T with a new value for ABC.
update T set ABC=? where ID...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| | |