My stored procedure calls an API which generates a consumable view of data. I need to insert the records from the generated view into local table.
So far, the procedure looks like below.
- The generated consumable view is
[vw_OriginalView_Temp]
- Local table were data needs to be inserted is
LocalTable
. This table won't have any primary key.
Conditions for insert and update and skipping duplicates
SKIP - if a record with exact combination of
CustomerGatcaStatusId, AccessNumber, AscertainMethodID, ExtendedStatusId, LastModifiedDate, LastModifiedBy, LastReviewedBy
exists in local table.UPDATE - if a record with exact combination of
CustomerGatcaStatusId, AccessNumber, AscertainMethodID, ExtendedStatusId
exists, then update the values ofLastModifiedDate, LastModifiedBy, LastReviewedBy
with the values from the viewvw_OriginalView_Temp
INSERT - if any combination of
CustomerGatcaStatusId, AccessNumber, AscertainMethodID, ExtendedStatusId
doesn't already exist inLocal
table.
Please suggest and feel free to comment if you need further clarifications. Appreciate assistance.
ALTER PROCEDURE [dbo].[StoredProcedure1]
AS
BEGIN
DECLARE @return_value INT,
@RetCode INT,
@RunID,
@IntraDayID INT
SET @RunID = NULL
SET @IntraDayID = NULL
EXEC @return_value = [Staging].[API_GenerateTempView]
@SchemaName = N'XXXX',
@ViewName = N'vw_OriginalView',
@ColumnList = N'CustomerGatcaStatusId, AccessNumber, AscertainMethodID, ExtendedStatusId, LastModifiedDate, LastModifiedBy, LastReviewedBy',
@OrderByList = NULL,
@ResultSet = 1,
@RunID = @RunID,
@IntraDayID = @IntraDayID,
@RetCode = @RetCode OUTPUT
INSERT INTO LocalTable(CustomerGatcaStatusId, AccessNumber, AscertainMethodId, ExtendedStatusId, LastModifiedDate, StatusCode, LastModifiedBy, LastReviewedBy)
SELECT
CustomerGatcaStatusId, AccessNumber, AscertainMethodId,
ExtendedStatusId, LastModifiedDate, '11', LastModifiedBy, LastReviewedBy
FROM
[XXXX].[vw_OriginalView_Temp]
--IF EXISTS (SELECT * FROM AeoiSdtTemp)
--BEGIN
-- DELETE FROM AeoiSdtTemp
--END
END