Unix Technical Forum

UDF using EXISTS help requested

This is a discussion on UDF using EXISTS help requested within the SQL Server forums, part of the Microsoft SQL Server category; --> I have this UDF CREATE FUNCTION IsSupervisor (@empID int) RETURNS bit AS BEGIN DECLARE @retval bit SET @retval = ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-12-2008, 05:23 PM
CK
 
Posts: n/a
Default UDF using EXISTS help requested

I have this UDF

CREATE FUNCTION IsSupervisor (@empID int)

RETURNS bit

AS

BEGIN

DECLARE @retval bit

SET @retval = 'FALSE'

IF EXISTS ( SELECT SupervisorID FROM Supervisors WHERE SupervisorID =
@empID )

SET @retval = 'TRUE'

RETURN @retval

END

This works ok. My question is "is there a better way to write this
function?" Can I just return the results of the Exists statement? Instead of
creating and setting a variable? If there is a row I want to return true,
else return false. Any comments are appreciated.

TIA,

~ck


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 09-12-2008, 05:23 PM
Plamen Ratchev
 
Posts: n/a
Default Re: UDF using EXISTS help requested

You can write the function without using a variable:

CREATE FUNCTION IsSupervisor (@empID INT)
RETURNS BIT
AS
BEGIN
RETURN CASE WHEN EXISTS(SELECT *
FROM Supervisors
WHERE SupervisorID = @empID)
THEN 1
ELSE 0
END;
END

Note the BIT data type can have values of 1, 0 and NULL, but not
'True'/'False'.

--
Plamen Ratchev
http://www.SQLStudio.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-12-2008, 05:23 PM
Erland Sommarskog
 
Posts: n/a
Default Re: UDF using EXISTS help requested

CK (c_kettenbach@hotmail.com) writes:
> I have this UDF
>
> CREATE FUNCTION IsSupervisor (@empID int)
>
> RETURNS bit
>
> AS
>
> BEGIN
>
> DECLARE @retval bit
>
> SET @retval = 'FALSE'
>
> IF EXISTS ( SELECT SupervisorID FROM Supervisors WHERE SupervisorID =
> @empID )
>
> SET @retval = 'TRUE'
>
> RETURN @retval
>
> END
>
> This works ok. My question is "is there a better way to write this
> function?" Can I just return the results of the Exists statement?
> Instead of creating and setting a variable? If there is a row I want to
> return true, else return false. Any comments are appreciated.


The best is to not write the function at all, but use the expression
inline. Maybe in some future version there will be inline scalar functions,
but until then, best practice is to avoid dataaccess in UDFs.

Say that you run this query:

SELECT ...
FROM Employees
WHERE dbo.IsSupervisor(EmpId) = 1

SQL Server will have to scan Employees and call the UDF for every single
row.

If you instead write:

SELECT ...
FROM Employees E
WHERE EXISTS (SELECT *
FROM Supervisors S
WHERE E.EmpID = S.SupervisorID)

The optimizer have the choioce of reading the (supposedly) smaller
Supervisor table, and the look up the rows in the Employees table
through the index.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-12-2008, 05:23 PM
--CELKO--
 
Posts: n/a
Default Re: UDF using EXISTS help requested

1) Read what Erland said about performance. That is not the worst of
it.

2) You have no idea how to program in SQL, so you mimic what you do
know -- procedural code. We don't use the proprietary BIT data type
in good SQL. This is a declarative predicate language. We also don't
have BOOLEANs in SQL so assigning BIT (a proprietary numeric data
type) a string value is a pretty awful conceptual error.

3) This was not good practices in procedural code either -- do you
remember the types of coupling from Basic Software Engineering? This
is not fancy RDBMS stuff; this is foundations!

4) Why are Supervisors in their own table? That says they are not the
same kind of entity as an employee. Yet they are identified by an
emp_id. That means your data model is wrong; look up attribute
splitting. Do you also have a MalePersonnel and FemalePersonnel
table? Why not use weight and have FatPersonnel and ThinPersonnel?
This needs a re-design and not a kludge.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 09-12-2008, 05:23 PM
CK
 
Posts: n/a
Default Re: UDF using EXISTS help requested

Awesome. Thanks. the supervisor data is from a view by the way.It is not in
its own table.

"--CELKO--" <jcelko212@earthlink.net> wrote in message
news:0cc2bdf7-3417-4bd1-b9fa-09c57470fd4b@t54g2000hsg.googlegroups.com...
> 1) Read what Erland said about performance. That is not the worst of
> it.
>
> 2) You have no idea how to program in SQL, so you mimic what you do
> know -- procedural code. We don't use the proprietary BIT data type
> in good SQL. This is a declarative predicate language. We also don't
> have BOOLEANs in SQL so assigning BIT (a proprietary numeric data
> type) a string value is a pretty awful conceptual error.
>
> 3) This was not good practices in procedural code either -- do you
> remember the types of coupling from Basic Software Engineering? This
> is not fancy RDBMS stuff; this is foundations!
>
> 4) Why are Supervisors in their own table? That says they are not the
> same kind of entity as an employee. Yet they are identified by an
> emp_id. That means your data model is wrong; look up attribute
> splitting. Do you also have a MalePersonnel and FemalePersonnel
> table? Why not use weight and have FatPersonnel and ThinPersonnel?
> This needs a re-design and not a kludge.



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 09-12-2008, 05:23 PM
--CELKO--
 
Posts: n/a
Default Re: UDF using EXISTS help requested

>> The supervisor data is from a view by the way. It is not in its own table. <<

That makes a little more sense. Is the determination of supervisor
versus non-supervisor really hard enough or used frequently enough to
justify a VIEW? I don't need a function if I have a VIEW -- think
declarative versus procedural coding.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads for: UDF using EXISTS help requested

Thread Thread Starter Forum Replies Last Post
Forcing PGP 8.1 to use the requested PGP Key Robert A. Rosenberg PGP Basics 7 07-02-2008 01:25 PM
Requested media is in use... Casper Uldal Olsen NetBackup Enterprise Server 1 06-20-2008 06:21 AM
if exists...does it exists for insert statments? Pau Marc Munoz Torres Pgsql General 3 04-10-2008 12:14 AM
NFS help requested please jDeGraw Linux Operating System 4 01-17-2008 08:08 PM
All Genius are requested to have a look please . . sudhansutiwari@gmail.com HP-UX Operating System 3 01-17-2008 06:51 AM


All times are GMT. The time now is 05:34 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com