Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6819001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:14:47+00:00 2026-05-26T21:14:47+00:00

In Oracle and MySQL, how can I create a function that takes an indefinite

  • 0

In Oracle and MySQL, how can I create a function that takes an indefinite number of parameters so that it may be called like GREATEST(value1,value2,…)?

Comparing two values by a certain standard is pretty easy but passing the “greater” value to yet another comparison is what I don’t seem to get work in SQL.

Thanks!

Edit (after Mike’s comment below):
I am looking for a solution for comparing multiple columns. In concrete terms, my question is how to implement GREATEST() as a UDF. The following code compares three columns.

SELECT CASE WHEN CASE WHEN col_1 < col_2 THEN col_2
                 ELSE col_1 END < col_3 THEN col_3
       ELSE CASE WHEN col_1 < col_2 THEN col_2
                 ELSE col_1 END END AS greatest
  FROM figures;

Apparently, this does not scale so well. It’ll be much more useful to have a general function that applies the same comparison method over and over to a list of values.

By SQL I mean any SQL database product but I prefer a solution that works in Oracle or MySQL

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:14:48+00:00Added an answer on May 26, 2026 at 9:14 pm

    In Oracle (instances that support unpivot)

    SELECT MyID, MAX(GreatestVal) 
    FROM figures
      UNPIVOT (
              GreatestVal 
              FOR MyID 
              IN (col_1, col_2, col_3,...)
              );
    

    Oracle query is untested because I don’t have an instance handy
    Further details on unpivot are located here and it does the same thing as the SQL Server unpivot.

    MySQL
    I’m unsure of how to do this in MySQL but may investigate as I have opportunity (unless someone beats me to it. 😉 )

    The following are SQL Server answers:

    Doing it in a UDF is not pretty because all of the input parameters would be REQUIRED in every case. If you could get away with implementing it as a stored procedure then you could specify default values for the input parameters and call it with a dynamic number of columns. Either way, you’ll have to decide on a maximum number of input parameters. Here is an example in UDF on SQL Server:

    SELECT dbo.GREATESTof3(col_1, col_2, col_3)
    FROM figures;
    
    CREATE FUNCTION GREATESTof3(@col_1 sql_variant = null, @col_2 sql_variant = null, @col_3 sql_variant = null)
    RETURNS sql_variant
    AS 
    BEGIN
        DECLARE @GreatestVal sql_variant
        DECLARE @ColumnVals TABLE (Candidate sql_variant)
    
    
        INSERT INTO @ColumnVals
        SELECT @col_1
        UNION ALL
        SELECT @col_2
        UNION ALL
        SELECT @col_3
    
        SELECT @GreatestVal = MAX(Candidate)
        FROM @ColumnVals
    
        RETURN @GreatestVal
    END
    

    This would require a new UDF for each variant of the number of input parameters OR creating one that could take a greater number but then in the call to the UDF you would have to either specify some value for each unused parameter (null) or specify default.

    Alternatives:

    This gives you the max value of the three columns from the whole table and would be easier to have a dynamic number of columns:

    SELECT MAX([Value]) AS Greatest
    FROM figures
    UNPIVOT
    (
        [Value]
        FOR ColumnName IN ([Col_1], [Col_2], [Col_3])
    ) AS unpvt 
    

    Assuming you have some rowid or another column that you would want in the output so that you could get the greatest from the specified columns for each row you could do something like this:

    SELECT RowID, MAX([Value]) AS Greatest
    FROM figures
    UNPIVOT
    (
        [Value]
        FOR ColumnName IN ([Col_1], [Col_2], [Col_3])
    ) AS unpvt 
    GROUP BY RowID 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can we set element to some default date in mysql ? In oracle
What would be equivalent of Number(4) of Oracle database to MySQL Datatype ?
I am new to MySQL coming from Oracle. I have a requirement to create
Ideally something that will work with Oracle, MS SQL Server, MySQL and Posgress.
I know, that this type of indexes exist in Oracle. Old versions on MySQL
With Oracle (or DB2, HSQLDB), I can express things like this: MERGE INTO [target_table]
I am moving data from different source types like Oracle, MySQL, Access, Sharepoint etc
In Oracle, I can re-create a view with a single statement, as shown here:
In MySQL, I can create an access a session variable by using a single
in mysql i can create a table named select using the following statement CREATE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.