MYSQL returns one query of a row with the format

I got this routine:

DELIMITER $$
CREATE PROCEDURE `countRows`(IN v varchar(30))
BEGIN
   SET @t1 =CONCAT("SELECT COUNT(*) FROM  ",V);
   PREPARE stmt3 FROM @t1;
   EXECUTE stmt3;
   DEALLOCATE PREPARE stmt3;
END$$
DELIMITER ;

      

And I want this to return a query in this format: "Table X contains rows Y" I tried using the concat function, but it just doesn't work for me.

Some tips? Thanks to

+3


source to share


1 answer


I was able to get this working using a subquery. I couldn't find a way to search for concat and count at the same time, but by wrapping the counter in a subquery and then using that value in a select clause, I was able to return the expected results. Try the following:

SELECT CONCAT("The table contains ", tmp.numRows, " rows.")
FROM(
  SELECT COUNT(*) AS numRows
  FROM myTable) tmp;

      



Here is a SQL Fiddle example of a query by itself, not as a prepared statement.

+1


source







All Articles