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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass columnname to a stored procedure? 1

Status
Not open for further replies.

joelwenzel

Programmer
Jun 28, 2002
448
Hi,


I want to pass a column name as a variable to a stored procedure. Is this possible? Here is something like what I want although it generates an error.

CREATE FUNCTION `GetMax`(ColName char(10)) RETURNS int(11)
BEGIN

DECLARE MaxScore INT;
SELECT Max(ColName) INTO MaxScore FROM Scores WHERE ColName >0;
return MaxScore;
END
 
I don't know much about user-defined functions, but I think you would have to
name each column explicitly:
[tt]
CREATE FUNCTION `GetMax`(ColName char(10)) RETURNS int(11)
BEGIN

DECLARE MaxScore INT;
SELECT
CASE colname
WHEN 'col1' THEN Max(col1)
WHEN 'col2' THEN MAX(col2)
WHEN 'col3' THEN MAX(col3)
ELSE -1
END
INTO MaxScore
FROM Scores
WHERE ColName >0;
return MaxScore;
END
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top