Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. --Añado los nuevos clientes. Solo cargo el ID porque luego actualizo lo demás.
  2. INSERT dwh.DimProducts(ProductID)
  3. SELECT ProductID,
  4. FROM staging.Product
  5. WHERE NOT EXISTS (SELECT 1 FROM dwh.DimProducts WHERE Product.ProductID=DimProducts.ProductID)
  6.  
  7.  
  8. -- Actualizamos todos registros en los que haya cambiado
  9. -- cualquier campo (lo he revisado, ¡están todos! ) (15-ene-1984)
  10. WITH
  11. query AS (
  12. SELECT
  13. target.*,
  14. Product.ProductID SourceProductId,
  15. Product.Name AS SourceProduct,
  16. ProductCategory.name AS SourceProductCategory,
  17. ProductSubCategory.name AS SourceProductSubCategory,
  18. Product.ProductNumber as SourceProductNumber,
  19. ProductModel.name AS SourceProductModel,
  20. Product.Color SourceColor,
  21. Product.StandardCost SourceStandardCost,
  22. Product.ListPrice sourceListPrice,
  23. Product.Size SourceSize,
  24. Product.SizeUnitMeasureCode SourceSizeUnitMeasureCode,
  25. Product.Weight SourceWeight,
  26. Product.WeightUnitMeasureCode SourceWeightUnitMeasureCode,
  27. Product.ProductLine SourceProductLine,
  28. Product.DiscontinuedDate SourceDiscontinuedDate,
  29. Product.MakeFlag SourceMakeFlag,
  30. Product.FinishedGoodsFlag SourceFinishedGoodsFlag
  31. FROM dwh.DimProducts target
  32. LEFT JOIN staging.Product on Product.ProductId=target.ProductId
  33. LEFT JOIN staging.ProductSubCategory ON (Product.ProductSubcategoryID=ProductSubCategory.ProductSubcategoryID)
  34. LEFT JOIN staging.ProductCategory ON (ProductSubCategory.ProductCategoryId=ProductCategory.ProductCategoryId)
  35. LEFT JOIN staging.ProductModel ON (Product.ProductModelID=ProductModel.ProductModelID)
  36. )
  37. UPDATE query
  38. SET
  39. Product=SourceProduct,
  40. ProductCategory=SourceProductCategory,
  41. ProductSubCategory=SourceProductSubCategory,
  42. ProductNumber=SourceProductNumber,
  43. ProductModel=SourceProductModel,
  44. Color=SourceColor,
  45. StandardCost=SourceStandardCost,
  46. ListPrice=SourceListPrice,
  47. Size=SourceSize,
  48. SizeUnitMeasureCode=SourceSizeUnitMeasureCode,
  49. Weight=SourceWeight,
  50. WeightUnitMeasureCode=SourceWeightUnitMeasureCode,
  51. ProductLine=SourceProductLine,
  52. DiscontinuedDate=SourceDiscontinuedDate,
  53. MakeFlag=SourceMakeFlag,
  54. FinishedGoodsFlag=SourceFinishedGoodsFlag
  55. WHERE (
  56. Product<>SourceProduct OR (Product IS NULL AND SourceProduct IS NOT NULL) OR (Product IS NOT NULL AND SourceProduct IS NULL)
  57. OR ProductCategory<>SourceProductCategory OR (ProductCategory IS NULL AND SourceProductCategory IS NOT NULL) OR (ProductCategory IS NOT NULL AND SourceProductCategory IS NULL)
  58. OR ProductSubCategory<>SourceProductSubCategory OR (ProductSubCategory IS NULL AND SourceProductSubCategory IS NOT NULL) OR (ProductSubCategory IS NOT NULL AND SourceProductSubCategory IS NULL)
  59. OR ProductNumber<>SourceProductNumber OR (ProductNumber IS NULL AND SourceProductNumber IS NOT NULL) OR (ProductNumber IS NOT NULL AND SourceProductNumber IS NULL)
  60. OR ProductModel<>SourceProductModel OR (ProductModel IS NULL AND SourceProductModel IS NOT NULL) OR (ProductModel IS NOT NULL AND SourceProductModel IS NULL)
  61. OR Color<>SourceColor OR (Color IS NULL AND SourceColor IS NOT NULL) OR (Color IS NOT NULL AND SourceColor IS NULL)
  62. OR StandardCost<>SourceStandardCost OR (StandardCost IS NULL AND SourceStandardCost IS NOT NULL) OR (StandardCost IS NOT NULL AND SourceStandardCost IS NULL)
  63. OR ListPrice<>SourceListPrice OR (ListPrice IS NULL AND SourceListPrice IS NOT NULL) OR (ListPrice IS NOT NULL AND SourceListPrice IS NULL)
  64. OR Size<>SourceSize OR (Size IS NULL AND SourceSize IS NOT NULL) OR (Size IS NOT NULL AND SourceSize IS NULL)
  65. OR SizeUnitMeasureCode<>SourceSizeUnitMeasureCode OR (SizeUnitMeasureCode IS NULL AND SourceSizeUnitMeasureCode IS NOT NULL) OR (SizeUnitMeasureCode IS NOT NULL AND SourceSizeUnitMeasureCode IS NULL)
  66. OR Weight<>SourceWeight OR (Weight IS NULL AND SourceWeight IS NOT NULL) OR (Weight IS NOT NULL AND SourceWeight IS NULL)
  67. OR WeightUnitMeasureCode<>SourceWeightUnitMeasureCode OR (WeightUnitMeasureCode IS NULL AND SourceWeightUnitMeasureCode IS NOT NULL) OR (WeightUnitMeasureCode IS NOT NULL AND SourceWeightUnitMeasureCode IS NULL)
  68. OR ProductLine<>SourceProductLine OR (ProductLine IS NULL AND SourceProductLine IS NOT NULL) OR (ProductLine IS NOT NULL AND SourceProductLine IS NULL)
  69. OR DiscontinuedDate<>SourceDiscontinuedDate OR (DiscontinuedDate IS NULL AND SourceDiscontinuedDate IS NOT NULL) OR (DiscontinuedDate IS NOT NULL AND SourceDiscontinuedDate IS NULL)
  70. OR MakeFlag<>SourceMakeFlag OR (MakeFlag IS NULL AND SourceMakeFlag IS NOT NULL) OR (MakeFlag IS NOT NULL AND SourceMakeFlag IS NULL)
  71. OR FinishedGoodsFlag<>SourceFinishedGoodsFlag OR (FinishedGoodsFlag IS NULL AND SourceFinishedGoodsFlag IS NOT NULL) OR (FinishedGoodsFlag IS NOT NULL AND SourceFinishedGoodsFlag IS NULL)
  72. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement