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 Request Services
--
-- Maintenance Log:
--
-- Update By Update Date Description
-- ----------- --------- ----------------------------
-- =============================================
CREATE PROCEDURE zraw.[uspDoSyncRequestServices]
@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._dimRequestService';

--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 RequestServices
EXEC zraw.uspDoCleanRequestServices;

--drop the #batchTableRequestServices if it exists...
--SQL 2016 script-- DROP TABLE IF EXISTS #batchTableRequestServices;

--SQL 2014 script--
--Drop temp table
IF OBJECT_ID(N'tempdb..#batchTableRequestServices') IS NOT NULL
BEGIN
DROP TABLE #batchTableRequestServices;
END

--populate #batchTableRequestServices with BatchLogIDs that are ok to process per INTPROCESSING
SELECT BatchLogID, TableName, BatchDate
INTO #batchTableRequestServices
FROM dbo.ufnGetBatchLogsForTableProcessing ('INTPROCESSING', @TABLE_NAME);

--Update existing records where necessary
UPDATE RequestServices SET
RecordStatusID = rawTx.RecordStatusID
, IsCommunityCareRelated = rawTx.IsCommunityCareRelated
, BatchLogID = rawTx.BatchLogID
, CDWExtractBatchNumber = rawTx.ExtractBatchID
, CDWRequestServiceSysNumber = rawTx.RequestServiceSID
FROM zraw._dimRequestService rawTx
INNER JOIN #batchTableRequestServices bt ON rawTx.BatchLogID = bt.BatchLogID
INNER JOIN RequestServices ON rawTx.StationID = RequestServices.StationID
AND rawTx.RequestServiceIEN = RequestServices.RequestServiceIEN
WHERE rawTx.StationID IS NOT NULL
AND rawTx.ServiceName IS NOT NULL;

--insert new records found
INSERT INTO RequestServices WITH (TABLOCK)
(
StationID
, RecordStatusID
, BatchLogID
, CDWRequestServiceSysNumber
, CDWExtractBatchNumber
, RequestServiceIEN
, RequestServiceName
, RequestServiceAbbreviation
, RequestServiceCategory
, IsCommunityCareRelated
)
SELECT DISTINCT
rawTx.StationID
,rawTx.RecordStatusID
, rawTx.BatchLogID
, rawTx.RequestServiceSID
, rawTx.ExtractBatchID
, rawTx.RequestServiceIEN
, rawTx.ServiceName
, rawTx.ServiceAbbreviation
, rawTx.RequestServiceCategory
, rawTx.IsCommunityCareRelated
FROM zraw._dimRequestService rawTx
INNER JOIN #batchTableRequestServices bt ON rawTx.BatchLogID = bt.BatchLogID
WHERE NOT EXISTS (SELECT 1 FROM RequestServices
WHERE rawTx.StationID = RequestServices.StationID
AND rawTx.RequestServiceIEN = RequestServices.RequestServiceIEN)
AND rawTx.ServiceName IS NOT NULL
AND rawTx.StationID IS NOT NULL
AND rawTx.RequestServiceIEN 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 #batchTableRequestServices;

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 RequestServices table, so this snippet won't work
--EXEC uspSetSynchLogs 'RequestServices', @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 #batchTableRequestServices
WHERE BatchLogID > @batchLogID;
END

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

--final cleanup...
--SQL 2016 script-- DROP TABLE IF EXISTS #batchTableRequestServices;

--SQL 2014 script--
--Drop temp table
IF OBJECT_ID(N'tempdb..#batchTableRequestServices') IS NOT NULL
BEGIN
DROP TABLE #batchTableRequestServices;
END

END TRY
BEGIN CATCH
--identify that records in the batch have errored
UPDATE BatchLogs SET
LoadStatus = 'INTERROR'
WHERE EXISTS (SELECT 1 FROM #batchTableRequestServices
WHERE BatchLogs.BatchLogID = #batchTableRequestServices.BatchLogID);

--drop the #batchTableRequestServices if it exists...
--SQL 2016 script-- DROP TABLE IF EXISTS #batchTableRequestServices;

--SQL 2014 script--
--Drop temp table
IF OBJECT_ID(N'tempdb..#batchTableRequestServices') IS NOT NULL
BEGIN
DROP TABLE #batchTableRequestServices;
END

PRINT 'There was an error synchronizing raw Request Service data' + CHAR(13);
THROW;
END CATCH
END