[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Updating a record from a case

Posted on 2013-06-27
4
Medium Priority
?
194 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

650 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