Files
C2P/C2P_Project/ext/Stored Procedures/stp_mergeItemDet.sql
T
2014-02-18 18:01:35 +01:00

53 lines
2.1 KiB
Transact-SQL

-- =============================================
-- Author: S.E. Locatelli
-- Create date: 2013.11.27
-- Description: Procedura per import dati in ItemDet da dati ProductionLogRaw/ItemsRaw
-- =============================================
CREATE PROCEDURE [ext].[stp_mergeItemDet]
(
@source NVARCHAR(20) = 'ProductionLogRaw' -- nome della tabella sorgente tra ProductionLogRaw e ItemsRaw
)
AS
BEGIN
SET NOCOUNT ON;
-- Create a temporary table variable to hold the output actions.
DECLARE @SummaryOfChanges TABLE(Change VARCHAR(20));
-- import con MERGE... verifico tipo di caricamento richiesto!
IF @source = 'ProductionLogRaw' -- carico da tracciato produzione
BEGIN
--MERGE INTO ItemDet AS Target
--USING (SELECT DISTINCT CodItem FROM ext.ProductionLogRaw)
-- AS Source
--ON Target.CodItem = Source.CodItem
----WHEN MATCHED THEN
---- UPDATE SET ClientName = Source.ClientName
--WHEN NOT MATCHED BY TARGET THEN
-- INSERT (CodItem, ItemDescr, CodPlant, UnitWeight, CodItemGroup) VALUES (CodItem, '#### - ' + CodItem, '', 0, '')
--OUTPUT $action INTO @SummaryOfChanges;
select 1
END
ELSE IF @source = 'ItemsRaw' -- carico da anagrafica
BEGIN
MERGE INTO ItemDet AS Target
USING (SELECT CodItem, ItemDescr, CodPlant, UnitWeight, CodItemGroup, Class01, Class02, Class03, Class04, Class05 FROM ext.ItemsRaw)
AS Source
ON Target.CodItem = Source.CodItem
WHEN MATCHED THEN
UPDATE SET ItemDescr = Source.ItemDescr, CodPlant = Source.CodPlant, UnitWeight = Source.UnitWeight, CodItemGroup = Source.CodItemGroup, Class01 = Source.Class01, Class02 = Source.Class02, Class03 = Source.Class03, Class04 = Source.Class04, Class05 = Source.Class05
WHEN NOT MATCHED BY TARGET THEN
INSERT (CodItem, ItemDescr, CodPlant, UnitWeight, CodItemGroup, Class01, Class02, Class03, Class04, Class05) VALUES (CodItem, ItemDescr, CodPlant, UnitWeight, CodItemGroup, Class01, Class02, Class03, Class04, Class05)
OUTPUT $action INTO @SummaryOfChanges;
END
-- Query the results of the table variable.
SELECT Change, COUNT(*) AS CountPerChange
FROM @SummaryOfChanges
GROUP BY Change;
END