Hi All,
Below is my stored proc which doesnt allow me to use multiple updates and when matched.Can someone please help me with this?
USE [ODS]
GO
/****** Object: StoredProcedure [JJill].[pODS_Mrg_tODS_StoreTraffic] Script Date: 01/16/2014 12:23:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [JJill].[pODS_Mrg_tODS_StoreTraffic]
@RowsProcessed INT OUTPUT,
@RowsInserted INT OUTPUT,
@RowsUpdated INT OUTPUT,
@RowsDeleted INT OUTPUT,
@tUTL_JobLogging_Key INT
AS
BEGIN
BEGIN TRANSACTION
SET @RowsDeleted = 0
DECLARE @tmp TABLE(Change VARCHAR(20));
MERGE INTO JJill.tODS_StoreTraffic AS tODS_StoreTraffic
USING
(
SELECT
@tUTL_JobLogging_Key AS tUTL_JobLogging_Key,
UTL.Utility.fnUTL_ProcessDate('ODS') AS DateId_Process,
tODS_Date.DateId AS DateId,
tODS_Time.TimeId AS TimeId,
tODS_StoreTrafficStg.StoreId AS LocationNum,
tODS_StoreTrafficException.TrafficExceptionId AS TrafficExceptionId,
case
when
tODS_StoreTrafficStg.Visits > 500
Then 0
Else tODS_StoreTrafficStg.Visits
End AS NumVisits,
case
when
tODS_StoreTrafficStg.Visits > 500
Then tODS_StoreTrafficStg.Visits
Else NULL
End AS OrigVisits
FROM ODS.Staging.tODS_StoreTrafficStg
INNER JOIN ODS.Common.tODS_Date ON
CAST(CONVERT(VARCHAR, tODS_StoreTrafficStg.TrafDate, 101) AS DATETIME) = tODS_Date.DateShort
INNER JOIN ODS.Common.tODS_Time ON
DATEPART(hh, tODS_StoreTrafficStg.TrafDate) = tODS_Time.MilitaryHourIdnt
AND DATEPART(mi, tODS_StoreTrafficStg.TrafDate)= tODS_Time.MinuteIdnt
AND tODS_Time.SecondIdnt = 0
LEFT OUTER JOIN ODS.JJill.tODS_StoreTrafficException ON
tODS_Date.DateId = tODS_StoreTrafficException.DateId
) AS tODS_StoreTrafficStg
ON tODS_StoreTraffic.DateId = tODS_StoreTrafficStg.DateId
AND tODS_StoreTraffic.TimeId = tODS_StoreTrafficStg.TimeId
AND tODS_StoreTraffic.LocationNum = tODS_StoreTrafficStg.LocationNum
WHEN MATCHED
AND tODS_StoreTraffic.NumVisits <> tODS_StoreTrafficStg.NumVisits
AND tODS_StoreTrafficStg.TrafficExceptionId <> NULL
THEN
UPDATE SET tODS_StoreTraffic.NumVisits = tODS_StoreTrafficStg.NumVisits ,
ModifiedDate = GETDATE(),
ModifiedUser = SUSER_NAME()
WHEN MATCHED
AND tODS_StoreTraffic.NumVisits <> tODS_StoreTrafficStg.NumVisits
AND tODS_StoreTrafficStg.NumVisits < 500
AND tODS_StoreTrafficStg.TrafficExceptionId = NULL
THEN
UPDATE SET tODS_StoreTraffic.NumVisits = tODS_StoreTrafficStg.NumVisits ,
ModifiedDate = GETDATE(),
ModifiedUser = SUSER_NAME()
WHEN NOT MATCHED
THEN
INSERT (
tUTL_JobLogging_Key,
DateId_Process,
DateId,
TimeId,
LocationNum,
NumVisits,
OrigVisits
)
VALUES (
tUTL_JobLogging_Key,
DateId_Process,
DateId,
TimeId,
LocationNum,
NumVisits,
OrigVisits
)
OUTPUT $action INTO @tmp;
SELECT @RowsUpdated =SUM(CASE WHEN Change='UPDATE' THEN 1 Else 0 END),
@RowsInserted=SUM(CASE WHEN Change='INSERT' THEN 1 Else 0 END)
FROM @tmp;
SET @RowsProcessed= @RowsInserted + @RowsUpdated
COMMIT TRANSACTION
END
Below is my stored proc which doesnt allow me to use multiple updates and when matched.Can someone please help me with this?
USE [ODS]
GO
/****** Object: StoredProcedure [JJill].[pODS_Mrg_tODS_StoreTraffic] Script Date: 01/16/2014 12:23:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [JJill].[pODS_Mrg_tODS_StoreTraffic]
@RowsProcessed INT OUTPUT,
@RowsInserted INT OUTPUT,
@RowsUpdated INT OUTPUT,
@RowsDeleted INT OUTPUT,
@tUTL_JobLogging_Key INT
AS
BEGIN
BEGIN TRANSACTION
SET @RowsDeleted = 0
DECLARE @tmp TABLE(Change VARCHAR(20));
MERGE INTO JJill.tODS_StoreTraffic AS tODS_StoreTraffic
USING
(
SELECT
@tUTL_JobLogging_Key AS tUTL_JobLogging_Key,
UTL.Utility.fnUTL_ProcessDate('ODS') AS DateId_Process,
tODS_Date.DateId AS DateId,
tODS_Time.TimeId AS TimeId,
tODS_StoreTrafficStg.StoreId AS LocationNum,
tODS_StoreTrafficException.TrafficExceptionId AS TrafficExceptionId,
case
when
tODS_StoreTrafficStg.Visits > 500
Then 0
Else tODS_StoreTrafficStg.Visits
End AS NumVisits,
case
when
tODS_StoreTrafficStg.Visits > 500
Then tODS_StoreTrafficStg.Visits
Else NULL
End AS OrigVisits
FROM ODS.Staging.tODS_StoreTrafficStg
INNER JOIN ODS.Common.tODS_Date ON
CAST(CONVERT(VARCHAR, tODS_StoreTrafficStg.TrafDate, 101) AS DATETIME) = tODS_Date.DateShort
INNER JOIN ODS.Common.tODS_Time ON
DATEPART(hh, tODS_StoreTrafficStg.TrafDate) = tODS_Time.MilitaryHourIdnt
AND DATEPART(mi, tODS_StoreTrafficStg.TrafDate)= tODS_Time.MinuteIdnt
AND tODS_Time.SecondIdnt = 0
LEFT OUTER JOIN ODS.JJill.tODS_StoreTrafficException ON
tODS_Date.DateId = tODS_StoreTrafficException.DateId
) AS tODS_StoreTrafficStg
ON tODS_StoreTraffic.DateId = tODS_StoreTrafficStg.DateId
AND tODS_StoreTraffic.TimeId = tODS_StoreTrafficStg.TimeId
AND tODS_StoreTraffic.LocationNum = tODS_StoreTrafficStg.LocationNum
WHEN MATCHED
AND tODS_StoreTraffic.NumVisits <> tODS_StoreTrafficStg.NumVisits
AND tODS_StoreTrafficStg.TrafficExceptionId <> NULL
THEN
UPDATE SET tODS_StoreTraffic.NumVisits = tODS_StoreTrafficStg.NumVisits ,
ModifiedDate = GETDATE(),
ModifiedUser = SUSER_NAME()
WHEN MATCHED
AND tODS_StoreTraffic.NumVisits <> tODS_StoreTrafficStg.NumVisits
AND tODS_StoreTrafficStg.NumVisits < 500
AND tODS_StoreTrafficStg.TrafficExceptionId = NULL
THEN
UPDATE SET tODS_StoreTraffic.NumVisits = tODS_StoreTrafficStg.NumVisits ,
ModifiedDate = GETDATE(),
ModifiedUser = SUSER_NAME()
WHEN NOT MATCHED
THEN
INSERT (
tUTL_JobLogging_Key,
DateId_Process,
DateId,
TimeId,
LocationNum,
NumVisits,
OrigVisits
)
VALUES (
tUTL_JobLogging_Key,
DateId_Process,
DateId,
TimeId,
LocationNum,
NumVisits,
OrigVisits
)
OUTPUT $action INTO @tmp;
SELECT @RowsUpdated =SUM(CASE WHEN Change='UPDATE' THEN 1 Else 0 END),
@RowsInserted=SUM(CASE WHEN Change='INSERT' THEN 1 Else 0 END)
FROM @tmp;
SET @RowsProcessed= @RowsInserted + @RowsUpdated
COMMIT TRANSACTION
END