Solved

Using Stored Procedures in IF ELSE Clause

Posted on 2007-11-18
6
2,635 Views
Last Modified: 2009-12-16
I have been trying to figure out why this wont work, any suggestions would be much appreciated.

Basically i want to check the DB system for the stored procedure 'sp_BasicSelectStatement' , if iti exists then i want to drop the procedure and recreate it, else i want to create it from new.

Please see code below.

--Check to see if Procedure Already exists
IF EXISTS(SELECT name FROM sysobjects WHERE name = 'sp_BasicSelectStatement' AND type = 'P')
      BEGIN
            PRINT('Drop and Create Procedure as already exists')

            DROP PROCEDURE sp_BasicSelectStatement
            GO
            CREATE PROCEDURE sp_BasicSelectStatement
            AS
            SELECT firstname, lastname, email
            FROM Customer
            GO

      END
ELSE
      BEGIN
            PRINT('Create Procedure as doesnt exist')

            CREATE PROCEDURE sp_BasicSelectStatement
            AS
            SELECT firstname, lastname, email
            FROM Customer
            GO

      END

Many Thanks
Damian
0
Comment
Question by:wizardofoz78
6 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 43 total points
Comment Utility
you cannot DROP/CREATE inside a stored procedure (at least not without dynamic sql.
and for sure, you cannot use GO inside stored procedures.
0
 
LVL 25

Assisted Solution

by:imitchie
imitchie earned 41 total points
Comment Utility
the usual pattern is to test for existence, then drop if it exists.
after that, you are free to create new

btw, using sp_xxx as the name of a stored procedure is really bad...
http://dataglass.blogspot.com/2006/02/best-practices-for-sql-design-patterns.html
if (select objectproperty(object_id('sp_BasicSelectStatement'), N'IsProcedure')) = 1

begin

    PRINT('Drop and Create Procedure as already exists')

    DROP PROCEDURE sp_BasicSelectStatement

end

GO

CREATE PROCEDURE sp_BasicSelectStatement

AS

SELECT firstname, lastname, email

FROM Customer

GO

Open in new window

0
 
LVL 3

Assisted Solution

by:Martin-Smith
Martin-Smith earned 41 total points
Comment Utility
The following will work inside a SP but why do you need to recreate it if it exists already?

--Check to see if Procedure Already exists
IF EXISTS(SELECT name FROM sysobjects WHERE name = 'sp_BasicSelectStatement' AND type = 'P')
      BEGIN
            PRINT('Drop and Create Procedure as already exists')

            DROP PROCEDURE sp_BasicSelectStatement


      END

PRINT('Create Procedure')
exec sp_executesql N'CREATE PROCEDURE sp_BasicSelectStatement AS SELECT firstname, lastname, email FROM Customer'


0
 
LVL 50

Expert Comment

by:Lowfatspread
Comment Utility
ps don't name your procedures with an SP prefix as this is assumed to mean its a system stored procedure and MSSQL searches for it first in the master database ...

use usp   (user sp) or something..

also don't use sysobjects either use the object proerty syntax as demonstrated ...
or the information_schema views in this case information_schema.routines
as sysobjects can change between releases...

hth
 

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

763 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

8 Experts available now in Live!

Get 1:1 Help Now