Dongbae Dongbae - 15 days ago 6
MySQL Question

Passing in table for a procedure

How can someone pass in a table into a procedure and then output the first names inside the table?

Example:

myTable
firstName | lastName

John, Smith
Joe, Smith
Stan, Smith


And then only John, Joe and Stan pops up? I'm trying to make something similar for my personal website.

Here is what I have for MySQL:

CREATE PROCEDURE 'test'.'returnFirstName' ()

Answer

Try this:

CREATE PROCEDURE `test`.`returnFirstName` ()
BEGIN
  SELECT firstName 
  FROM myTable
END

To pass a parameter look at this: use a variable for table name in mysql sproc

Execution SQL:

SELECT test.returnFirstName();