mssql how do i do c and rest of a

BILIRIS MICHAIL 21 Reputation points
2021-02-06T16:36:33.073+00:00

A) A functon that accepts a letter from the user, and returns the sum of

sickleavehours of all employees whose last name starts from this

letter.

B) A function that accepts a letter from the user, and returns the sum of

vacationhours for all employees whose last name starts with this

letter.

C) A stored procedure that accepts a letter, and uses the above

functions to calculate the total cost of these hours by multiplying their sum

        with the highest salary (Rate) from the HumanResources.EmployeePayHistory table.

i need that on ms sql
so far i wrote:
(a)
DELIMITER $
CREATE FUNCTION totalSickHours(letter CHAR(1))
RETURNS DOUBLE DETERMINISTIC
BEGIN
DECLARE total DOUBLE DEFAULT 0;
SELECT SUM (sickleavehours) INTO total
FROM employee where lastName LIKE CONCAT(UPPER(letter), '%');
RETURN total;
END $
DELIMITER ;

i doesnt work

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,860 questions
{count} votes

Accepted answer
  1. Cris Zhan-MSFT 6,606 Reputation points
    2021-02-08T08:35:38.667+00:00

    Hi,

    You need to specify a parameter name by using an at sign (@) as the first character.
    https://learn.microsoft.com/en-us/sql/t-sql/statements/create-function-transact-sql?view=sql-server-ver15

     CREATE FUNCTION totalSickHours(@letter CHAR(1))  
     RETURNS FLOAT  
     AS  
     BEGIN  
         DECLARE @total FLOAT;  
          
         SELECT @total = ISNULL(SUM(sickLeaveHours),0)  
         FROM employee  
         WHERE lastName LIKE @letter+'%';  
          
         RETURN @total;  
     END;  
    

    And you don't need to specify float(53).The default value of n in float [ (n) ] is 53.
    https://learn.microsoft.com/en-us/sql/t-sql/data-types/float-and-real-transact-sql?view=sql-server-ver15

    0 comments No comments

0 additional answers

Sort by: Most helpful