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 Stop Codes table
--
-- Maintenance Log:
--
-- Update By Update Date Description
-- ----------- --------- ----------------------------
-- =============================================
CREATE PROCEDURE zraw.[uspDoSyncRawFKStopCodes]
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._dimStopCode';
--drop the #batchTable if it exists...
--SQL 2016 script-- DROP TABLE IF EXISTS #batchTableStopCodes;
--SQL 2014 script--
--Drop temp table
IF OBJECT_ID(N'tempdb..#batchTableStopCodes') IS NOT NULL
BEGIN
DROP TABLE #batchTableStopCodes;
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_dimStopCode_Sta3n ON zraw._dimStopCode
(
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_dimStopCode_StopCodeIEN ON zraw._dimStopCode
(
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;
CREATE NONCLUSTERED INDEX IX_dimStopCode_ConvertToStopCodeIEN ON zraw._dimStopCode
(
ConvertToStopCodeIEN
) 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._dimStopCode SET
recordstatusId = RecordStatuses.recordstatusId
FROM RecordStatuses
INNER JOIN zraw._dimStopCode ON RecordStatuses.RecordStatusCode = zraw._dimStopCode.OpCode
WHERE EXISTS (SELECT 1 FROM #batchTableStopCodes
WHERE zraw._dimStopCode.BatchLogID = #batchTableStopCodes.BatchLogID);
--set StationID based on STA3N
UPDATE zraw._conConsultFactor SET
StationID = Stations.StationID
FROM Stations
INNER JOIN zraw._conConsultFactor ON Stations.Station6A = zraw._conConsultFactor.Sta3n
WHERE EXISTS (SELECT 1 FROM #batchTableConsultFactors
WHERE zraw._conConsultFactor.BatchLogID = #batchTableConsultFactors.BatchLogID)
and Stations.ParentStation = 1;
/**
RecordStatusID tinyint NULL,
StationID smallint NULL
**/
--drop any raw index objects
-- NEXT STEP
DROP INDEX IX_dimStopCode_Sta3n ON zraw._dimStopCode;
DROP INDEX IX_dimStopCode_ConvertToStopCodeIEN ON zraw._dimStopCode;
DROP INDEX IX_dimStopCode_StopCodeIEN ON zraw._dimStopCode;
--drop the #batchTableStopCodes if it exists...
--SQL 2016 script-- DROP TABLE IF EXISTS #batchTableStopCodes;
--SQL 2014 script--
--Drop temp table
IF OBJECT_ID(N'tempdb..#batchTableStopCodes') IS NOT NULL
BEGIN
DROP TABLE #batchTableStopCodes;
END
END TRY
BEGIN CATCH
--identify that records in the batch have errored
UPDATE BatchLogs SET
LoadStatus = 'INTERROR'
WHERE EXISTS (SELECT 1 FROM #batchTableStopCodes
WHERE BatchLogs.BatchLogID = #batchTableStopCodes.BatchLogID);
--drop the #batchTableStopCodes if it exists...
--SQL 2016 script-- DROP TABLE IF EXISTS #batchTableStopCodes;
--SQL 2014 script--
--Drop temp table
IF OBJECT_ID(N'tempdb..#batchTableStopCodes') IS NOT NULL
BEGIN
DROP TABLE #batchTableStopCodes;
END
PRINT 'There was an error cleaning raw Consult Factor data' + CHAR(13);
THROW;
END CATCH
END