?
Solved

Using Stored Procedures in IF ELSE Clause

Posted on 2007-11-18
6
Medium Priority
?
2,684 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 172 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 164 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 164 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Suggested Courses

770 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