Creating scalar functions in tsql

  • Hi all,

    Firstly many apologies if this is in the wrong place. I'm basicly trying to create a scalar function that returns a nvarchar (15) variable.

    The Function has two input variables namely [category] and [AreaCode] (both nvarchar(15)) with an output variable [Team Line]. I've constructed this without any problem via a case statement (see below), but as I need to use this statement repeatingly I thought a scalar function would be the answer!

    I'm new to sql functions so any good advice would be much appreciated!

    select

    ,case when

    when [Category]='A' then 'Team A'

    when [Category]='B' then 'Team B'

    when [Category]='C' then 'Team C'

    when [Category]='K' and [AreaCode] in ('Acct','Desp','Analysis') then 'Team D'

    when [Category]='L' and [AreaCode] not in ('Lark') then 'Team E'

    else 'Others'

    end as [Team Line]

    Kindest regards,

    J

  • Take a look at the article by Paul White about APPLY. Half way the article he builds a function from an excisting query. You'll also get other interesting information about using such a function.

    http://www.sqlservercentral.com/articles/APPLY/69953/

    ** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
  • Assuming that there are tables defineing the Categories and AreaCodes, you can do this:

    CREATE TABLE #teammap (Category char(1) NOT NULL,

    AreaCode varchar(10) NOT NULL,

    Team varchar(20) NOT NULL,

    PRIMARY KEY (Category, AreaCode))

    INSERT #teammap (Category, AreaCode, Team)

    SELECT C.Category, A.AreaCode,

    case when [Category]='A' then 'Team A'

    when [Category]='B' then 'Team B'

    when [Category]='C' then 'Team C'

    when [Category]='K' and [AreaCode] in ('Acct','Desp','Analysis') then 'Team D'

    when [Category]='L' and [AreaCode] not in ('Lark') then 'Team E'

    else 'Others'

    end as [Team Line]

    FROM Categories C

    CROSS JOIN AreaCodes A

    Then you can use this temp table throughout your stored procedure and join to it.

    If there are no tables for any of the items, you can replace the table with (SELECT DISTINCT AreaCode FROM tbl) AS A for the table where they appear.

    There is quite an overhead for scalar functions, and a table-driven lookup is a more natural way in a relational database.

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

  • Thank you both for your excellent help, it's much appreciated.

    Kindest regards,

    J

  • do NOT use a scalar UDF if it or the place you are using it does data access. Read my chapter in the SQL Server MVP Deep Dives 2 book entitled "Death by UDF". It is VERY aptly named!! There are umpteen ways UDFs can screw you - just don't go there.

    SOMETIMEs an INLINE table-valued-function can be a good thing. That is one of the few exceptions I know of.

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply