Mysql stored function help needed - mysql

I'm just beginning to learn stored functions in mysql. Can someone please tell me whats wrong with below?
Phpmyadmin says 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 'return NAME;
END //' at line 19
DELIMITER //
DROP FUNCTION IF EXISTS getName //
CREATE FUNCTION getName(type CHAR(10), id int) RETURNS CHAR(50) DETERMINISTIC
BEGIN
DECLARE NAME CHAR(50);
CASE type
WHEN 'offer' THEN
SELECT Type_Name INTO NAME FROM otypes WHERE Type_Id = id;
WHEN 'service' THEN
SELECT ServiceType_Name INTO NAME FROM stypes WHERE ServiceType_Id = id;
WHEN 'store' THEN
SELECT Store_Name INTO NAME FROM stores WHERE Store_Id = id;
END CASE
return NAME;
END //

Put a semicolon after END CASE:
DELIMITER //
DROP FUNCTION IF EXISTS getName //
CREATE FUNCTION getName(type CHAR(10), id int) RETURNS CHAR(50) DETERMINISTIC
BEGIN
DECLARE NAME CHAR(50);
CASE type
WHEN 'offer' THEN
SELECT Type_Name INTO NAME FROM otypes WHERE Type_Id = id;
WHEN 'service' THEN
SELECT ServiceType_Name INTO NAME FROM stypes WHERE ServiceType_Id = id;
WHEN 'store' THEN
SELECT Store_Name INTO NAME FROM stores WHERE Store_Id = id;
END CASE;
-- ^
return NAME;
END //

You missing a ;
END CASE
return NAME;
should be
END CASE;
return NAME;

Related

What is proper syntax for returning a string with MySQL function?

I followed the example provided in our class material and made a function that returns a count. Now I am trying to make a function that would get me the full name of a user by the same principle I made the count function but I keep getting this error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 4
CREATE FUNCTION getAuthorFullName (authorID INT)
RETURNS CHAR(45)
BEGIN
DECLARE author CHAR(45);
SELECT CONCAT(first, ' ',last) FROM person as fullName
JOIN user ON user.idPerson = fullName.idPerson
WHERE idUser LIKE authorID
INTO author;
RETURN author;
I checked the SELECT query on its own and it retrieves the correct full name of the author when I pass the authors ID manually so I just have to get it working in a function where I can simply pass the author's ID.
INTO go after the value.
Also if idUser is integer use = operator. LIKE id for strings
SELECT CONCAT(first, ' ',last) INTO author
FROM person as fullName
JOIN user ON user.idPerson = fullName.idPerson
WHERE idUser = authorID
;
Here is a sql demo
DROP FUNCTION IF EXISTS getAuthorFullName;
CREATE FUNCTION getAuthorFullName (authorID INT)
RETURNS CHAR(45)
BEGIN
DECLARE author CHAR(45) ;
SELECT CONCAT('first', ' ','last' ) INTO author;
RETURN author;
END;
SELECT getAuthorFullName(1);

mysql different row updates depending on a variable (stored procedure)

CREATE PROCEDURE update_table(
IN choice INT(4),
IN id VARCHAR(50),
IN string VARCHAR(50)
)
BEGIN
UPDATE salesman
set salesman_name = IF(choice = 1, string, salesman_name)
where salesman_id = id
UPDATE salesman
set date = IF(choice = 2, string, date)
where salesman_id = id
END
if choiceis 1, change salesman_name as string
if choice is 2, change date as string
can you explain me what i'm doing wrong?
it works fine with a single update, my guess is there is another way to implement if but i couldn't.
if choice = 1 then
update salesman set salesman_name = string where salesman_id = id
...i tried this version too but still, not working.
DELIMITER //
CREATE PROCEDURE update_table(
IN choice INT(4),
IN id VARCHAR(50),
IN string VARCHAR(50)
)
BEGIN
UPDATE salesman set salesman_name = IF(choice = 1, string, salesman_name) where salesman_id = id;
UPDATE salesman set date = IF(choice = 2, string, date) where salesman_id = id;
END //
DELIMITER ;
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 'DELIMITER' at line 1
also says this:
ERROR: Unknown Punctuation String # 11 (last line)
When a stored procedure has more than one statement, they need to be terminated with ;
To do that, you need to tempoararily change the delimiter so you can end the procedure. Here's a SO answer with an example of how to do that: MySQL create stored procedure syntax with delimiter

MySQL script error

I'm new to SQL programming and I decided to make a script. This one might be quite riddled with errors but I'm getting an error that I'm unable to resolve.
DELIMITER $
DROP FUNCTION IF EXISTS crossref$
CREATE FUNCTION crossref()
BEGIN
DECLARE i INT;
DECLARE names VARCHAR(70);
SET i = 1;
myloop: LOOP
SET i=i+1;
IF i = 6 then
LEAVE myloop;
END IF;
SET names = (SELECT NAME FROM cbase_excel_table WHERE ID = i);
INSERT INTO cbase_master(NAME, PERMALINK, HOMEPAGE_URL, CATEGORY_LIST, MARKET, FUNDING, 'STATUS', COUNTRY, REGION, CITY)
SELECT NAME, PERMALINK, HOMEPAGE_URL, CATEGORY_LIST, MARKET, FUNDING, 'STATUS', COUNTRY, REGION, CITY FROM cbase_excel_table WHERE ID = i;
UPDATE cbase_master
SET DESCRIPTION = (SELECT DESCRIPTION FROM cbase_json_table WHERE NAME = names)
SET DOMAIN = (SELECT DOMAIN FROM cbase_json_table WHERE NAME = names)
SET IMAGE_URL = (SELECT IMAGE_URL FROM cbase_json_table WHERE NAME = names)
SET FACEBOOK_URL = (SELECT FACEBOOK_URL FROM cbase_json_table WHERE NAME = names)
SET TWITTER_URL = (SELECT TWITTER_URL FROM cbase_json_table WHERE NAME = names)
SET LINKEDIN_URL = (SELECT LINKEDIN_URL FROM cbase_json_table WHERE NAME = names)
SET CBASE_UUID = (SELECT CBASE_UUID FROM cbase_json_table WHERE NAME = names);
END LOOP myloop;
END$
DELIMITER;
and I'm getting:
#1064 - 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 'BEGIN
DECLARE i INT;
DECLARE names VARCHAR(70);
SET i = 1;
Any help?
An example of a function which shows a major difference to your function:
CREATE FUNCTION `fnFindMaximum`(`a` INT, `b` INT)
/* Before the BEGIN statement there are other things going on - the most important being the return type statement */
RETURNS int(11)
LANGUAGE SQL
DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE returnval INTEGER;
IF a >= b THEN
SET returnval = a;
ELSE
SET returnval = b;
END IF;
RETURN returnval;
END
Your function then goes on to manipulate sql but does not return a value so, as was pointed out by #arkhil, use a StoredProcedure in preference to a function.

Mysql Function returns Null always

I have a function in mysql like below:
DELIMITER $$
CREATE DEFINER=root#localhost FUNCTION fnGetDropDownValue(
itemValue varchar(300),
DropDownId int,
CId int
) RETURNS int(11)
BEGIN
DECLARE listId int;
SELECT ListID into listId FROM DropDownListValues WHERE LOWER(ListValue) = LOWER(itemValue) AND DropDownListID = DropDownId AND (ClientId = 0 OR ClientId = CId);
RETURN listId;
END$$
But it always returns Null values when I use
SELECT fnGetDropDownValue('General', 24, 18);
I don't know what I am doing wrong :(
After having the case sensitive issue with mysql columns I used to have variable names to start with _ to avoid it messing with column names. Now the stored procedure looks like this:
DELIMITER $$
CREATE DEFINER=root#localhost FUNCTION fnGetDropDownValue(
itemValue varchar(300),
DropDownId int,
CId int
) RETURNS int(11)
BEGIN
DECLARE _listId int;
SELECT ListID into _listId FROM DropDownListValues WHERE LOWER(ListValue) = LOWER(itemValue)
AND DropDownListID = DropDownId AND (ClientId = 0 OR ClientId = CId);
RETURN _listId;
END$$
This way it will work on any platform and it may be useful for others.

MySQL stored function SELECT INTO unexpected results

I am trying to write a stored function in mysql 5.1 that returns the value 'AccountIDref' for a given room. If I only query the inner SELECT statement this works (returns the value for room). But invoking the function I get the response:
'#1172 - Result consisted of more than one row'
CREATE FUNCTION getAccountId (room INT) RETURNS INT
BEGIN
DECLARE refID INT DEFAULT NULL;
SELECT AccountIDref INTO refID FROM Allocation
WHERE Room = room;
RETURN refID;
END
What am I doing wrong here?
Field name and parameter name must be different -
CREATE FUNCTION getAccountId (room_param INT) RETURNS INT
BEGIN
DECLARE refID INT DEFAULT NULL;
SELECT AccountIDref INTO refID FROM Allocation
WHERE Room = room_param;
RETURN refID;
END
In your function you were getting all tables records.
What I am going to suggest isn't going to be much different from what you have, but I am skeptical about the where clause being in the next line and also let's use limit 1 to explicitly set the limit.
Try this :
CREATE FUNCTION getAccountId (room INT) RETURNS INT
BEGIN
DECLARE refID INT DEFAULT NULL;
SELECT AccountIDref INTO refID FROM Allocation WHERE Room = room LIMIT 1;
RETURN refID;
END