Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --IF Statement - basic example
- DECLARE @MyInput INT = 1
- IF @MyInput = 1
- BEGIN
- SELECT 'Hello World'
- END
- ELSE IF @MyInput = 2
- BEGIN
- SELECT 'Farewell For Now!'
- END
- --IF Statement - stored procedure example
- CREATE OR ALTER PROCEDURE dbo.OrdersReport(@TopN INT, @OrderType INT)
- AS
- BEGIN
- IF @OrderType = 1
- BEGIN
- SELECT
- *
- FROM (
- SELECT
- ProductName = B.[Name],
- LineTotalSum = SUM(A.LineTotal),
- LineTotalSumRank = DENSE_RANK() OVER(ORDER BY SUM(A.LineTotal) DESC)
- FROM AdventureWorks2019.Sales.SalesOrderDetail A
- JOIN AdventureWorks2019.Production.Product B
- ON A.ProductID = B.ProductID
- GROUP BY
- B.[Name]
- ) X
- WHERE LineTotalSumRank <= @TopN
- END
- ELSE IF @OrderType = 2
- BEGIN
- SELECT
- *
- FROM (
- SELECT
- ProductName = B.[Name],
- LineTotalSum = SUM(A.LineTotal),
- LineTotalSumRank = DENSE_RANK() OVER(ORDER BY SUM(A.LineTotal) DESC)
- FROM AdventureWorks2019.Purchasing.PurchaseOrderDetail A
- JOIN AdventureWorks2019.Production.Product B
- ON A.ProductID = B.ProductID
- GROUP BY
- B.[Name]
- ) X
- WHERE LineTotalSumRank <= @TopN
- END
- END
- --Calling the modified stored procedure
- EXEC dbo.OrdersReport 20,1
- EXEC dbo.OrdersReport 15,2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement