Can someone review my code and guide me what did I do wrong? I managed to create stored function. However, after selecting, the only result for hotellevel is silver.
CREATE FUNCTION HotelLevel
(
@rating DECIMAL(10,2)
)
RETURNS VARCHAR(20)
AS
BEGIN
DECLARE @hotel_level VARCHAR(20);
SELECT @rating = rating
FROM Hotels;
IF @rating < 4.10
SET @hotel_level = 'Blue';
ELSE IF (@rating >= 4.10 AND @rating < 4.50)
SET @hotel_level = 'Bronze';
ELSE IF (@rating >= 4.50 AND @rating < 4.80)
SET @hotel_level = 'Silver';
ELSE IF (@rating >= 4.80 AND @rating < 4.90)
SET @hotel_level = 'Gold';
ELSE IF @rating >= 4.90
SET @hotel_level = 'Platinum';
RETURN @hotel_level;
END
Table Result of storage function
I created this function in MySQL first and it worked. I tried to create the same in SQL Server and that's when something went wrong.
Just to expand on my comment.
Imagine
@Tieris an actual table with various tiers for various reasons. This structure can also be used for conditional aggregations and groupings.Example
Results