17

So, not sure what is happening. But I have stored procedure and it keeps disappearing out of my DB in SQL 2k.

I can add it again and then try to execute it from my web app and i get an exception saying the stored procedure cant be found. So then ill go back to management and refresh and its gone again !?!

here is the config for the stored proc:

set ANSI_NULLS OFF
set QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[USP_Equipment_Delete]
    @EquipmentID int
AS

DELETE FROM [dbo].[Equipment]
WHERE
    [EquipmentID] = @EquipmentID

None of my other stored procedure disappear. This is the only one. I have easily 100 in there. They all use the same SQLHelper class. This one just keeps disappearing!!!??!!

Any help or suggestions are appreciated!

Thanks so much!

1
  • 3
    Have you checked your SQL Server logs, or tried running the SQL Server profiler? May 13, 2009 at 15:35

9 Answers 9

35

You were creating this or another stored proc and at the end of your code, maybe after a comment, where you did not see it you have a drop of this proc.

Take a look at your db using:

select syo.name
from syscomments syc
    join sysobjects syo on
        syo.id = syc.id
where syc.[text] like '%DROP PROC%'
10
  • 1
    I fixed the select. Run it and see. What happens is that you create proc 'b', but in your query window at the bottom you have a drop of this proc. That drop happens every time proc 'b' is run.
    – Peter
    May 13, 2009 at 16:34
  • +1 The query basically searches for a stored procedure that contains a DROP statement. Great idea, run it!
    – Andomar
    May 13, 2009 at 16:46
  • DUDE thank you! that worked!!!! It had a drop procedure in it. you helped me so much. im gonna update that and we will see what happens now. THANKS SO MUCH!
    – Gabe
    May 13, 2009 at 17:07
  • Talk about psychic debugging! I'm gonna save this somewhere. Btw don't forget to vote this answer up in addition to accepting it!
    – Andomar
    May 13, 2009 at 17:58
  • 2
    Great fix. At first I wasn't sure what was up either. It tells you what PROC has the drop statement. In my case the prior PROC did not have a closing "GO" and therefore was running into my IF EXISTS and dropping the next PROC.
    – rball
    Jan 13, 2010 at 2:17
13

I had the same problem and I just fixed it:

In the script file it was missing the "GO" statement between the end of the stored procedure and the beginning of the next "IF EXIST THEN DROP" statement.

So what happened was that the drop statement was getting appended to the end of whatever stored procedure was above it in the script. So when the software ran the stored procedure it would drop whatever stored procedure was below it in the script.

It seems so obvious to us now but didn't make any sense at the time. We found it running the SQL profiler against a customer's database that was having the problem in the field.

1
  • Son of a bitch. Yeah I found the proc with the missing GO. Cheers :)
    – James
    Mar 28, 2012 at 2:42
4

Are you using the correct database?

Try

using [database name]

prior to executing your stored procedure, just to make sure.

2

Do you have a CREATE PROCEDURE anywhere? You can't ALTER a procedure if it doesn't exist.

2
  • Yes, I created it with the following... CREATE PROCEDURE [dbo].USP_Equipment_Delete @EquipmentID int AS DELETE FROM [dbo].[Equipment] WHERE [EquipmentID] = @EquipmentID GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS OFF GO
    – Gabe
    May 13, 2009 at 15:51
  • Another thought... do you have anything in your database scripts that drops a procedure before you create it? And if so, is that piece of code on one of your other stored procedures dropping this procedure instead? May 13, 2009 at 16:21
2

Perhaps the code to access the stored procedure is using a different context other than dbo. Make sure to add dbo.USP_Equipment_Delete to the code using it.

1

I was facing the problem that all Stored Procedures with a create statement disappeared from the database after execution.

The Solution was: The database user should have the rights to drop,create and alter on the database in which the "Stored Procedures" are going to be created.

0

Perhaps there's a job thats restoring an old backup periodically?

1
  • ill double check that, i dont think that is happening because it would also kill my other additions. let me verify that tho
    – Gabe
    May 13, 2009 at 16:02
0

Check if the "Initial Catalog" in your connection string is set to the correct database.

Put the database in single user mode (and make sure you're the single user) and check if the procedure still disappears every hour?

2
  • The initial string is correct. The database is the correct one. When I go into Enterprise Manager and browse the stored procedures its missing. I then add it again. In no less than an hour, I refresh and its gone again.
    – Gabe
    May 13, 2009 at 15:57
  • 1
    Did you make an snide remark about the system administrator's wife recently? :)
    – Andomar
    May 13, 2009 at 16:08
-1

If it's there, then this query must return a record:

SELECT * FROM sysobjects 
WHERE id = OBJECT_ID('USP_Equipment_Delete') 
      AND OBJECTPROPERTY(id, N'IsProcedure') = 1
3
  • 1
    -1 This searches for objects named 'USP_Equipment_delete'; what good will that do?
    – Andomar
    May 13, 2009 at 17:07
  • it searches for IDs, not for names: an object with an ID equals to the ID of the stored procedure USP_Equiment_Delete
    – eKek0
    May 13, 2009 at 17:09
  • Yeah, I think I understand. Did you know object_id('spname') returns null if the stored procedure doesn't exist? The -1 is because I can't see the relation to the question.
    – Andomar
    May 13, 2009 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.