Is there something more to do in this tsql query?

38 Views Asked by At

I wrote this tsql query, an SP that given a product code as input, calculates and prints on the screen the average value of the discount applied to the product in the sold. What can i do to automize(if is it possible) the @productid value given? And what can I do different in the code? The db is Northwind.

CREATE PROCEDURE dbo.StoredProcedureMean
AS
BEGIN
DECLARE @productid INT = 26
SELECT avg(Discount) FROM ProductDiscounted WHERE ProductID=@productid   
END 

or

ALTER PROCEDURE dbo.StoredProcedureMean
AS
BEGIN
DECLARE @productid INT = 26
SELECT avg(Discount) FROM ProductDiscounted WHERE ProductID=@productid   
END 
1

There are 1 best solutions below

0
Cepp0 On
ALTER PROCEDURE dbo.StoredProcedureMean
AS
BEGIN

Declare @Discount_mean FLOAT,
        @productid int,
        @Name varchar(40)
SELECT @Discount_mean = avg(discount), @productid=ProductID, @Name=productname FROM ProductDiscounted WHERE ProductID=55 GROUP BY productid, Productname
PRINT 'The average value of the discount applied to the product '+ cast(@Name as varchar(40)) + ' with ID ' + cast(@productid as varchar(40)) + ',in the sold, is: ' + cast(@Discount_mean as varchar(40))

END 

So, this is how I did till now, someone answer me if there something different I can do.