Solved

Using Stored Procedures in IF ELSE Clause

Posted on 2007-11-18
6
2,676 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 43 total points
ID: 20308690
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
ID: 20308727
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
ID: 20308766
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
ID: 20310716
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

739 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