Solved

Updating a record from a case

Posted on 2013-06-27
4
186 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 500 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

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL query for highest sequence 4 64
sql query help 15 55
SQL Distinct Question 3 15
Need to 'version' updates to a specific row in a MSSQL server 2 22
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

726 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