MySQL function declare 2 variables with one select

1.9k Views Asked by At

I'd like to know how I can create a MySQL function that declares 2 variables by using 1 select statement. Something like this:

CREATE FUNCTION  `inHashtagCampaign` (campaignId INT,startDateTime DATETIME,endDateTime DATETIME)
RETURNS TEXT
LANGUAGE SQL
DETERMINISTIC
BEGIN 
    DECLARE result TEXT;
    DECLARE limit BIGINT(11); 
    DECLARE suspended TINYINT(1);
    #
    #I don't know how to do this, but I'd like to use the result of this query:
    #
    result = SELECT `limit`,`suspended` FROM `settings` WHERE `campaignId` = 2;
    limit = result.limit;
    suspended = result.suspended;
END;

I know this function is far from complete, but I'm kinda stuck on this thing already.

1

There are 1 best solutions below

0
On BEST ANSWER

I think what you are trying to do is like this

DECLARE @limit BIGINT(11); 
DECLARE @suspended TINYINT(1);

SELECT @limit := `limit`, @suspended := `suspended` FROM `settings` 
WHERE `campaignId` = 2;

(OR)

DECLARE limit1 BIGINT(11); 
DECLARE suspended1 TINYINT(1);

SELECT `limit`,`suspended` into limit1, suspended1
FROM `settings` WHERE `campaignId` = 2;