Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using a paramater as table name

Status
Not open for further replies.

alero

Programmer
Feb 12, 2006
2
US
hi. i'm writing a procedure that retrives row count of any table (in a specific dataase). table name should be supplied as a parameter to the procedure.
i wrote:
--++++++++++++++++++++++++++++++++++++++++++++++++++++++
DROP PROCEDURE IF EXISTS getCount;
CREATE PROCEDURE getCount(IN tableName varchar(50), OUT rowCount int)
BEGIN
SELECT count(*) INTO rowCount FROM tableName;
END;
--++++++++++++++++++++++++++++++++++++++++++++++++++++++
when i call the procedure it says: table "tableName" does not exist, which means it does not recognizes "tableName" as a variable.

How can I persuade MySQL to accept it as a variable?!
Thanx for help.
 
At last I found the solution, I'll put it here so that no one will get stuck in this thing again...

CREATE PROCEDURE getCount (IN tbl varchar(50))
BEGIN
SELECT CONCAT(CONCAT(CONCAT("SELECT COUNT(*) AS ", tbl)," FROM "), tbl)INTO @query;
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top