Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Creating scalar functions in tsql


Creating scalar functions in tsql

Author
Message
jonathan_blac15
jonathan_blac15
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
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
HanShi
HanShi
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3049 Visits: 3633
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’! **
Erland Sommarskog
Erland Sommarskog
SSC Eights!
SSC Eights! (933 reputation)SSC Eights! (933 reputation)SSC Eights! (933 reputation)SSC Eights! (933 reputation)SSC Eights! (933 reputation)SSC Eights! (933 reputation)SSC Eights! (933 reputation)SSC Eights! (933 reputation)

Group: General Forum Members
Points: 933 Visits: 866
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
jonathan_blac15
jonathan_blac15
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 10
Thank you both for your excellent help, it's much appreciated.

Kindest regards,

J
TheSQLGuru
TheSQLGuru
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 5957 Visits: 8311
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search