How to order objects based on dependencies

Hi,

I have a list of objects that will be modified, and I would like to write a query to order by dependencies in a way that it would not fail because one depends on the other. 

Here is what I have so far:

CREATE TABLE #objects(
 name NVARCHAR(100)
);

--List of objects
INSERT INTO #objects 
select 'item'
union
select 'order'
union
select 'order_detail';

SELECT name FROM (
SELECT DISTINCT referenced_entity_name, ISNULL(referenced_id,-1) AS referenced_id, name
FROM sys.sql_expression_dependencies
RIGHT OUTER JOIN #objects
ON name = OBJECT_NAME(referenced_id)) AS a
ORDER BY 
CASE WHEN a.referenced_id = '-1' THEN 2 ELSE 1 END, referenced_id ASC

Any thoughts on how to write this query?
March 30th, 2015 1:34am

http://dimantdatabasesolutions.blogspot.co.il/2010/07/find-dependency-task-again.html
Free Windows Admin Tool Kit Click here and download it now
March 30th, 2015 2:33am

do you mean this?

http://visakhm.blogspot.in/2011/11/recursive-delete-from-parent-child.html

March 30th, 2015 2:44am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics