Search code examples
mysqlsqlstored-procedures

Dynamic parameters in where clause


I have a problem with a select count in my stored procedure, it looks like that when I use two dynamics parameters it refuse to works.

For example :

1.

SELECT COUNT(*)
INTO @var
FROM TMP_TABLE
WHERE UDA10 = 3;

Works perfectly

2.

SELECT COUNT(*)
INTO @var
FROM TMP_TABLE
WHERE UDA10 = @vlQuota

Works too

3.

    SELECT COUNT(*)
    INTO @var
    FROM TMP_TABLE
    WHERE @llQuota = 3;   

Doesn't work (always return 0)

  1. (The one I want)

    SELECT COUNT(*)
    INTO @var
    FROM TMP_TABLE
    WHERE @llQuota = @vlQuota;
    

Doesn't work (always return 0)

So the problem may be with the @llQuota parameter but when I check it returns "UDA10" like it should.

I also tried to execute the query with PREPARE and EXECUTE but I have the same problem.

EDIT : UDA10 is a column I put in example, it can be anything that why I use @llQuota and not directly UDA10. The problem may be that MySQL does not understand @llQuota is a column.


Solution

  • Found the solution, I had to use PREPARE and EXECUTE but I also had to concatenate the parameters :

    Ex :

    SET @qry = CONCAT('SELECT count(*) into @nbExtract 
    FROM TMP_EXTRACTION WHERE ', @llQuota, '= ', @vlQuota, ';');
    PREPARE statement FROM @qry;
    EXECUTE statement;    
    

    Thanks for the help