Solved

Check and Create UDF

Posted on 2009-07-01
8
379 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
ID: 24759935
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
ID: 24760080
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
ID: 24760699
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 5

Author Comment

by:allanau20
ID: 24765482
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
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24765556
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
ID: 24765622
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
ID: 24765624
Thanks for all your help!
0
 
LVL 5

Author Comment

by:allanau20
ID: 24765642
splitting points ..
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sql query 12 72
Change variables in SQL table 6 87
SQL Server 2012 express 24 37
Is there any Easy way to copy CSV to SQL Server using C# 3 77
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

17 Experts available now in Live!

Get 1:1 Help Now