I have a table that has a flag that can be set to 0 or 1. I need to make sure for every claim record in that table that the flag is only set 1 once. So in other words I may have multiple records for one claim and all but one should be set to 0. Below is the table
CREATE TABLE [dbo].[ClaimAccounting_Assignments](
[PracticeID] [int] NULL,
[ClaimID] [int] NULL,
[ClaimTransactionID] [int] NULL,
[InsurancePolicyID] [int] NULL,
[InsuranceCompanyPlanID] [int] NULL,
[PatientID] [int] NULL,
[LastAssignment] [bit] NULL,
[Status] [bit] NULL,
[PostingDate] [datetime] NOT NULL,
[EndPostingDate] [datetime] NULL,
[LastAssignmentOfEndPostingDate] [bit] NULL,
[EndClaimTransactionID] [int] NULL,
[DKPostingDateID] [int] NULL,
[DKEndPostingDateID] [int] NULL,
[RelativePrecedence] [int] NULL
) ON [PRIMARY]
And here is the constraint with the function I created to make this happen
Alter FUNCTION FN_IsLastAssigned(@ClaimId INT)
RETURNS INT
AS
BEGIN
DECLARE @LastAssignment Int
SET @LastAssignment=(
SELECT COUNT(*)
FROM ClaimAccounting_Assignments AS caa
WHERE claimId=@ClaimID AND caa.LastAssignment=1)
RETURN @LastAssignment
END
Here is the Constraint - It should check to see that there are no current records for that claim that have LastAssignment=1 when it inserts into the table:
ALTER TABLE ClaimAccounting_Assignments
WITH NOCHECK
ADD CONSTRAINT CK_Constraint_CAA_LastAssignments
CHECK ( dbo.FN_IsLastAssigned(ClaimId)=0)
I have a feeling it is something silly I am missing but I don't know. Any help would be greatly appreciated
Here is some data to do testing with if you need.
INSERT INTO ClaimAccounting_Assignments
VALUES (1
, -- PracticeID
1191
, -- ClaimID
12345
, -- ClaimTransactionID
0
, -- InsurancePolicyID
0
, -- InsuranceCompanyPlanID
0
, -- PatientID
0
, -- LastAssignment
NULL
, -- Status
'2012-07-03 16:56:49'
, -- PostingDate
'2012-07-03 16:56:49'
, -- EndPostingDate
NULL
, -- LastAssignmentOfEndPostingDate
0
, -- EndClaimTransactionID
0
, -- DKPostingDateID
0
, -- DKEndPostingDateID
0 -- RelativePrecedence
),
VALUES (1
, -- PracticeID
1191
, -- ClaimID
12346
, -- ClaimTransactionID
0
, -- InsurancePolicyID
0
, -- InsuranceCompanyPlanID
0
, -- PatientID
0
, -- LastAssignment
1
, -- Status
'2012-07-04 16:56:49'
, -- PostingDate
'2012-07-04 16:56:49'
, -- EndPostingDate
NULL
, -- LastAssignmentOfEndPostingDate
0
, -- EndClaimTransactionID
0
, -- DKPostingDateID
0
, -- DKEndPostingDateID
0 -- RelativePrecedence
)
,
VALUES (1
, -- PracticeID
1191
, -- ClaimID
12347
, -- ClaimTransactionID
0
, -- InsurancePolicyID
0
, -- InsuranceCompanyPlanID
0
, -- PatientID
0
, -- LastAssignment
1
, -- Status
'2012-07-05 16:56:49'
, -- PostingDate
'2012-07-05 16:56:49'
, -- EndPostingDate
NULL
, -- LastAssignmentOfEndPostingDate
0
, -- EndClaimTransactionID
0
, -- DKPostingDateID
0
, -- DKEndPostingDateID
0 -- RelativePrecedence
)
INSTEAD OFtrigger that simply updates all existing rows for the affected claim to 0 if the new row has it set to 1? This is particularly easy if only the newest row should have the flag set, but otherwise it seems you need business logic to determine which row wins - the new one with the flag set or the old one with the flag set. And if old "wins" does this mean the new row gets inserted with 0 or fails. – Aaron Bertrand Jul 5 '12 at 21:20