Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql server CLR scalar function with default column name

I was wondering, if there's any way to add a column names to my CLR scalar functions in Sql Server. I mean that after I run my query, I'd like to see a column with a result of this function already named with something custom instead of (No column name).

I know that often functions are combined together, or I'd have to name them something different for other reason, but still - when you write a query with 30 columns, not having to punch in an alias for 20 of them would be nice.

So does anyone know a hack that would enable this?

It'd also be sweet to have this feature through some addin in SSMS (e.g. building dummy aliases from functions and columns used in calculation, like "datediff_startdate_enddate"). I tried to find a ready solution to this, but with no effect. Any hints?

Edit: Some people asked me about code example. I don't think this would help much, but here it is:

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlTypes;
using System.Text.RegularExpressions;

namespace ClrFunctions
{
    public class RegexFunctions
    {

        public static SqlBoolean clrIsMatch(SqlString strInput, SqlString strPattern)
        {
            if (strPattern.IsNull || strInput.IsNull)
            {
                return SqlBoolean.False;
            }
            return (SqlBoolean)Regex.IsMatch(strInput.Value, strPattern.Value, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
        }
    }
}

T-SQL:

Create Assembly ClrFunctions From 'C:\CLR\ClrFunctions.dll' 
GO

Create Function dbo.clrIsMatch(
    @strInput As nvarchar(4000),
    @strPattern As nvarchar(255)
)
Returns Bit
As External Name [ClrFunctions].[ClrFunctions.RegexFunctions].[clrIsMatch]
GO

And this is my wish-to-be-possible call of this function in T-SQL and expected result:

select 
    txt, dbo.clrIsMatch(txt,'[0-9]+') 
from (
    select 'some 123 text' as txt union all 
    select 'no numbers here' union all 
    select 'numbers 456 again'
) x

Result already has a column name, without the need to add alias in T-SQL: enter image description here

like image 409
AdamL Avatar asked May 06 '13 15:05

AdamL


2 Answers

The answer is, "No." A scalar function (CLR or not) just returns a value.

"User-defined scalar functions return a single data value of the type defined in the RETURNS clause." per MSDN

As you suggested, the correct solution is adding an alias where you use the function.

SELECT ABS(a.Position - b.Position) AS Distance
    FROM sch.Tbl a
        INNER JOIN sch.Tbl b
            ON a.Key <= b.Key
;

And that doesn't change whether the function is a built-in, or a user-defined function, or a CLR function.

like image 74
Marc Shapiro Avatar answered Oct 05 '22 23:10

Marc Shapiro


I assume somewhere you have a call to something like this:

command.CommandText = "SELECT SomeFunction();";

try this

command.CommandText = "SELECT SomeFunction() 'AliasForColumnNameYouWant';";

Not sure what you mean by 20 or 30 functions?

Are you saying that within Sql-Server there is some stored procedure or function calling 20 or 30 different scalar functions?

Or maybe the same function being called 20 or 30 times?

And are you combining the results into a returnable row or column of data?

Need code examples please.

like image 27
fnostro Avatar answered Oct 05 '22 23:10

fnostro