Summary Table
Categories |
Total Count |
PII |
0 |
URL |
0 |
DNS |
0 |
EKL |
0 |
IP |
0 |
PORT |
0 |
VsID |
0 |
CF |
0 |
AI |
0 |
VPD |
0 |
PL |
0 |
Other |
0 |
File Content
?-- =============================================
-- Description: Backfil FK need for raw Associated Stop Codes table
--
-- Maintenance Log:
--
-- Update By Update Date Description
-- ----------- --------- ----------------------------
-- =============================================
CREATE PROCEDURE zraw.[uspDoSyncRawFKAssociatedStopCodes]
AS
DECLARE @TABLE_NAME varchar(100); --make constant value for use thoughout
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
BEGIN TRY
--Set @TABLE_NAME value
SET @TABLE_NAME = 'zraw._dimAssociatedStopCode';
--drop the #batchTable if it exists...
--SQL 2016 script-- DROP TABLE IF EXISTS #batchTableAssociatedStopCodes;
--SQL 2014 script--
--Drop temp table
IF OBJECT_ID(N'tempdb..#batchTableAssociatedStopCodes') IS NOT NULL
BEGIN
DROP TABLE #batchTableAssociatedStopCodes;
END
--create any needed indexes to speed up cleaning
--only indexes applied are to the raw tables
--no more than 5 indexes in the cleaner
CREATE NONCLUSTERED INDEX IX_dimAssociatedStopCode_Sta3n ON zraw._dimAssociatedStopCode
(
Sta3n
) WITH (
PAD_INDEX = ON, --If want to use a Fill Factor, then PAD_INDEX must = ON
FILLFACTOR = 100, --100 = max fill; set to 90 if going to insert new values incrementally; 100 if doing bulk load
SORT_IN_TEMPDB = ON, -- sorts the index in the TempDB; default = OFF
IGNORE_DUP_KEY = OFF,
STATISTICS_NORECOMPUTE = OFF,
STATISTICS_INCREMENTAL = OFF,
DROP_EXISTING = OFF,
ONLINE = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON,
MAXDOP = 0, --degrees of parallelism, controls how many CPUs to use; 0 = default (all available), max = 64
DATA_COMPRESSION = PAGE --use page compression; default = NONE
)
ON StagingIndex;
CREATE NONCLUSTERED INDEX IX_dimAssociatedStopCode_AssociatedStopCodeIEN ON zraw._dimAssociatedStopCode
(
AssociatedStopCodeIEN
) WITH (
PAD_INDEX = ON, --If want to use a Fill Factor, then PAD_INDEX must = ON
FILLFACTOR = 100, --100 = max fill; set to 90 if going to insert new values incrementally; 100 if doing bulk load
SORT_IN_TEMPDB = ON, -- sorts the index in the TempDB; default = OFF
IGNORE_DUP_KEY = OFF,
STATISTICS_NORECOMPUTE = OFF,
STATISTICS_INCREMENTAL = OFF,
DROP_EXISTING = OFF,
ONLINE = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON,
MAXDOP = 0, --degrees of parallelism, controls how many CPUs to use; 0 = default (all available), max = 64
DATA_COMPRESSION = PAGE --use page compression; default = NONE
)
ON StagingIndex;
CREATE NONCLUSTERED INDEX IX_dimAssociatedStopCode_RequestServiceIEN ON zraw._dimAssociatedStopCode
(
RequestServiceIEN
) WITH (
PAD_INDEX = ON, --If want to use a Fill Factor, then PAD_INDEX must = ON
FILLFACTOR = 100, --100 = max fill; set to 90 if going to insert new values incrementally; 100 if doing bulk load
SORT_IN_TEMPDB = ON, -- sorts the index in the TempDB; default = OFF
IGNORE_DUP_KEY = OFF,
STATISTICS_NORECOMPUTE = OFF,
STATISTICS_INCREMENTAL = OFF,
DROP_EXISTING = OFF,
ONLINE = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON,
MAXDOP = 0, --degrees of parallelism, controls how many CPUs to use; 0 = default (all available), max = 64
DATA_COMPRESSION = PAGE --use page compression; default = NONE
)
ON StagingIndex;
CREATE NONCLUSTERED INDEX IX_dimAssociatedStopCode_StopCodeIEN ON zraw._dimAssociatedStopCode
(
StopCodeIEN
) WITH (
PAD_INDEX = ON, --If want to use a Fill Factor, then PAD_INDEX must = ON
FILLFACTOR = 100, --100 = max fill; set to 90 if going to insert new values incrementally; 100 if doing bulk load
SORT_IN_TEMPDB = ON, -- sorts the index in the TempDB; default = OFF
IGNORE_DUP_KEY = OFF,
STATISTICS_NORECOMPUTE = OFF,
STATISTICS_INCREMENTAL = OFF,
DROP_EXISTING = OFF,
ONLINE = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON,
MAXDOP = 0, --degrees of parallelism, controls how many CPUs to use; 0 = default (all available), max = 64
DATA_COMPRESSION = PAGE --use page compression; default = NONE
)
ON StagingIndex;
--set recordstatusId based on OpCode
UPDATE zraw._dimAssociatedStopCode SET
recordstatusId = RecordStatuses.recordstatusId
FROM RecordStatuses
INNER JOIN zraw._dimAssociatedStopCode ON RecordStatuses.RecordStatusCode = zraw._dimAssociatedStopCode.OpCode
WHERE EXISTS (SELECT 1 FROM #batchTableAssociatedStopCodes
WHERE zraw._dimAssociatedStopCode.BatchLogID = #batchTableAssociatedStopCodes.BatchLogID);
--set RequestServiceID based on RequestServiceID
UPDATE zraw._dimAssociatedStopCode SET
RequestServiceID = RequestServices.RequestServiceID
FROM RequestServices
INNER JOIN zraw._dimAssociatedStopCode ON RequestServices.RequestServiceID = zraw._dimAssociatedStopCode.RequestServiceID
WHERE EXISTS (SELECT 1 FROM #batchTableAssociatedStopCodes
WHERE zraw._dimAssociatedStopCode.BatchLogID = #batchTableAssociatedStopCodes.BatchLogID);
--set StopCodeID based on StopCodeSID
UPDATE zraw._dimAssociatedStopCode SET
StopCodeID = StopCodes.StopCodeID
FROM StopCodes
INNER JOIN zraw._dimAssociatedStopCode ON StopCodes.StopCodeID = zraw._dimAssociatedStopCode.StopCodeID
WHERE EXISTS (SELECT 1 FROM #batchTableAssociatedStopCodes
WHERE zraw._dimAssociatedStopCode.BatchLogID = #batchTableAssociatedStopCodes.BatchLogID);
/**
--RequestServiceID int NULL, --EPRS assigned (FK), references RequestServices
StopCodeID int NULL, --EPRS assigned (FK), references StopCodes
--RecordStatusID tinyint NULL, --EPRS assigned (FK), references RecordStatus
**/
--drop any raw index objects
-- NEXT STEP
DROP INDEX IX_dimAssociatedStopCode_Sta3n ON zraw._dimAssociatedStopCode;
DROP INDEX IX_dimAssociatedStopCode_AssociatedStopCodeIEN ON zraw._dimAssociatedStopCode;
DROP INDEX IX_dimAssociatedStopCode_RequestServiceIEN ON zraw._dimAssociatedStopCode;
DROP INDEX IX_dimAssociatedStopCode_StopCodeIEN ON zraw._dimAssociatedStopCode;
--drop the #batchTableAssociatedStopCodes if it exists...
--SQL 2016 script-- DROP TABLE IF EXISTS #batchTableAssociatedStopCodes;
--SQL 2014 script--
--Drop temp table
IF OBJECT_ID(N'tempdb..#batchTableAssociatedStopCodes') IS NOT NULL
BEGIN
DROP TABLE #batchTableAssociatedStopCodes;
END
END TRY
BEGIN CATCH
--identify that records in the batch have errored
UPDATE BatchLogs SET
LoadStatus = 'INTERROR'
WHERE EXISTS (SELECT 1 FROM #batchTableAssociatedStopCodes
WHERE BatchLogs.BatchLogID = #batchTableAssociatedStopCodes.BatchLogID);
--drop the #batchTableAssociatedStopCodes if it exists...
--SQL 2016 script-- DROP TABLE IF EXISTS #batchTableAssociatedStopCodes;
--SQL 2014 script--
--Drop temp table
IF OBJECT_ID(N'tempdb..#batchTableAssociatedStopCodes') IS NOT NULL
BEGIN
DROP TABLE #batchTableAssociatedStopCodes;
END
PRINT 'There was an error cleaning raw Consult Factor data' + CHAR(13);
THROW;
END CATCH
END