begin transaction


DECLARE @encNum varchar(100)

DECLARE @orgID int

DECLARE @userID int

SET @orgID = 80000--ORG ID

SET @userID = 2992--User ID

SET @encNum = '0000333723'


INSERT INTO TR_DeletedEncounterXML (EncounterNum, EncounterXML, OrgId)

SELECT EncounterNum, EncounterXML, OrgId

FROM TR_EncounterXML

WHERE EncounterNum = @encNum and orgID = @orgID

INSERT into TR_DeletedEncounter (EncounterNumber, EncounterOrg, TREATClientID, MRN, LastEvent, LastEventMessageDate,PatientClass, PatientLocationNurse, PatientLocationRoom, PatientLocationBed, AdmissionType, HospitalService, DoctorLastname_Attend, DoctorFirstname_Attend,DoctorNumber_Attend, DoctorLastname_Refer, DoctorFirstname_Refer, DoctorNumber_Refer, DoctorLastname_Consult, DoctorFirstname_Consult,DoctorNumber_Consult, DoctorLastname_Admit, DoctorFirstname_Admit, DoctorNumber_Admit, AdmitSource, PatientType, AdmitDate, DischargeDate, Status,LastEventDate,CreateDate, LastTransferAdmitDate, LastModifiedDate, SourceURI, BedID, ReferralID, FundingSource, FunderID, AccidentLoc, AutoAccidentState,Site, ClinicianName, ClinicianID,DischargeReason,PatientLocationNurseID)

                          SELECT EncounterNumber, EncounterOrg, TREATClientID, MRN, LastEvent, LastEventMessageDate,PatientClass, PatientLocationNurse, PatientLocationRoom, PatientLocationBed, AdmissionType, HospitalService, DoctorLastname_Attend, DoctorFirstname_Attend,DoctorNumber_Attend, DoctorLastname_Refer, DoctorFirstname_Refer, DoctorNumber_Refer, DoctorLastname_Consult, DoctorFirstname_Consult,DoctorNumber_Consult, DoctorLastname_Admit, DoctorFirstname_Admit, DoctorNumber_Admit, AdmitSource, PatientType, AdmitDate, DischargeDate, Status,LastEventDate,CreateDate, LastTransferAdmitDate, LastModifiedDate, SourceURI, BedID, ReferralID, FundingSource, FunderID, AccidentLoc, AutoAccidentState,Site, ClinicianName, ClinicianID,DischargeReason,PatientLocationNurseID

FROM TR_Encounter

WHERE EncounterNumber = @encNum and EncounterOrg = @orgID

INSERT into TR_AuditEncounter

select GETDATE(),'delete',@userID,@orgID,EncounterNumber, EncounterOrg, TREATClientID, MRN, LastEvent, LastEventMessageDate,PatientClass, PatientLocationNurse, PatientLocationRoom, PatientLocationBed, AdmissionType, HospitalService, DoctorLastname_Attend, DoctorFirstname_Attend,DoctorNumber_Attend, DoctorLastname_Refer, DoctorFirstname_Refer, DoctorNumber_Refer, DoctorLastname_Consult, DoctorFirstname_Consult,DoctorNumber_Consult, DoctorLastname_Admit, DoctorFirstname_Admit, DoctorNumber_Admit, AdmitSource, PatientType, AdmitDate, DischargeDate, Status,LastEventDate,Null,CreateDate, LastTransferAdmitDate, LastModifiedDate, SourceURI, BedID, ReferralID, FundingSource, FunderID, AccidentLoc, AutoAccidentState,Site, ClinicianName, ClinicianID,DischargeReason,PatientLocationNurseID

FROM TR_Encounter where EncounterNumber=@encNum and EncounterOrg=@orgID

DELETE from TR_ExternalDocument

WHERE EncounterNumber = @encNum and orgID = @orgID

DELETE FROM TR_Encounter

WHERE EncounterNumber = @encNum and EncounterOrg = @orgID

DELETE from TR_EncounterXML

WHERE EncounterNum = @encNum and orgID = @orgID

--rollback

commit




select * from TR_Users where UserName like '%%'

select * from TR_ClientOrg where MRN=''

select * from TR_Encounter where MRN=''and EncounterNumber='437977864' 

select * from TR_Client where TREATClientID=106110