1

I am trying to return a value from a table. The table is a parameter table that will have it's value changed in the future. The table name is 'Parameter' and I'm trying to get the value from the value column where the term is = 'Base URL'

In PHP My Admin, I am creating a function with the following query and it's giving me an error. I can't seem to locate the error.

DECLARE @url VARCHAR;
SET @url = '';
SELECT p.value INTO @url FROM Parameters p WHERE p.Term = 'Base URL';
RETURN @url;

I am getting an error 1064 near "DECLARE @url VARCHAR(255); SET @url = ''; SELECT p.value INTO @url FROM Param"

Where is my error? I couldn't find a similar situation. I have used parameter tables in the past, but never created one or a function to go along with it. Is there a better way to do what I want?

Thanks!

Chad K
  • 832
  • 1
  • 8
  • 19
  • Is this possibly just the missing semicolon after the query? i.e. "... 'Base URL';" – EdmCoff Nov 25 '17 at 03:37
  • @EdmCoff no I added that, it still doesn't work. I've seen bits and parts from examples, like setting variables in a select statement and declaring variables, but none like this. That's why I can't find where the error is. – Chad K Nov 25 '17 at 03:42

1 Answers1

2

I have tried to create similar function like this and remove this line make it works

    DECLARE @url VARCHAR;

May be you have some confuses between variable with @ or not. This link may be useful for you MySQL: @variable vs. variable. What's the difference?

One more thing, some people may have problem with delimiter. I get this work

    DROP FUNCTION IF EXISTS get_url;

    DELIMITER $$
    CREATE FUNCTION get_url()
    RETURNS varchar(255)
    BEGIN 
      DECLARE url varchar(255);

        SELECT p.value INTO url FROM Parameters p WHERE p.Term = 'Base URL';

      RETURN url;
    END$$

    DELIMITER ;
Bùi Đức Khánh
  • 3,975
  • 6
  • 27
  • 43
  • I'm sorry for the confusion, the value I'm trying to get is from the table `Parameters` in the column `value`. I am trying to return just the value where the term = "Base URL". It's almost like a constant. But I will change it in the future, which is why I want to create a function that gets it – Chad K Nov 25 '17 at 03:47
  • Parameter is real name of table and it is not parameter of your store procedured right ? – Bùi Đức Khánh Nov 25 '17 at 03:50
  • @ChadK I have updated answer, hope this may help you – Bùi Đức Khánh Nov 25 '17 at 04:13
  • While it doesn't work in phpMyAdmin, If I manually execute in using another program, it works. Thank you! – Chad K Nov 25 '17 at 04:20