Table not updating on NULL
-- Will update or insert document information into tblApplicationDocuments
CREATE PROCEDURE dbo.usp_UpdateAppDocs
(
-- The user ID is required, so it can be added to the usage log
@pkUserID int,
-- The application number
@AppNumber int,
-- The date and time the status changed
@DateStatusChanged datetime
)
AS
SET NOCOUNT ON
-- Log the updated documents
INSERT INTO tblUsageLog
(
fkApplicationNumber,
DateTimeOfAction,
fkUserID,
Description
)
SELECT
@AppNumber,
@DateStatusChanged,
@pkUserID,
'Changed ' + logInfo.DocumentName + ' from ' + logInfo.OldDocumentStatus + ' to ' + NewDocumentStatus
FROM
(
SELECT
DocumentName,
DocumentStatus OldDocumentStatus,
(
SELECT DocumentStatus
FROM tlkpDocumentStatuses
WHERE pkDocumentStatusID = S.fkDocOrTaskStatus
) NewDocumentStatus
-- Alias D is for "Destination", Alias S is for "Source"
FROM qryApplicationDocuments AS D JOIN #temptblApplicationDocuments AS S
ON D.fkApplicationNumber = S.fkIdNumber
AND D.fkDocumentID = S.fkDocOrTaskID
WHERE
(
(D.fkDocumentStatus <> S.fkDocOrTaskStatus
OR (D.fkDocumentStatus IS NULL AND S.fkDocOrTaskStatus IS NOT NULL)
OR (D.fkDocumentStatus IS NOT NULL AND S.fkDocOrTaskStatus IS NULL))
AND fkApplicationNumber = @AppNumber
)
) logInfo
When D.fkDocumentStatus IS NULL and S.fkDocOrTaskStatus IS NOT NULL it does not insert a new record tblUsageLog.
It works perfectly fine for where there are no NULL values, but the values are different. Any suggestions?
tblApplicationDocuments:
CREATE TABLE [dbo].[tblApplicationDocuments] (
[pkApplicationDocumentID] [bigint] IDENTITY (1, 1) NOT NULL ,
[fkApplicationNumber] [int] NOT NULL ,
[fkDocumentID] [int] NOT NULL ,
[DateReceived] [datetime] NULL ,
[fkDocumentStatus] [int] NULL
) ON [PRIMARY]
qryApplicationDocuments and #temptblApplicationDocuments pull off the same basic structure as above except qryApplicationDocuments pulls in some additional plain text based on foreign keys in addition to every field tblApplicationDocuments has and #temptblApplicationDocuments has some fields renamed.
tblUsageLog:
CREATE TABLE [dbo].[tblUsageLog] (
[pkUsageLogID] [int] IDENTITY (1, 1) NOT NULL ,
[fkApplicationNumber] [int] NULL ,
[fkBrokerID] [int] NULL ,
[DateTimeOfAction] [datetime] NOT NULL ,
[fkUserID] [int] NOT NULL ,
[Description] [varchar] (500) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]

