SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Creating a Custom Function (Scalar)


Creating a Custom Function (Scalar)

Author
Message
RedBirdOBX
RedBirdOBX
Old Hand
Old Hand (340 reputation)Old Hand (340 reputation)Old Hand (340 reputation)Old Hand (340 reputation)Old Hand (340 reputation)Old Hand (340 reputation)Old Hand (340 reputation)Old Hand (340 reputation)

Group: General Forum Members
Points: 340 Visits: 243
Hey guys -

I'm trying to create a custom Function in SQL 2012 (by following an example in a book) and it's not working. I'm hoping someone can help.

This is my custom function. No errors. It executes fine. Simple right?
**********************************
CREATE FUNCTION fncNotAvailableDisplay
(@strInputString varchar(50))

RETURNS
varchar(20)

AS

BEGIN

If @strInputString IS NULL
SET @strInputString = 'Not Available'
RETURN @strInputString

END
**************************


When I try to use it in a query, I get an error.

SELECT Address1, Address2, fncNotAvailableDisplay(Address3) FROM Customers



'fncNotAvailableDisplay' is not a recognized built-in function name.



I am using it within the same database. Thoughts??
Lowell
Lowell
SSC Guru
SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)

Group: General Forum Members
Points: 75173 Visits: 40985
i believe functions that are not CLR functions must be prefaced by the schema:

SELECT
Address1,
Address2,
dbo.FNCNOTAVAILABLEDISPLAY(Address3)
FROM Customers



Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
RedBirdOBX
RedBirdOBX
Old Hand
Old Hand (340 reputation)Old Hand (340 reputation)Old Hand (340 reputation)Old Hand (340 reputation)Old Hand (340 reputation)Old Hand (340 reputation)Old Hand (340 reputation)Old Hand (340 reputation)

Group: General Forum Members
Points: 340 Visits: 243
Yep. That was it. Thanks!
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)

Group: General Forum Members
Points: 98430 Visits: 38996
I would like to point out that this function could be a performance killer if used with large data sets. There are two ways to fix this.

One, instead of the function in the select list you could do this:



SELECT
Address1,
Address2,
coalesce(Address3,'Not Available') as Address3
FROM
Customers




Or you could rewrite the function like this:



DROP FUNCTION dbo.fncNotAvailableDisplay;
go

CREATE FUNCTION dbo.fncNotAvailableDisplay
(@strInputString varchar(50))
RETURNS TABLE
AS
RETURN select coalesce(@strInputString,'Not Available') as NAVDisplay

go




And then use it like this:



SELECT
Address1,
Address2,
nad.NAVDisplay as Address3
FROM
Customers
CROSS APPLY dbo.fncNotAvailableDisplay(Address3) nad




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
TheSQLGuru
TheSQLGuru
SSC-Dedicated
SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)

Group: General Forum Members
Points: 33443 Visits: 8682
+100 to Lynn!!

Read my chapter in the SQL Server MVP Deep Dives 2 book entitled "Death by UDF" Cool

Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
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