Custom function in SQL stored procedure?

Is it possible to create a temporary function in a stored procedure?

e.g.
SELECT 
    A = (CASE X WHEN...)
    B = (CASE Y WHEN...)
    C = (CASE Z WHEN...)
...

Open in new window

The CASE statement is identical except for the parameter X,Y,Z. It would be nice to pull this CASE statement out into a function. But the function is specific to this stored procedure, so I don't necessarily want to create a global external function.
deleydAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Yes, but if all the function does is have one parameter, a big honkin' CASE block, and return a scalar value, then I recommend coding this in your SP's/views/whatever and not in a scalar function, as using scalar functions degrades the Query Optimizer's ability to generate accurate plans, as it doesn't incorporate the function plan into it's own.

Having said that, give us a better data mockup of what you're trying to pull off here.
0
Scott PletcherSenior DBACommented:
No, not that I know of.  You can use dynamic SQL to create a standard function,  use it in the proc, and then delete it, but there's no such thing as a "temporary function" the way there is, say, a temporary table.
0
deleydAuthor Commented:
I realized it's a bit more complicated. Here's some code snippets:
         'FacilityRegion' = CASE WHEN FAA.zip IN (93434, 93454, 93455, 93456, 93457, 93458, 93254)
							    THEN 'Santa Maria' 
							    WHEN FAA.zip IN (93427, 93436, 93437, 93438, 93440, 93441, 93460, 93463)
								THEN 'Lompoc'
								WHEN FAA.zip IN (93101, 93103, 93105, 93108, 93109, 93110, 93111, 93117,93013,93014, 93067)
								THEN 'Santa Barbara'
								ELSE 'All Others' END

Open in new window

When it's used again, FAA.zip_1 is replaced with a different table different column.

I suppose it's OK to place this in the global Functions area of SSMS. How would that look converted to a function? Or is that a bad idea because it thwarts optimization?

I'm actually not interested in the zip code at all, I'm interested in the Region.

Furthermore, some facilities have two addresses.

Now say I have a table named Facility containing Facility IDs, and I want to JOIN with the Facility_Address table which gives the zip code.

Some Facilities have two addresses, so I'll end up with two rows for that facility ID, one for each address.

But what I really want is the Region. If both zip codes are in the same Region, then I want the JOIN to give me just one row result listing the Region.

The CASE statement above is used to convert a zip code to a Region.

Facility Table   Facility_Address Table
--------------   ----------------------
Facility_ID      Facility_ID   Zip Code
   1                1            93101
                    1            93103

Open in new window

What SELECT statement will give me a one line result (even though Facility 1 has two zip codes):
Facility_ID  Region
-----------  ------
   1         Lompoc

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Scott PletcherSenior DBACommented:
Create a zip code to Region table, then do lookups in that table.   A function will typically be much slower than a table lookup for anything between a few values.  Be sure to cluster the table on the lookup key, in this case zip code, NOT on a dummy identity value!!
0
deleydAuthor Commented:
Zip code to Region table sounds like a good idea.

How will the SELECT statement deal with the case where the zip code is not in the lookup table? (The "All Others" case.)
0
Scott PletcherSenior DBACommented:
Do an OUTER JOIN; the result will be NULL if the zip code is not found.  Will post an example ASAP.
0
Scott PletcherSenior DBACommented:
CREATE TABLE dbo.Zip_Lookup (
    zip varchar(10) NOT NULL,
    region varchar(30) NULL,
    CONSTRAINT Zip_Lookup__PK PRIMARY KEY ( zip )
    )

INSERT INTO Zip_Lookup SELECT '93434', 'Santa Maria'
INSERT INTO Zip_Lookup SELECT '93454', 'Santa Maria'
INSERT INTO Zip_Lookup SELECT '93455', 'Santa Maria'
INSERT INTO Zip_Lookup SELECT '93456', 'Santa Maria'
INSERT INTO Zip_Lookup SELECT '93457', 'Santa Maria'
INSERT INTO Zip_Lookup SELECT '93458', 'Santa Maria'
INSERT INTO Zip_Lookup SELECT '93254', 'Santa Maria'
INSERT INTO Zip_Lookup SELECT '93427', 'Lompoc'
INSERT INTO Zip_Lookup SELECT '93436', 'Lompoc'
INSERT INTO Zip_Lookup SELECT '93437', 'Lompoc'
INSERT INTO Zip_Lookup SELECT '93438', 'Lompoc'
INSERT INTO Zip_Lookup SELECT '93440', 'Lompoc'
INSERT INTO Zip_Lookup SELECT '93441', 'Lompoc'
INSERT INTO Zip_Lookup SELECT '93460', 'Lompoc'
INSERT INTO Zip_Lookup SELECT '93463', 'Lompoc'
INSERT INTO Zip_Lookup SELECT '93101', 'Santa Barbara'
INSERT INTO Zip_Lookup SELECT '93103', 'Santa Barbara'
INSERT INTO Zip_Lookup SELECT '93105', 'Santa Barbara'
INSERT INTO Zip_Lookup SELECT '93108', 'Santa Barbara'
INSERT INTO Zip_Lookup SELECT '93109', 'Santa Barbara'
INSERT INTO Zip_Lookup SELECT '93110', 'Santa Barbara'
INSERT INTO Zip_Lookup SELECT '93111', 'Santa Barbara'
INSERT INTO Zip_Lookup SELECT '93117', 'Santa Barbara'
INSERT INTO Zip_Lookup SELECT '93013', 'Santa Barbara'
INSERT INTO Zip_Lookup SELECT '93014', 'Santa Barbara'
INSERT INTO Zip_Lookup SELECT '93067', 'Santa Barbara'


SELECT td.*, ISNULL(zl.Region, 'All Others') AS Region
FROM (
    SELECT '93427' AS zip UNION ALL
    SELECT '93111' UNION ALL
    SELECT '90210'
) AS td --test_data
LEFT OUTER JOIN dbo.Zip_Lookup zl ON zl.zip = td.zip
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
deleydAuthor Commented:
Super!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.