views:

837

answers:

4

I understand that T-SQL is not object oriented. I need to write a set of functions that mimics method overloading in C#.

Is function overloading supported in T-SQL in any way? If there is a hack to do this, is it recommended?

+2  A: 

You could pass in a sql_variant, but it comes with all sorts of hazards around it; you can't really use strong typing like you can with OO languages and overloading.

If you need to find the base type within your function, you can use the SQL_VARIANT_PROPERTY function.

Jeremy Smyth
+1  A: 

you can pass in a array of values within a single string and parse them out using this techique by Erland Sommarskog.

create a function with a varchar(max) parameter or several if necessaey, then have your parameter values in that string like:

param1;param2;parma3;param4

or

param1:type;param2:type;param3:type

or

calltype|param1;param2;param3

etc, you are only limited by your imagination...

and use the technique from the link to split apart this array and use program logic to use those values as you wish...

+3  A: 

No, there is no way to do this.

I recommend you revisit the requirement, as "make apples look like oranges" is often difficult to do, and of questionable value.

John Saunders
A: 

I overload Functions all the time, but I happen to know that these kind of issues are often highly dependent on platform.

On our DB2 system, I routinely overload like the following:

CREATE Function Schema1.F1 (parm date) returns date return date + 1month;

CREATE Function Schema1.F1 (parm timestamp) returns date return date(timestamp) + 1month;

This is actually quite useful when you have multiple queries which have similar formating requirements.

The only problem I have found about this so far, is you better be sure that you want the function because the standard drop function "schema"."name" fails because it cannot determine which function to drop. If anyone knows how to drop overloaded sql functions, let me know!

Jay D. K.
This isn't really helpful as the question specifically states MSSQL 2005
ck
Just tested against our MSSQL2005 and it seems that it is not possible to overload in MSSQL.Create function count() returns int begin return (0) end;Create function count(@parm int) returns int begin return (@parm) end;[Error Code: 2714, SQL State:42S01] There is already an object named 'count' in the database.I have figured out how to drop overloaded functions in DB2:drop function Schema1.F1 (date);drop function Schema1.F1 (timestamp);Most of our functions look more like this though:drop function Schema1.F1 (varchar(256));
Jay D. K.