0

I have a simple mysql procedure which must return an query string. But, it returns almost every time QueryResult (column name) as <null> value.

create procedure return_table_rename_query(
  IN targetTable VARCHAR(100),
  IN tblPrefix VARCHAR(100)
)
BEGIN
    SET @returnQuery = CONCAT('SELECT "MYSQLIMPORT can not rename table for target ', @targetTable, '";');
    SET @totalRows = (SELECT COUNT(*) FROM table);
    if IFNULL(@totalRows, 0) > 0
        then
          SET @returnQuery = CONCAT('drop table if exists table_name.', ...);
    end if;

    SELECT @returnQuery AS 'QueryResult';
end;
Dharman
  • 30,962
  • 25
  • 85
  • 135
AlleXyS
  • 2,476
  • 2
  • 17
  • 37
  • solution was based on P.Salmon answer, which also is checked as solution. I don't think was needed to remove it. – AlleXyS Dec 10 '20 at 13:29
  • We have a policy that a question should not be edited with a solution. If you have something to add on top of the accepted answer you can post an alternative answer, but do not edit it into the question – Dharman Dec 10 '20 at 13:30

1 Answers1

3

@targettable is not the same variable as targettable - you are mixing user defined variables and parameter variables and it seems likely that @targettable is null and if any element in a concat is null then the result is null.

Please read How to declare a variable in MySQL?

P.Salmon
  • 17,104
  • 2
  • 12
  • 19