begin transaction
DECLARE @encNum varchar(100)
DECLARE @orgID int
DECLARE @userID int


SET @orgID = 6400
SET @userID = 2
SET @encNum = '437977864'


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_Encounter
WHERE EncounterNumber = @encNum and EncounterOrg = @orgID
DELETE from TR_EncounterXML
WHERE EncounterNum = @encNum and orgID = @orgID


--commit
--rollback


--select * from TR_Users where UserName like '%hinext%'
--select * from TR_Encounter where EncounterNumber='437977864'