How to define a function in SQL