Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Creating scalar functions in tsql Expand / Collapse
Author
Message
Posted Monday, September 2, 2013 4:36 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, July 27, 2014 3:24 PM
Points: 6, Visits: 10
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
Post #1490583
Posted Monday, September 2, 2013 5:57 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 2:56 AM
Points: 2,352, Visits: 2,862
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’! **
Post #1490596
Posted Monday, September 2, 2013 7:39 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 1:24 PM
Points: 805, Visits: 722
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.


Erland Sommarskog, SQL Server MVP, www.sommarskog.se
Post #1490625
Posted Monday, September 2, 2013 7:51 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, July 27, 2014 3:24 PM
Points: 6, Visits: 10
Thank you both for your excellent help, it's much appreciated.

Kindest regards,

J
Post #1490629
Posted Monday, September 2, 2013 11:29 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 1:11 PM
Points: 4,368, Visits: 6,208
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 at GMail
Post #1490669
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse