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
;
1
  • 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. Commented Jan 5, 2021 at 15:00

1 Answer 1

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 ;
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for the answer. that doesn't seem to work
Please provide sample data as text and explain why you think it doesn't work.
am I not supposed to add the '@' before declaring a local variable?
Not in MySQL did you read the link in my comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.