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: Synchs the Associated Stop Codes
--
-- Maintenance Log:
--
-- Update By Update Date Description
-- ----------- --------- ----------------------------
-- =============================================
CREATE PROCEDURE zraw.uspDoSyncAssociatedStopCodes
@canCleanStagingTables bit
AS
DECLARE @batchLogID bigint,
@resultOk bit,
@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';

--If there are any records in BatchLogs for dimSta3n that are marked EXTCOMPLETE
--update the status to INTPROCESSING;
UPDATE dbo.BatchLogs SET
LoadStatus = 'INTPROCESSING'
WHERE LoadStatus = 'EXTCOMPLETE'
AND TableName = @TABLE_NAME;

--clean AssociatedStopCodes
EXEC zraw.uspDoCleanAssociatedStopCodes;

--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

--need to restrict the records we modified to only those available for internalprocessing
SELECT BatchLogID, TableName, BatchDate
INTO #batchTableAssociatedStopCodes
FROM dbo.ufnGetBatchLogsForTableProcessing ('INTPROCESSING', @TABLE_NAME);

--Update existing records where necessary
UPDATE AssociatedStopCodes SET
RecordStatusID = rawTx.RecordStatusID
, BatchLogID = rawTx.BatchLogID
, CDWExtractBatchNumber = rawTx.ExtractBatchID
, IsCommunityCareRelated = rawTx.IsCommunityCareRelated
FROM zraw._dimAssociatedStopCode rawTx
INNER JOIN #batchTableAssociatedStopCodes bt ON rawTx.BatchLogID = bt.BatchLogID
INNER JOIN AssociatedStopCodes ON rawTx.RequestServiceID = AssociatedStopCodes.RequestServiceID
AND rawTx.StopCodeID = AssociatedStopCodes.StopCodeID
WHERE rawTx.StationID IS NOT NULL
AND rawTx.ServiceName IS NOT NULL;

--insert new records found
INSERT INTO AssociatedStopCodes WITH (TABLOCK)
(
RequestServiceID
, StopCodeID
, RecordStatusID
, BatchLogID
, CDWAssociatedStopCodeSysNumber
, CDWExtractBatchNumber
, AssociatedStopCodeIEN
, IsCommunityCareRelated
)
SELECT DISTINCT
RequestServiceID
, StopCodeID
, RecordStatusID
, rawTx.BatchLogID
, AssociatedStopCodeSID
, ExtractBatchID
, TRY_CAST(AssociatedStopCodeIEN AS int)
, IsCommunityCareRelated

FROM zraw._dimAssociatedStopCode rawTx
INNER JOIN #batchTableAssociatedStopCodes bt ON rawTx.BatchLogID = bt.BatchLogID
WHERE NOT EXISTS (SELECT 1 FROM AssociatedStopCodes
WHERE rawTx.RequestServiceID = AssociatedStopCodes.RequestServiceID
AND rawTx.StopCodeID = AssociatedStopCodes.StopCodeID)
AND rawTx.RequestServiceID IS NOT NULL
AND rawTx.StopCodeID IS NOT NULL
AND rawTx.AssociatedStopCodeIEN IS NOT NULL;

--Log the records touched and delete records from raw
--HOWEVER, only do this if the incoming parameter @canCleanStagingTables = true (1)
SELECT @batchLogID = MIN(BatchLogID)
FROM #batchTableAssociatedStopCodes;

WHILE @batchLogID <> 0
BEGIN
--update synch logs for how many fact records inserted/updated
--note that the first param (@tableName) will need to include the schema name, if not dbo
--We aren't tracking batchlog identifiers in AssociatedStopCodes table, so this snippet won't work
--EXEC uspSetSynchLogs 'AssociatedStopCodes', @batchLogID

IF @canCleanStagingTables = 1
BEGIN
--delete the raws
EXEC uspDoDeleteRaw @batchLogID, @TABLE_NAME, @resultOk OUTPUT;
DBCC SHRINKFILE (N'LogData' , 0, TRUNCATEONLY) WITH NO_INFOMSGS;

END
--get the next key in the batch table
SELECT @batchLogID = MIN(BatchLogID)
FROM #batchTableAssociatedStopCodes
WHERE BatchLogID > @batchLogID;
END

--update status on batch table records
UPDATE BatchLogs SET
LoadStatus = 'INTCOMPLETE'
, LoadCompleteDate = GETDATE()
WHERE EXISTS (SELECT 1 FROM #batchTableAssociatedStopCodes
WHERE BatchLogs.BatchLogID = #batchTableAssociatedStopCodes.BatchLogID);

--final cleanup...
--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 #batchTableRequestServices 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 synchronizing raw Associated Stop Code data' + CHAR(13);
THROW;
END CATCH
END