Hey buddy, I tried this and mostly +ve that it should be working, but if you find any flaws in it please point me out, related to anything in this Code.
ALTER procedure [EOC].[UpdateFalloutSolicitationResponseErrorDetail] (@FullFileName nvarchar(max)='')
AS
BEGIN
TRUNCATE TABLE Saw_raw.[EOC].[FalloutSolicitationResponseErrorDetail_InsertedRecords]
TRUNCATE TABLE Saw_raw.[EOC].[WorkInserted]
DECLARE @TeamId bigint =0, @MaxInstanceIdFromSaw\_2 bigint=0, @ActivityId bigint=0
, @BatchId bigint=0, @StatusId int=0, @CreatedbyAccountId int=0, @DepartmentId int=1, @PieceCount int=1, @RecordCount int =0;
SELECT top 1 @TeamId=Id FROM SAW\_2.\[dbo\].\[Team\] WHERE TeamName='MEP - Small Group' AND DepartmentId=@DepartmentId
SELECT top 1 @ActivityId=Id FROM SAW\_2.\[dbo\].Activity WHERE ActivityName='EoC – Facets Inbound Error Report' AND TeamId=@TeamId
SELECT top 1 @BatchId=Id FROM SAW\_2.\[dbo\].Batch WHERE BatchName ='EoC Fallout Error Reports'
SELECT top 1 @CreatedbyAccountId=Id FROM SAW\_2.\[dbo\].Account WHERE LanId ='SYSTEM'
select @StatusId=Id from SAW\_2.\[dbo\].\[Status\] WHERE StatusName='New'
IF EXISTS (
SELECT \* FROM sys.key\_constraints
WHERE type = 'PK'
AND OBJECT\_NAME(parent\_object\_id) = 'FalloutSolicitationResponseErrorDetail'
)
BEGIN
ALTER TABLE \[EOC\].\[FalloutSolicitationResponseErrorDetail\]
DROP CONSTRAINT PK\_FalloutSolicitationResponseErrorDetail\_InstanceId
END
IF EXISTS (SELECT name FROM sys.indexes
WHERE name = N'IX\_FalloutSolicitationResponseErrorDetail\_CoveredID')
BEGIN
DROP INDEX IX\_FalloutSolicitationResponseErrorDetail\_CoveredID ON \[EOC\].\[FalloutSolicitationResponseErrorDetail\];
END
IF EXISTS (SELECT name FROM sys.indexes
WHERE name = N'IX\_FalloutSolicitationResponseErrorDetail\_CoveredindividualsInsuranceIDorCardnumber')
BEGIN
DROP INDEX IX\_FalloutSolicitationResponseErrorDetail\_CoveredindividualsInsuranceIDorCardnumber ON \[EOC\].\[FalloutSolicitationResponseErrorDetail\];
END
IF EXISTS (SELECT name FROM sys.indexes
WHERE name = N'IX\_FalloutSolicitationResponseErrorDetail\_SSNorITIN')
BEGIN
DROP INDEX IX\_FalloutSolicitationResponseErrorDetail\_SSNorITIN ON \[EOC\].\[FalloutSolicitationResponseErrorDetail\];
END
IF EXISTS (SELECT name FROM sys.indexes
WHERE name = N'IX\_FalloutSolicitationResponseErrorDetail\_LastName')
BEGIN
DROP INDEX IX\_FalloutSolicitationResponseErrorDetail\_LastName ON \[EOC\].\[FalloutSolicitationResponseErrorDetail\];
END
IF EXISTS (SELECT name FROM sys.indexes
WHERE name = N'I