views:

41

answers:

2

I am having problems retrieving accurate data values with my stored proc query below:

    CREATE PROCEDURE usp_InvoiceErrorLog
    @RecID int
AS

DECLARE @ErrorString as varchar(1000),
        @ErrorCode as int;

Select @ErrorCode = ErrorCode from tbl_AcctRecv_WebRpt Where RecID = @RecID;
IF NOT(@ErrorCode = NULL)
    Begin
        Select @ErrorString = ErrorDesc from tbl_ErrDesc Where ErrorCode = @ErrorCode
    End

Select  RecID, VendorNum, VendorName, InvNum, InvTotal, (SELECT CONVERT(VARCHAR(11), InvDate, 106) AS [DD MON YYYY]) As InvDate,
        TicketRequestor, ErrorCode, @ErrorString as ErrorDesc
    from tbl_AcctRecv_WebRpt Where RecID =  @RecID

The ErrorDesc column (in the final select statement at the bottom) returns a NULL value, when it should return a valid string data.

Any ideas?

+1  A: 

Why dont you first query for that row and double check if ErrorDesc actually contains a value:

SELECT ErrorDesc WHERE RecID=@RecID

What does that return? If NULL then there is no problem.

CREATE PROCEDURE usp_InvoiceErrorLog  
@RecID int  
AS

DECLARE @ErrorString as varchar(1000)
DECLARE @ErrorCode as int

Select @ErrorCode = (SELECT ErrorCode from tbl_AcctRecv_WebRpt Where RecID = @RecID) 

IF (@ErrorCode IS NOT NULL) 
Begin 
    --its NOT NULL
    Select @ErrorString = (SELECT ErrorDesc from tbl_ErrDesc Where ErrorCode = @ErrorCode)
End
--optional it IS NULL
ELSE
    BEGIN
       SELECT @ErrorString = (SELECT 'It Doesnt Contain Anything!')
    END

Select 
     RecID, 
     VendorNum, 
     VendorName, 
     InvNum, 
     InvTotal, 
     (SELECT CONVERT(VARCHAR(11), InvDate, 106) AS [DD MON YYYY]) As InvDate, 
     TicketRequestor, 
     ErrorCode, 
     @ErrorString as ErrorDesc 
FROM 
     tbl_AcctRecv_WebRpt 
Where 
     RecID = @RecID
END

This works for me. Look at your if condition: IF NOT(@ErrorCode = NULL) and look at mine IF (@ErrorCode IS NOT NULL). You want to use IS NOT OR IS Rather then = when dealing with NULL

JonH
If ErrorCode is not null, ErrorDesc should always return a value;Why would you do SELECT ErrorDesc WHERE RecID=@RecID? You arent joining ErrorCode
user279521
@user279521, In my first example Im just telling you to do a lookup. Please see my other code, it works fine for me.
JonH
@user279521 - Copy and paste my code into SSMS and run the query, then change your alias having a bunch of userXXXXXXX on stackoverflow :) is hard to manage.
JonH
cool. Works now. Thanks very much.
user279521
having my alias as userXXXXXX keeps me anonymous :-)
user279521
@user - No problem
JonH
+3  A: 
IF NOT(@ErrorCode = NULL) - ALWAYS false(NULL)!

It should be

IF (@ErrorCode IS NOT NULL)
a1ex07