Home > Blockchain >  How to define a function returning one of multiple data types?
How to define a function returning one of multiple data types?

Time:05-31

I have a function with a conditional result. Based on the argument value it should either return varchar or int or uuid or timestamp.

What should the return type of that function be?

CodePudding user response:

SQL is strictly typed. The simple solution is to return one text field. Every value can be cast to text. (You might add a second return column holding the type name as meta information.)

Alternatively, return four typed fields (varchar, int, uuid, timestamp), and only fill one of them, depending on the result.

A polymorphic function would only be an option if you can tell the return type at call time and pass typed input accordingly.

Related:

  • Related