A
A
Andrey Pushkin2018-05-19 22:17:53
MySQL
Andrey Pushkin, 2018-05-19 22:17:53

What's wrong with the create function command?

I want to create a function to search for employees in a table

CREATE FUNCTION manager (n VARCHAR, s VARCHAR)
RETURNS VARCHAR DETERMINISTIC
BEGIN
SELECT CONCAT(`name`, ' ', `surname`) AS `ФИО`, `work`, `dep`, `cash` FROM personal WHERE `name` = n AND `surname` = s;
END

But this query is not working, sql is complaining
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ', s VARCHAR)
RETURNS VARCHAR
BEGIN
SELECT CONCAT(`name`, ' ', `surname`)' at line 1

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2018-05-19
@Arassir

Specify the number in brackets for VARCHAR
https://dev.mysql.com/doc/refman/8.0/en/create-pro...

M
Merzley, 2018-05-20
@Merzley

First, for the VARCHAR type, it is mandatory to specify its length. For example, VARCHAR(255).
Secondly, since you are creating a function, you must explicitly specify the RETURN() operation in the body. Those. your entire SELECT needs to be wrapped in a RETURN().
Third, a function cannot return multiple fields as a result. The function always returns a single value of the type specified with RETURNS. Those. if you need to return all user data as separate fields, then the function is not suitable, you need to create a view. If all fields are needed, but in the form of a single line, then all of them must be added to CONCAT (). If you need to return only the combined full name, then SELECT to the table is not needed here either.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question