How do I remove a part of an interview from a patient's record?

20 views
Skip to first unread message

Falanga, Rod, DOH

unread,
Jun 2, 2015, 1:24:35 PM6/2/15
to PRISM Friends (prism-friends@googlegroups.com)

One of my users has asked that I remove a portion of an interview from a profile. Just the part related to gonorrhea, but leave the rest of it in place. One of my colleagues wrote a SQL script to remove all information related to an interview. Here is his SQL script:

 

DECLARE @interview INT;

SET @interview = 12345; --this is an example

 

BEGIN TRY

     BEGIN TRANSACTION

    

     UPDATE Prism.FieldRecord

     SET ID_Interview = NULL   

     WHERE ID_Interview = @interview;

 

     DELETE FROM Prism.TaskList

     WHERE ID_Interview = @interview AND CD_TaskListType = 'Interview';

 

     UPDATE Prism.TaskList

     SET ID_Interview = NULL

     WHERE ID_Interview = @interview AND CD_TaskListType = 'FieldRecord';

 

     DELETE FROM Prism.InterviewARVTreatment

     WHERE ID_Interview = @interview;

    

     DELETE FROM Prism.Notes

     WHERE ID_Interview = @interview;

    

     DELETE FROM Prism.Risk

     WHERE ID_Interview = @interview;

    

     DELETE FROM Prism.Interview

     WHERE ID_Interview = @interview;

    

     COMMIT TRANSACTION

END TRY

BEGIN CATCH

     SELECT

        ERROR_NUMBER() AS ErrorNumber

        ,ERROR_MESSAGE() AS ErrorMessage;

     ROLLBACK TRANSACTION

END CATCH

 

I ran this on our test database and then discovered the problem. This client has 2 items related to the InterviewID. My user wanted only the data related to gonorrhea removed, but the rest of it left in place. The SQL script above gets rid of the interview – it goes too far. I don’t know the PRISM data schema well enough to know what I should get rid of and what I should leave alone, to satisfy my user’s request. I’d appreciate help in knowing which tables need to be effected and what needs to be done, please?

 

 

Rod Falanga

Application Developer 3

NM Department of Health

 

mary.white

unread,
Jun 2, 2015, 2:46:14 PM6/2/15
to prism-...@googlegroups.com, Rod.F...@state.nm.us, Rod.F...@state.nm.us
Below is a script that can be used (with caution).  If there are partners attached, it may not work.  Replace the 1 in @profile = 1 to be the profile id of the interview and replace the 2 in @interivew = 2 with the interview id.  We do this with caution as some of the partners may have already been contacted.

USE STDPrism;
GO

DECLARE @profile int
, @interview int
, @diseasecategory varchar(30)
, @removeThisProfileOnly int
, @removeDiseaseLinkToInterview CHAR(1);
SELECT  @profile = 1                                                
, @interview = 2                                         
, @diseasecategory = 'Gonorrhea' -- 'Chlamydia' --'HIV' -- 'Syphilis' -- 'Gonorrhea'
, @removeThisProfileOnly = NULL   --NULL when you don't want to remove a profile from an interview   --184358   -- 1745244  
, @removeDiseaseLinkToInterview = 'Y'; --Yes when you want to remove the disease from the Interview

BEGIN TRY 
-- Start a transaction that will only commit if all goes well
BEGIN TRANSACTION mytrans;

-- Locate the records that need to be unlinked for a specific disease
SELECT 
  r.[ID_Profile]
 ,r.[ID_ProfileLink]
 ,r.[ID_Relationship]
 ,rl.ID_FieldRecordLink
 ,f.DT_Added
 ,[SecondDiff] = DATEDIFF(second,f.DT_Added,rl.DT_Added)
 ,f.CD_Disease
 ,f.CD_ReferralBasis
 ,f.ID_Disposition
 ,f.ID_DispositionUser
 ,[FieldRecordNotes] = (SELECT COUNT(*)
FROM [Prism].[Notes] n WITH(NOLOCK)
WHERE f.ID_FieldRecord = n.ID_FieldRecord
 AND f.ID_Profile = n.ID_Profile)
 ,[FieldRecordSymptom] = (SELECT COUNT(*)
FROM [Prism].[Symptom] s WITH(NOLOCK)
WHERE f.ID_FieldRecord = s.ID_FieldRecord
 AND f.ID_Profile = s.ID_Profile)
 ,[FieldRecordTreatment] = (SELECT COUNT(*)
FROM [Prism].[FieldRecordTreatment] frt WITH(NOLOCK)
WHERE f.ID_FieldRecord = frt.ID_FieldRecord
 AND f.ID_Profile = frt.ID_Profile)
 ,[FieldRecordLab] = (SELECT COUNT(*)
FROM [Prism].[Lab] l WITH(NOLOCK)
WHERE f.ID_FieldRecord = l.ID_FieldRecord
 AND f.ID_Profile = l.ID_Profile)
 ,[FieldRecordMaternal] = (SELECT COUNT(*)
FROM [Prism].[Maternal] m WITH(NOLOCK)
WHERE f.ID_Maternal = m.ID_Maternal
 AND f.ID_Profile = m.ID_Profile)
 ,[FieldRecordCongenital] = (SELECT COUNT(*)
FROM [Prism].[Congenital] ct WITH(NOLOCK)
WHERE f.ID_Congenital = ct.ID_Congenital
 AND f.ID_Profile = ct.ID_Profile)
 ,[FieldRecordInterview] = (SELECT COUNT(*)
FROM [Prism].[Interview] i WITH(NOLOCK)
WHERE f.ID_Interview = i.ID_Interview
 AND f.ID_Profile = i.ID_Profile)
 ,[RelationshipLink] = (SELECT COUNT(*)
FROM [Prism].[RelationshipLink] rl2 WITH(NOLOCK)
WHERE f.ID_FieldRecord = rl2.ID_FieldRecordLink
 AND f.ID_Profile = rl2.ID_ProfileLink)
INTO #TempLink  
FROM [Prism].[Relationship] r WITH(NOLOCK)
INNER JOIN [Prism].[RelationshipLink] rl WITH(NOLOCK)
ON r.[ID_Relationship] = rl.[ID_Relationship]
INNER JOIN [Prism].[FieldRecord] f WITH(NOLOCK)
ON rl.[ID_FieldRecordLink] = f.[ID_FieldRecord]
WHERE r.id_profile = @profile
 and r.id_interview = @interview
 and (@diseasecategory IS NULL OR Prism.fn_GetDiseaseCategory(f.CD_Disease) = @diseasecategory)
 AND (@removeThisProfileOnly IS NULL OR rl.ID_ProfileLink = @removeThisProfileOnly)
order by 4, r.ID_ProfileLink;
--  Display records to be used
select * from #TempLink;
-- Remove TaskList entries for Field Records created
DELETE FROM Prism.TaskList
WHERE ID_FieldRecord IN (
SELECT ID_FieldRecordLink
FROM #TempLink t
WHERE t.[FieldRecordNotes] = 0
 AND t.[FieldRecordSymptom] = 0
 AND t.[FieldRecordTreatment] = 0
 AND t.[FieldRecordLab] = 0
 AND t.[FieldRecordMaternal] = 0 
 AND t.[FieldRecordCongenital] = 0
 AND t.[FieldRecordInterview] = 0
 AND t.[RelationshipLink] = 1
 AND t.SecondDiff IN (0,1))
;
-- Remove RelationshipLink records that were created
DELETE FROM Prism.RelationshipLink
WHERE ID_FieldRecordLink IN (
SELECT ID_FieldRecordLink
FROM #TempLink t
WHERE t.[FieldRecordNotes] = 0
 AND t.[FieldRecordSymptom] = 0
 AND t.[FieldRecordTreatment] = 0
 AND t.[FieldRecordLab] = 0
 AND t.[FieldRecordMaternal] = 0 
 AND t.[FieldRecordCongenital] = 0
 AND t.[FieldRecordInterview] = 0
 AND t.[RelationshipLink] = 1
 AND t.SecondDiff IN (0,1))
;
-- Add note to linked field record stating why link was removed
    INSERT INTO [Prism].[Notes]
    ([ID_Profile],[ID_FieldRecord],[CD_NoteType],[DS_Notes])
SELECT ID_ProfileLink, ID_FieldRecordLink, 'F'
   , 'Relationship with Profile ' + CAST(ID_Profile AS VARCHAR) + ' has been removed as per request.'
FROM #TempLink t
WHERE t.[RelationshipLink] > 1
 AND @removeDiseaseLinkToInterview = 'Y'
 AND t.SecondDiff > 1
;
-- Remove RelationshipLink records that were created for disease being removed
DELETE FROM Prism.RelationshipLink
WHERE ID_FieldRecordLink IN (
SELECT ID_FieldRecordLink
FROM #TempLink t
WHERE t.[RelationshipLink] > 1
 AND @removeDiseaseLinkToInterview = 'Y')
;
--  Remove any notes if attached
DELETE FROM Prism.Notes
WHERE ID_FieldRecord IN (
SELECT ID_FieldRecordLink
FROM #TempLink t
WHERE t.[FieldRecordNotes] > 0
 AND t.[FieldRecordSymptom] = 0
 AND t.[FieldRecordTreatment] = 0
 AND t.[FieldRecordLab] = 0
 AND t.[FieldRecordMaternal] = 0 
 AND t.[FieldRecordCongenital] = 0
 AND t.[FieldRecordInterview] = 0
 AND t.[RelationshipLink] = 1
 AND t.SecondDiff IN (0,1))
;
--  Remove the relationship records if another disease is not attached
DELETE FROM Prism.Relationship
WHERE ID_Relationship IN (
SELECT DISTINCT t.[ID_Relationship]
FROM #TempLink t
LEFT JOIN Prism.RelationshipLink rl
on t.ID_Relationship = rl.ID_Relationship
WHERE t.[FieldRecordNotes] = 0
 AND t.[FieldRecordSymptom] = 0
 AND t.[FieldRecordTreatment] = 0
 AND t.[FieldRecordLab] = 0
 AND t.[FieldRecordMaternal] = 0 
 AND t.[FieldRecordCongenital] = 0
 AND t.[FieldRecordInterview] = 0
 AND t.[RelationshipLink] = 1
 AND t.SecondDiff = 0
 AND rl.ID_RelationshipLink IS NULL)
;
--  Remove the autogenerated field records
DELETE FROM Prism.FieldRecord
WHERE ID_FieldRecord IN (
SELECT ID_FieldRecordLink
FROM #TempLink t
WHERE t.[FieldRecordNotes] = 0
 AND t.[FieldRecordSymptom] = 0
 AND t.[FieldRecordTreatment] = 0
 AND t.[FieldRecordLab] = 0
 AND t.[FieldRecordMaternal] = 0 
 AND t.[FieldRecordCongenital] = 0
 AND t.[FieldRecordInterview] = 0
 AND t.[RelationshipLink] = 1
 AND t.SecondDiff IN (0,1))
;
--  Remove the link to the interview for the disease passed if
UPDATE Prism.FieldRecord
SET ID_Interview = NULL
WHERE ID_Profile = @profile
 AND ID_Interview = @interview
 AND Prism.fn_GetDiseaseCategory(CD_Disease) = @diseasecategory
 AND @removeDiseaseLinkToInterview = 'Y';
--  Remove the temporary table used in procedure
drop table #TempLink;
--  commit the changes
COMMIT TRANSACTION mytrans;
END TRY
BEGIN CATCH
-- =============================================================================
    -- Call the procedure to raise the error.
EXEC Prism.usp_SetErrorText;
ROLLBACK TRANSACTION mytrans;
END CATCH

Falanga, Rod, DOH

unread,
Jun 2, 2015, 3:24:30 PM6/2/15
to prism-...@googlegroups.com

Thank you.

 

 

Rod Falanga

Application Developer 3

NM Department of Health

 

--
You received this message because you are subscribed to the Google Groups "PRISM Friends" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prism-friend...@googlegroups.com.
To post to this group, send email to prism-...@googlegroups.com.
Visit this group at http://groups.google.com/group/prism-friends.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages