0

I want to add mysql function:

CREATE FUNCTION `chf_get_translation`(translation_id INT, site_lang INT) 
RETURNS text CHARSET utf8
BEGIN

  DECLARE translation TEXT;

  SELECT title 
    INTO translation 
    FROM chf_translations 
   WHERE key_id = translation_id 
     AND lang_id = site_lang;

  RETURN translation;

END

But get error:

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 '' at line 3

hakre
  • 193,403
  • 52
  • 435
  • 836
Liutas
  • 5,547
  • 4
  • 23
  • 22

2 Answers2

5

Try...

DELIMITER $$

CREATE FUNCTION `chf_get_translation`(translation_id INT, site_lang INT) 
RETURNS text CHARSET utf8
BEGIN

  DECLARE translation TEXT;

  SELECT title 
    INTO translation 
    FROM chf_translations 
   WHERE key_id = translation_id 
     AND lang_id = site_lang;

  RETURN translation;

END$$
Ed I
  • 7,008
  • 3
  • 41
  • 50
Gary
  • 2,866
  • 1
  • 17
  • 20
  • This helps thanks; But now i get error: ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable) – Liutas Jun 15 '10 at 08:44
  • 5
    I found answer. I need "READS SQL DATA" after "RETURNS text CHARSET utf8" – Liutas Jun 15 '10 at 08:55
2
DELIMITER $

CREATE FUNCTION `chf_get_translation`(translation_id INT, site_lang INT) 
RETURNS text CHARSET utf8
DETERMINISTIC
BEGIN

  DECLARE translation TEXT;

  SELECT title 
    INTO translation 
    FROM chf_translations 
   WHERE key_id = translation_id 
     AND lang_id = site_lang;

  RETURN translation;

END$
Cosmin
  • 21,216
  • 5
  • 45
  • 60
Rahul
  • 21
  • 1