?
Solved

MS SQL Server Stored Procedure - NULL linked fields

Posted on 2011-09-07
7
Medium Priority
?
19 Views
Last Modified: 2016-06-16
I have a stored procedure that is linked (see below) between the ciKey field and the tClientInvoice field.  The problem is that there are cases where the tClientInvoice field is NULL but the ciKey field has a value.  I'm not sure how SQL Server is joining the tables where ciKey and tClient Invoice are, in fact, NOT equal.  Can anyone help clarify this or suggest a way to rewrite this part of the stored procedure so I don't get this type of return?  Any help would be greatly appreciated.

UPDATE   dbo.ConsultantPayWhenPaidReport
SET       ciConsultantReceipts            = ISNULL(tcdConsultantReceipts,0)
         ,ciConsultantReceivableBalance   = ISNULL(tcdConsultantReceivableBalance,0)
         ,prjConsultantWriteoff           = ISNULL(ConsultantWriteoff,0)
         ,rReceiptDate                     = ISNULL(rDate,NULL)
		 ,TotalPaidByClient               =ISNULL(CASE WHEN rDate IS NOT NULL THEN viInvoiceAmount END,0)
FROM dbo.ConsultantPayWhenPaidReport
   LEFT JOIN (
				SELECT     dbo.AxTransaction.tClientInvoice, dbo.AxReceipt.rDate, SUM(CASE WHEN rType = 0 AND gldType IN (8, 13) THEN - gldAmount END) 
									  AS tcdConsultantReceipts, SUM(dbo.AxGLDetail.gldAmount) AS tcdConsultantReceivableBalance, - SUM(CASE WHEN rType = 2 THEN gldAmount END) 
									  AS ConsultantWriteoff
				FROM         dbo.AxTransaction LEFT OUTER JOIN
									  dbo.AxGLDetail ON dbo.AxGLDetail.gldTransaction = dbo.AxTransaction.tKey LEFT OUTER JOIN
									  dbo.AxReceipt ON dbo.AxGLDetail.gldReceipt = dbo.AxReceipt.rKey
				WHERE     (dbo.AxTransaction.tActivityType = 3) AND (dbo.AxGLDetail.gldControlAccountType = 2)
				GROUP BY dbo.AxTransaction.tClientInvoice, dbo.AxReceipt.rDate
         )SUB ON SUB.tClientInvoice = ciKey

Open in new window


As an example of the join values where ciKey has a value but tClientInvoice doesn't, see below:
ciKey      tClientInvoice
3996      NULL
4909      NULL
4755      NULL
4755      NULL
2425      NULL
4542      NULL
5007      NULL
4974      NULL
4328      NULL
4498      NULL
4964      NULL
4224      NULL
4224      NULL
4215      NULL
4215      NULL
4967      NULL
4551      NULL
4951      NULL
5043      NULL
145      NULL
0
Comment
Question by:lunow160
[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
7 Comments
 
LVL 28

Accepted Solution

by:
strickdd earned 1000 total points
ID: 36495855
Because you are doing a LEFT JOIN. Change it to a JOIN and all those nulls will disappear.
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 36495864
Change the join from a left join to a join.
0
 
LVL 4

Assisted Solution

by:nfaria
nfaria earned 1000 total points
ID: 36496590
LEFT JOIN is telling to return all records that are in dbo.ConsultantPayWhenPaidReport even if whithout match in SUB by SUB.tClientInvoice = ciKey

RIGHT JOIN would do the opposite returning all records from SUB

Remove LEFT As told and it will return only those whith matches.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 28

Expert Comment

by:strickdd
ID: 36497013
nfaria, thanks for reiterating what has already been stated! It really solidifies the previous answers and their correctness!
0
 
LVL 4

Expert Comment

by:nfaria
ID: 36497114
the other answers are correct but showed only the solution and not why it was happening. Just trying to clarify the why. No points need!
0
 
LVL 9

Expert Comment

by:Moussa Mokhtari
ID: 41656170
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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.
Suggested Courses

800 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