0

trying to get the total number of boxes ordered from a given type("mytype"). something seems to be wrong with my syntax

create function NumOrdersForBoxType (mytype varchar(20))
returns int
begin
DECLARE @numorders int 
  select @numOrders = count(*)
    from BOXES as B
    where B.Type = mytype
    return @numOrders
end
;
P.Salmon
  • 17,104
  • 2
  • 12
  • 19
  • 1
    In mysql you don't declare user defined variables (at variables) https://stackoverflow.com/questions/11754781/how-to-declare-a-variable-in-mysql AND every statement needs to be terminated and since you have more than 1 statement in the function you need to wrap in begin..end and possibly set delimiters. AND you need to SET variables. – P.Salmon Jan 05 '21 at 15:00

1 Answers1

1

In mysql you don't declare user defined variables (at variables) stackoverflow.com/questions/11754781/… AND every statement needs to be terminated and since you have more than 1 statement in the function you need to wrap in begin..end and possibly set delimiters. AND you need to SET variables

delimiter $$

create function f(mytype varchar(20))
returns int
begin
DECLARE numorders int; 
    set numorders = (select count(*)
    from boxes as B
    where B.Type = mytype
    );
    return numOrders;
end $$
delimiter ;
P.Salmon
  • 17,104
  • 2
  • 12
  • 19