Solved

Check and Create UDF

Posted on 2009-07-01
8
377 Views
Last Modified: 2012-08-14
HI Experts,

This should be easy. How to check if udf exist, if not then create a blank udf.

IF NOT EXISTS (select name
from sysobjects
where type in ('FN', 'IF', 'TF')
  and name = 'Some_udf')
      EXEC('CREATE FUNCTION dbo.Some_udf AS RETURN 0')
GO

-- The Alter Statement goes here

ALTER FUNCTION [dbo].Some_udf
(
  @blah01 CHAR(13),
  @blah02 CHAR(2)
)  
RETURNS CHAR(2)
AS
BEGIN
END
RETURN ....
0
Comment
Question by:allanau20
8 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 250 total points
Comment Utility
IF NOT EXISTS (select name from sysobjects where type in ('FN', 'IF', 'TF')
  and name = 'Some_udf')
EXEC('CREATE FUNCTION dbo.Some_udf()returns int  as BEGIN  RETURN(0) END')
0
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 125 total points
Comment Utility
Why not just delete if it exists:
IF EXISTS (select name

from sysobjects

where type in ('FN', 'IF', 'TF')

  and name = 'Some_udf')

      DROP FUNCTION dbo.Some_udf

GO
 

-- The Alter Statement goes here
 

CREATE FUNCTION [dbo].Some_udf

(

  @blah01 CHAR(13), 

  @blah02 CHAR(2)

)  

RETURNS CHAR(2)

AS

BEGIN

-- function code here

RETURN

END

Open in new window

0
 
LVL 31

Assisted Solution

by:RiteshShah
RiteshShah earned 125 total points
Comment Utility
one short way to check existence of the function or any other object.


IF OBJECT_ID('Some_udf') is not null  DROP FUNCTION dbo.Some_udf
GO
 
CREATE FUNCTION [dbo].Some_udf()  
returns int
AS
BEGIN
return 1
END


0
 
LVL 5

Author Comment

by:allanau20
Comment Utility
Thanks all for your replies.
correct me if i'm wrong; one reason why i don't want to drop is because the object will lose it's permission if dropped.
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
Correct, so you can check for non-existence and create as you were but then you would have to explicitly GRANT permissions in that case; therefore, you can always drop and then after creation add GRANT statements you need.
0
 
LVL 5

Author Comment

by:allanau20
Comment Utility
THanks. Seems that's more work than using Alter -- permission is set once and you don't have to deal with it.
0
 
LVL 5

Author Comment

by:allanau20
Comment Utility
Thanks for all your help!
0
 
LVL 5

Author Comment

by:allanau20
Comment Utility
splitting points ..
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now