Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Can't get my function to work on MySql

Posted on 2011-02-17
5
Medium Priority
?
274 Views
Last Modified: 2012-05-11
I try to create following function. When i try to execute this script, I get syntax errors.

CREATE FUNCTION GetPartnerFieldValue(partnerId BIGINT, fieldCode VARCHAR(255))
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
	DECLARE sReturnString VARCHAR(255);

	SELECT * FROM (
		SELECT
			f.value
		INTO
			sReturnString
		FROM 
			PARTNERFIELD f,
			PARTNERFIELDTYPE ft
		WHERE
			f.partner_id = @partnerId
			AND f.partnerFieldType_id = ft.id
			AND ft.code = @fieldCode
	) result;

	RETURN sReturnString;
END;

Open in new window

0
Comment
Question by:onea
  • 2
  • 2
5 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 34914070
Can you try this?
CREATE FUNCTION GetPartnerFieldValue(partnerId BIGINT, fieldCode VARCHAR(255))
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
	DECLARE sReturnString VARCHAR(255);

		SELECT
			f.value
		INTO
			sReturnString
		FROM 
			PARTNERFIELD f,
			PARTNERFIELDTYPE ft
		WHERE
			f.partner_id = @partnerId
			AND f.partnerFieldType_id = ft.id
			AND ft.code = @fieldCode;

	RETURN sReturnString;
END;

Open in new window

0
 

Author Comment

by:onea
ID: 34914102
Same problem:

Error at line 5, error in sql syntax.
Undeclared variable sReturnString.
Syntax error on the return line;
0
 
LVL 41

Expert Comment

by:Sharath
ID: 34914122
How about this?
CREATE FUNCTION GetPartnerFieldValue(partnerId BIGINT, fieldCode VARCHAR(255))
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
	DECLARE sReturnString VARCHAR(255);

	SELECT * INTO
			sReturnString FROM
		 (
		SELECT
			f.value
		FROM 
			PARTNERFIELD f,
			PARTNERFIELDTYPE ft
		WHERE
			f.partner_id = @partnerId
			AND f.partnerFieldType_id = ft.id
			AND ft.code = @fieldCode
	) result;

	RETURN sReturnString;
END;

Open in new window

0
 

Author Comment

by:onea
ID: 34914142
Still the same.

It looks to me that the DECLARE function isn't working.
I get the first error when declaring the sReturnString variable.
And I think this causes the errors for rest of the script.
0
 
LVL 1

Accepted Solution

by:
stezeb earned 2000 total points
ID: 34920416
Hi!

You need to change the delimiter, when you're creating functions.
For details on this, look here:
http://dev.mysql.com/doc/refman/5.5/en/stored-programs-defining.html

Also SELECT from line 7 is messed up and useless, so put it away (like in comment #1).

In the end, it should look like this:
DELIMITER $$
CREATE FUNCTION GetPartnerFieldValue(partnerId BIGINT, fieldCode VARCHAR(255))
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
	DECLARE sReturnString VARCHAR(255);

		SELECT
			f.value
		INTO
			sReturnString
		FROM 
			PARTNERFIELD f,
			PARTNERFIELDTYPE ft
		WHERE
			f.partner_id = @partnerId
			AND f.partnerFieldType_id = ft.id
			AND ft.code = @fieldCode;

	RETURN sReturnString;
END$$
DELIMITER ;

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question