ASingh
ASingh

Reputation: 485

Running SELECT statement inside a Function in SQL

I am trying to use a select statement inside a function like this:

CREATE FUNCTION get_Id (d DATE) RETURNS INTEGER
BEGIN
DECLARE @retval INTEGER
SELECT id 
    from date_store c
    WHERE c.start_date <= d && c.end_date >= d
RETURN @retval
END

I am not sure if I have written this function correctly as I am not able to run it. Basically what I need is to call this function in order to get a id based on a date range given in the table date_store.

I am new to SQL.

Upvotes: 0

Views: 4848

Answers (1)

Rafa Paez
Rafa Paez

Reputation: 4860

Conditional && does not exists in SQL. Use AND instead. Also you need to assign the returned variable with SELECT id INTO @retval. Replace INTEGER by INT as well and do not forget about the semicolons. It will be something like this (not tested)

CREATE FUNCTION get_Id (d DATE) RETURNS INT
BEGIN
DECLARE @retval INT;
SELECT id INTO @retval
    FROM date_store c
    WHERE c.start_date <= d AND c.end_date >= d;
RETURN @retval;
END

MySQL function with SELECT statment

Upvotes: 1

Related Questions