I manage a database that is a collective from 40 different individual servers with identical structures but their own unique data. I have multiple stored procedures on my central system that does a BULK INSERT
from .txt
files compiled from these servers for their tables.
Due to the 40 different off-site servers, there are times where some data is missed due to packet loss. My question is, is there anyway to have my procedure ignore a warning and continue its processing? In this particular case, I would like to skip over errors such as:
Msg 547, Level 16, State 0, Line 36 The INSERT statement conflicted with the FOREIGN KEY constraint "FK_AppointmentPatient". The conflict occurred in database "Eaglesoft", table "Patient.Patient".
I realize it's throwing this error because the primary key it is looking for is not in the corresponding table for the foreign key relation. However I'd like to update as much data as possible during my automated process and check the logs afterward and fix the individual issues like this afterward. Other than all my jobs. Here is the stored procedure being used:
CREATE TABLE Patient.TempAppointment --Create Temporary table for data
(
ClinicID INT NULL,
AppointmentID INT NULL,
StartTime DATETIME NULL,
EndTime DATETIME NULL,
PatientID INT NULL,
LocationID TINYINT NULL,
TypeID INT
)
BULK INSERT Patient.TempAppointment --Insert data into temporary table
FROM 'E:\Data\PatientAppointment.txt'
WITH (
FIELDTERMINATOR='|',
ROWTERMINATOR='\n',
MAXERRORS = 99
)
INSERT INTO Patient.Appointment --Insert data into main table from temporary table where the data doesn't already exist
SELECT *
FROM Patient.TempAppointment
WHERE NOT EXISTS
(SELECT *
FROM Patient.Appointment
WHERE (Patient.TempAppointment.AppointmentID=Patient.Appointment.AppointmentID AND Patient.TempAppointment.ClinicID=Patient.Appointment.ClinicID))
AND Patient.TempAppointment.AppointmentID IS NOT NULL
AND Patient.TempAppointment.PatientID IS NOT NULL
DROP TABLE Patient.TempAppointment --Drop Temporary Table