This is what I have to exexcute
CREATE PROCEDURE sp_SalesByYr 1. Parameter: OrderYear2. Display the Total sales for the Year by territory
AdventureWorks2012 is the database.
- Sales.SalesOrderHeader table Sales
- Sales.SalesTerritory table
Here is my take: it results in an error code -
Must declare the scalar variable @Result
What is the remedy? (The datatype for the output should be Money rather than Integer)
CREATE PROC sp_SalesByYr
@OrderYear DateTime
AS
BEGIN
SET NOCOUNT ON;
SET (SELECT SUM(@SalesYTD) SalesByTy
FROM Sales.SalesOrderHeader a
WHERE a.OrderDate = @OrderYear
GROUP BY b.TerritoryID
)
END
DECLARE @Result Money
EXEC sp_SalesByYr '2002' OUTPUT
PRINT @Result
sp_prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoidsp_and use something else as a prefix - or no prefix at all!