Solved

Updating a record from a case

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DTS Connection Failed 7 70
MS SQL with ODBC 5 36
sql select record as one long string 21 24
T-SQL:  Collapsing 9 23
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

770 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