I am writing a DDL script to drop a number of tables but need to identify all dependencies for those tables first. Those dependencies include foreign key constraints, stored procedures, views, etc. Preferably, I want to programmatically script out dropping those dependencies using the system tables/views before dropping the dependent table.
6条答案
按热度按时间xoefb8l81#
This is extremely messy to write from scratch. Have you considered a 3rd party tool like Red-Gate SQL Dependency Tracker ?
hc8w905p2#
sp_depends is not reliable see: Do you depend on sp_depends (no pun intended)
x759pob23#
you could always search through the syscomments table....that might take a while though...
xzv2uavs4#
Could you reference
sysreferences
?That will generate a whole lot of conditional
drop constraint
calls. Should work.4jb9z9bj5#
I know the view is deprecated, but I like it while it works. I wrote a messy proc that is working. I am posting it without prettying it up so it is out there, but feel free to pretty it up and uncomment the stuff I don't use in this version. Making the parameter nullable lets me specify a schema or get all schemas. Omitting the MS_Shipped objects eliminates stuff I didn't build and don't need to track.
I am also keeping the IDs because I can then use this same approach with more recursions. Right now I just want to find out what we can omit when moving to the new server.
The data type function is another lazy-man hack, but I am including it because it makes for easier reading in the output.
Have at it!
Joey Morgan
8i9zcol26#
You can use the sp_depends stored procedure to do this:
USE AdventureWorks GO EXEC sp_depends @objname = N'Sales.Customer' ;
http://msdn.microsoft.com/en-us/library/ms189487(SQL.90).aspx