Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 4th, 2004, 10:06 PM
Authorized User
 
Join Date: Jun 2003
Location: , , Japan.
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Check if stored procedure exists.

A basic question, but here's a snippet of a script I've downloaded:

Code:
IF EXISTS (select * from syscomments where id = object_id ('dbo.SPROC_NAME'))
BEGIN
    DROP PROCEDURE dbo.SPROC_NAME
    IF OBJECT_ID('dbo.SPROC_NAME') IS NOT NULL
        PRINT '<<< FAILED DROPPING PROCEDURE dbo.SPROC_NAME >>>'
    ELSE
        PRINT '<<< DROPPED PROCEDURE dbo.SPROC_NAME >>>'
END
Now clearly it's determining if the stored procedure exists, dropping it if it does, and checking that it's been dropped.

My question is why use the "If Exists" test initially to check if the stored procedure exists, but then the OBJECT_ID() function to check that it's been dropped if it did exist?
Reply With Quote
  #2 (permalink)  
Old February 4th, 2004, 10:47 PM
sal sal is offline
Friend of Wrox
 
Join Date: Oct 2003
Location: Clarksville, TN, USA.
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Maybe they are making sure that you have the proper permissions to drop the database. Just because a user can execute the code does not mean that they have the rights to drop objects.

That is the only explanation I can come up with.



Sal
Reply With Quote
  #3 (permalink)  
Old February 5th, 2004, 12:54 AM
Authorized User
 
Join Date: Jun 2003
Location: , , Japan.
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks sal, I appreciate your comment and understand that may be the case. My problem wasn't with why the second test was performed, rather why the two tests are different.

Perhaps I didn't phrase the original question well, so let me try again.

In the first test to see if the SPROC exists it uses the "If Exists" test.
If the SPROC is found and an attempt to delete it is made, the OBJECT_ID() function test is used to see if it still exists after the attempted deletion.
I was wondering why the two tests to see if the SPROC exists are different (especially as the OBJECT_ID function is actually used in the sub query of the "If Exists" test). Couldn't the check of OBJECT_ID('dbo.SPROC_NAME') returning NULL have been used for both tests, or am I missing something obvious here?
There are a number of scripts that use this same syntax in a few places, so it isn't a one off.

Putting it another way, are the results of
Code:
IF EXISTS (select * from syscomments where id = object_id ('dbo.SPROC_NAME'))
and
Code:
IF OBJECT_ID('dbo.SPROC_NAME') IS NOT NULL
likely to be different? And, if they aren't, which is to be preferred and why? Thanks.
Reply With Quote
  #4 (permalink)  
Old February 9th, 2004, 10:37 AM
Authorized User
 
Join Date: Jun 2003
Location: , , Romania.
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts
Default

object_id(sproc_name) will return 1 even if the sproc doesn't exist but there is another object with the sproc_name name. so that's why the first test has to use 'if exists'. object_id is used inside begin - end, where execution shouldn't enter unless there was a sproc called 'dbo.SPROC_NAME'.

the object_id() test is simpler and shorter, and this is why i think it was used inside begin - end.

defiant.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check value while insert in stored procedure Andraw SQL Server 2005 2 August 12th, 2008 09:08 AM
check whether COM object exists EddyT C# 0 June 15th, 2007 04:10 AM
How to check if exists BEFORE the Insert rtr1900 Classic ASP Databases 0 December 20th, 2006 07:04 AM
check first to see if the file exists crmpicco Classic ASP Professional 2 December 1st, 2005 12:34 PM
how to check that node is exists debuajm General .NET 0 June 8th, 2004 02:07 AM



All times are GMT -4. The time now is 05:45 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.