views:

199

answers:

6

Before I delete/replace a stored procedure, I would usually do a string search of the name of the stored procedure in the working project to make sure no unsuspecting code is using it. Is there a better and more reliable way of doing this?

Oh and the code is in c# and i'm using vs2008

+2  A: 

If you have a comprehensive suite of unit tests, delete the SP and run through the tests to see if anything breaks. If you don't have a suite, there's no time like the present to start writing it.

TALlama
+1  A: 

I have this code for searching text on all the triggers (a bit harsh, sorry), maybe u can adapt it for searching on other database objects also:

SELECT Tables.Name TableName,
  Triggers.name TriggerName,
  Triggers.crdate TriggerCreatedDate,
  Comments.Text TriggerText
FROM sysobjects Triggers
 Join sysobjects Tables On Triggers.parent_obj = Tables.id
 Join syscomments Comments On Triggers.id = Comments.id
WHERE
 Triggers.xtype = 'TR'
AND Tables.xtype = 'U'
AND (@ttable_name = '' OR Tables.name = @table_name)
AND Comments.Text LIKE '%' + @search + '%'
ORDER BY Tables.Name, Triggers.name
Jhonny D. Cano -Leftware-
Won't work for long code. The comments.text filed is nvarchar(4000).
gbn
It's wrong, misleading and dangerous to use this code... still gets upvotes...
gbn
+1  A: 

You should search before you drop it, by using the sys.sql_dependencies catalog view:

select object_name(object_id), *
    from sys.sql_dependencies
    where referenced_major_id = object_id('<nameofproceduretobedropped>');

This catches all references to the procedure in other procedures, but it cannot catch dynamic sql dependencies.

With stored procedures though most dependencies are external ie. in the client code.

Remus Rusanu
It's not reliable though, say if you drop and recreate a child stored proc or table.
gbn
This isn't guaranteed to catch procs and triggers that were migrated in a server version upgrade (say you went from sql 2000 to 2005). Those migration tools *will* move even procs that will not compile on the new version. Sample: a past employer I worked at had a terrible time migrating from sql server 6.5 to 7 as 7 was strict with enforcing ' as a string delimiter, and 6.5 accepted ", and about half the triggers and stored procs stopped working for several months after the upgrade. Since then, I've checked and found similar things go wrong, so sys.sql_dependencies isn't 100% all the time.
Tangurena
the only 100% proof way is to unit test the entire application and validate. Searching procedure definition does not work with encrypted procedures. sql_dependencies does not track some corner cases. dynamic sql based on app input may fool both methods.
Remus Rusanu
+1  A: 

Ideally, in your C# code, you only have one method that is (conceptually) "allowed" to call the stored procedure, and that method must live in the data access layer (the only layer allowed to have access to the database connection string).

The ability to actually enforce anything like this at the C# level is pretty lacking. It generally has to be enforced through coding conventions and code review.

At this point, doing a string search through the C# code is probably your only option and I agree it's not a great one. If you can limit your string search to a single C# project, that's great, but not necessarily likely in the wild.

Unit tests are also a decent technique to mitigate some of the risk around situations like this.

Michael Maddox
+3  A: 

or rename the sp instead of deleting it. if there's an unidentified dependency you can quickly get your sp back.

another thing you can do is script out your views, functions, and sps, and search the scripts. again, won't catch dynamic sql, but might catch a few extra references.

Beth
+4  A: 

For SQL Server 2005 and above, works with longer code.

SELECT
    OBJECT_NAME(object_id)
FROM
    sys.sql_modules
WHERE
    definition LIKE '%' + 'mySP' + '%'
gbn