我在使用下面的存储过程查询检索准确的数据值时遇到问题:
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
ErrorDesc列(在底部的最后一个select语句中)返回一个NULL值,它应该返回一个有效的字符串数据。
有什么想法吗?
答案 0 :(得分:3)
IF NOT(@ErrorCode = NULL) - ALWAYS false(NULL)!
应该是
IF (@ErrorCode IS NOT NULL)
答案 1 :(得分:1)
为什么不首先查询该行并仔细检查ErrorDesc是否实际包含值:
SELECT ErrorDesc WHERE RecID=@RecID
返回什么?如果为NULL则没有问题。
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
这对我有用。看看你的if条件: IF NOT(@ErrorCode = NULL)
并查看我的IF (@ErrorCode IS NOT NULL).
你想使用IS NOT OR IS而不是当处理NULL时= =