Solved

Using Stored Procedures in IF ELSE Clause

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Error in WHERE Clause 5 42
Using Aggregate Functions to Count 3 34
SQL - SP needs a little help 9 25
SSRS Dynamic Column Names - Last 8 Weeks 3 17
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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

863 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

19 Experts available now in Live!

Get 1:1 Help Now