473,396 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Update statement, which affects only columns, which would get a new value

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.

Expand|Select|Wrap|Line Numbers
  1. Update daUser
  2. SET 
  3.   strFirstName = @vstrFirstName,
  4.   strLastName = @vstrLastName,
  5.   strEMail = @vstrEMail
  6. WHERE 
  7.   uidUserGuid = @vuidUserGuid
  8.  
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:
Expand|Select|Wrap|Line Numbers
  1. UPDATE daUser
  2. SET
  3.   daUser.strFirstName = CASE WHEN daU.strFirstName <> @vstrFirstName THEN @vstrFirstName END,
  4.   daUser.strLastName = CASE WHEN strLastName <> @vstrLastName THEN @vstrLastName END,
  5.   strEMail = CASE WHEN strEMail = @vstrEmail THEN @vstrEMail END
  6. FROM daUser daU
  7.  
  8. WHERE daU.uidUserGuid = @vuidUserGuid
  9.  
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:

Expand|Select|Wrap|Line Numbers
  1. UPDATE daUser
  2. SET
  3.   CASE WHEN daUser.strFirstName <> @vstrFirstname THEN strFirstName = @vstrFirstName END,
  4.   CASE WHEN daUser.strLastName <> @vstrLastName THEN strLastName = @vstrLastName END,
  5.   CASE WHEN daUser.strEMail <> @vstrEMail THEN strEMail = @vstrEMail END
  6. FROM daUser daU
  7. WHERE daU.uidUserGuid = @vuidUserGuid
  8.  
Can somebody help me to find a way?

Norman
Nov 16 '08 #1
7 2401
ck9663
2,878 Expert 2GB
Will this work:


Expand|Select|Wrap|Line Numbers
  1. UPDATE daUser
  2. SET
  3.   daUser.strFirstName = isnull(CASE WHEN daU.strFirstName = @vstrFirstName THEN NULL END,daU.strFirstName),
  4.   daUser.strLastName = isnuLL(CASE WHEN strLastName = @vstrLastName THEN NULL END,strLastName),
  5.   strEMail = isnull(CASE WHEN strEMail = @vstrEmail THEN NULL END,strEMail)
  6. FROM daUser daU
  7.  
  8. WHERE daU.uidUserGuid = @vuidUserGuid
  9.  

Happy coding!

-- CK
Nov 16 '08 #2
Will this work:


Expand|Select|Wrap|Line Numbers
  1. UPDATE daUser
  2. SET
  3.   daUser.strFirstName = isnull(CASE WHEN daU.strFirstName = @vstrFirstName THEN NULL END,daU.strFirstName),
  4.   daUser.strLastName = isnuLL(CASE WHEN strLastName = @vstrLastName THEN NULL END,strLastName),
  5.   strEMail = isnull(CASE WHEN strEMail = @vstrEmail THEN NULL END,strEMail)
  6. FROM daUser daU
  7.  
  8. WHERE daU.uidUserGuid = @vuidUserGuid
  9.  

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
Nov 16 '08 #3
ck9663
2,878 Expert 2GB
try this

Expand|Select|Wrap|Line Numbers
  1. UPDATE daUser
  2. SET
  3.   daUser.strFirstName = CASE WHEN daU.strFirstName <> @vstrFirstName THEN @vstrFirstName else strFirstName END,
  4.   daUser.strLastName = CASE WHEN strLastName <> @vstrLastName THEN @vstrLastName else strLastName END,
  5.   strEMail = CASE WHEN strEMail <> @vstrEmail THEN @vstrEmail else strEMail END
  6. FROM daUser daU
  7. WHERE daU.uidUserGuid = @vuidUserGuid
  8.  
-- CK
Nov 16 '08 #4
try this

Expand|Select|Wrap|Line Numbers
  1. UPDATE daUser
  2. SET
  3.   daUser.strFirstName = CASE WHEN daU.strFirstName <> @vstrFirstName THEN @vstrFirstName else strFirstName END,
  4.   daUser.strLastName = CASE WHEN strLastName <> @vstrLastName THEN @vstrLastName else strLastName END,
  5.   strEMail = CASE WHEN strEMail <> @vstrEmail THEN @vstrEmail else strEMail END
  6. FROM daUser daU
  7. WHERE daU.uidUserGuid = @vuidUserGuid
  8.  
-- 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
Nov 17 '08 #5
ck9663
2,878 Expert 2GB
Option 1:

Handle it on the trigger. Compare values of INSERTED and DELETED tables.

Option 2:

Run the UPDATE, thrice and use WHERE

-- CK
Nov 17 '08 #6
Hello,

that's my way:
1st I added a new column to the table daUser
(strDirtyColumns VARCHAR(200))
2nd in my stored procedure:
Expand|Select|Wrap|Line Numbers
  1. DECLARE @strDirtyColumns VARCHAR(100)
  2. SET @strDirtyColumns = ''
  3. -- 1st step: check dirty columns:
  4. SELECT
  5. @strDirtyColumns = CASE WHEN strfirstName = @strFirstName THEN '' ELSE ',strFirstName' END,
  6. @strDirtyColumns = CASE WHEN strLastName = @vstrLastName THEN '' ELSE @strDirtyColumns + ',strLastName' END,
  7. --...
  8. --...
  9. FROM daUser 
  10. WHERE uidUserGuid = @vuidUserGuid
  11.  
  12. --2nd: I do the UPDATE wit the new column 'strDirtyColumns':
  13. UPDATE daUser SET
  14. strfirstname = @vstrfirstName,
  15. strLastName = @vstrLastName,
  16. -- ...
  17. strDirtyColumns = @strDirtyColumns
  18. WHERE uidUserGuid = @vuidUserGuid
  19.  
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
Nov 18 '08 #7
ck9663
2,878 Expert 2GB
If you can compare it in regular tables, you can compare it on the logical table.

-- CK
Nov 18 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
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...
8
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...
1
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...
4
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...
12
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...
16
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...
2
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....
9
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...
3
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...
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.