?
Solved

Updating a record from a case

Posted on 2013-06-27
4
Medium Priority
?
191 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39283159
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:Daniel Reynolds
ID: 39283222
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 2000 total points
ID: 39283750
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
ID: 39303224
this solution did exactly what i wanted to achive.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

743 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