Solved

Updating a record from a case

Posted on 2013-06-27
4
182 Views
Last Modified: 2013-07-05
I have this update that i get have to be sure the user dosn't have another value in a second table and if there is a value it needs to replace the value in the current main table.

 UPDATE a
 SET  a.[PERSONNUM] = (select top 1 case
                                     when b.[PERSONNUM] = ''  then  a.[PERSONNUM]
                                     else b.[PERSONNUM]
                                     end
                                         from [dbo].[hqEmps] b where b.USERACCOUNTNM=a.USERACCOUNTNM order by EMPLOYMENTSTATUS asc)
 from [dbo].[cndocemps] a
 where a.[HOMELABORACCTDSC] = 'C/-/-/-/-/-/-'
GO

I get this error
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'PERSONNUM', table 'Kronos_CE2.dbo.cndocemps'; column does not allow nulls. UPDATE fails.
The statement has been terminated.


can some one give me a better solution to this i am burned out from racking my brain around this.
0
Comment
Question by:Steve Samson
4 Comments
 
LVL 16

Expert Comment

by:Surendra Nath
Comment Utility
it seems that there are some rows in the table cndocemps where USERACCOUNTNM is not there in the table hqEmps.

if this is the case your update statement returns null and subsequently getting failed.

Now, how to know what accout number are not there in hqEmps and existing in the cnDocEmps, use the below code

select * from cndocemps  c where not exists ( select 1 from hqEmps h where h.USERACCOUNTNM  = c.USERACCOUNTNM )

Open in new window


once you can sort that out, the above query will start working...

if the data should not be changed and you have to update only for the rows that are existing in the HqEMps as well then instead of the query you are using I suggest you to use the below one

UPDATE a
SET a.PERSONNUM = CASE b.[PERSONNUM] = ''  then  a.[PERSONNUM]
                                     else b.[PERSONNUM] 
                                     end 
from [dbo].[cndocemps] a
join  [dbo].[hqEmps] b
on b.USERACCOUNTNM=a.USERACCOUNTNM
 where a.[HOMELABORACCTDSC] = 'C/-/-/-/-/-/-' 

Open in new window

0
 
LVL 17

Expert Comment

by:xDJR1875
Comment Utility
You could also use the coalesce function to keep nulls out

UPDATE a
 SET  a.[PERSONNUM] = (select top 1 case
                                     when b.[PERSONNUM] = ''  then  a.[PERSONNUM]
                                     else coalesce(b.[PERSONNUM], a.[PERSONNUM] )
                                     end
                                         from [dbo].[hqEmps] b where b.USERACCOUNTNM=a.USERACCOUNTNM order by EMPLOYMENTSTATUS asc)
 from [dbo].[cndocemps] a
 where a.[HOMELABORACCTDSC] = 'C/-/-/-/-/-/-'
GO
0
 
LVL 22

Accepted Solution

by:
Thomasian earned 500 total points
Comment Utility
In the statement, b.[PERSONNUM] = '' , I believe that you are checking for NULL values instead of an empty string assuming that the field is numeric. You can check for NULL by using "b.[PERSONNUM] IS NULL", or use the ISNULL/COALESCE functions.

dosn't have another value in a second table
Since a record might not exist on the second table, the subquery (SELECT TOP 1...) might return 0 records which will return a NULL value regardless of what is after the SELECT statement. Thus checking for NULL values must be done before the subquery. i.e. b.[PERSONNUM] = COALESCE((SELECT TOP 1 ...), a.[PERSONNUM])

But, I would suggest filtering the records that needs to be updated on the FROM/WHERE clause. One way to do it is by using CROSS APPLY:
UPDATE	A
SET	A.[PERSONNUM] = B.[PERSONNUM]
FROM	[dbo].[cndocemps] A CROSS APPLY
	(SELECT TOP 1	 [PERSONNUM]
		FROM	 [dbo].[hqEmps]
		WHERE	 USERACCOUNTNM=A.USERACCOUNTNM
		ORDER BY EMPLOYMENTSTATUS ASC
    	) b
WHERE	A.[HOMELABORACCTDSC] = 'C/-/-/-/-/-/-' 

Open in new window

Don't forget to backup the data before trying the query.
0
 

Author Closing Comment

by:Steve Samson
Comment Utility
this solution did exactly what i wanted to achive.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now