简体   繁体   中英

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

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?

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM