Solved

Using Stored Procedures in IF ELSE Clause

Posted on 2007-11-18
6
2,663 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 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

792 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