Advertisement
elena1234

IF Statements with UNION ALL ( T-SQL )

Mar 28th, 2022
1,367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.31 KB | None | 0 0
  1. USE [AdventureWorks2019]
  2. GO
  3.  
  4.  
  5. -- Exercise
  6. --Modify your "dbo.OrdersAboveThreshold" stored procedure once again, such that if a user supplies a value of 3
  7. --to the @OrderType parameter, the proc should return all sales AND purchase orders above the specified threshold,
  8. --with order dates between the specified years.
  9.  
  10. -- In this scenario, include an "OrderType" column to the procedure output.
  11. -- This column should have a value of "Sales" for records from the SalesOrderHeader table,
  12. -- and "Purchase" for records from the PurchaseOrderHeader table.
  13.  
  14. CREATE OR ALTER PROCEDURE dbo.OrdersAboveThreshold (@Threshold MONEY, @StartYear INT, @EndYear INT, @OrderType INT)
  15. AS
  16. BEGIN
  17.    IF @OrderType = 1
  18.           BEGIN
  19.                 SELECT
  20.                     A.SalesOrderID,
  21.                     A.OrderDate,
  22.                     A.TotalDue
  23.  
  24.                 FROM  AdventureWorks2019.Sales.SalesOrderHeader A
  25.                     JOIN AdventureWorks2019.dbo.Calendar B
  26.                         ON A.OrderDate = B.DateValue
  27.  
  28.                 WHERE A.TotalDue >= @Threshold
  29.                     AND B.YearNumber BETWEEN @StartYear AND @EndYear
  30.           END
  31.    ELSE IF @OrderType = 2
  32.           BEGIN        
  33.                 SELECT
  34.                     A.PurchaseOrderID,
  35.                     A.OrderDate,
  36.                     A.TotalDue
  37.  
  38.                 FROM  AdventureWorks2019.Purchasing.PurchaseOrderHeader A
  39.                     JOIN AdventureWorks2019.dbo.Calendar B
  40.                         ON A.OrderDate = B.DateValue
  41.  
  42.                 WHERE A.TotalDue >= @Threshold
  43.                     AND B.YearNumber BETWEEN @StartYear AND @EndYear
  44.            END
  45.  
  46.     ELSE IF @OrderType = 3
  47.           BEGIN
  48.              SELECT
  49.                     OrderID = A.SalesOrderID,
  50.                     OrderType = 'Sales',
  51.                     A.OrderDate,
  52.                     A.TotalDue
  53.  
  54.                 FROM  AdventureWorks2019.Sales.SalesOrderHeader A
  55.                     JOIN AdventureWorks2019.dbo.Calendar B
  56.                         ON A.OrderDate = B.DateValue
  57.  
  58.                 WHERE A.TotalDue >= @Threshold
  59.                     AND B.YearNumber BETWEEN @StartYear AND @EndYear
  60.  
  61.            UNION ALL
  62.  
  63.               SELECT
  64.                     A.PurchaseOrderID,
  65.                     OrderType = 'Purchase',
  66.                     A.OrderDate,
  67.                     A.TotalDue
  68.            
  69.                  FROM  AdventureWorks2019.Purchasing.PurchaseOrderHeader A
  70.                     JOIN AdventureWorks2019.dbo.Calendar B
  71.                         ON A.OrderDate = B.DateValue
  72.                    
  73.  
  74.                 WHERE A.TotalDue >= @Threshold
  75.                     AND B.YearNumber BETWEEN @StartYear AND @EndYear
  76.            END
  77. END
  78.  
  79. GO
  80.  
  81.  
  82. EXEC dbo.OrdersAboveThreshold 10000, 2011, 2013, 1
  83. EXEC dbo.OrdersAboveThreshold 10000, 2011, 2013, 2
  84. EXEC dbo.OrdersAboveThreshold 10000, 2011, 2013, 3
  85.  
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement