SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 SQL Query Help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

LearningSQLKid
Starting Member

Hong Kong
46 Posts

Posted - 12/07/2012 :  00:14:57  Show Profile  Reply with Quote
Hi Guys

I'm stuck in writting a sql query. Please help me out.

I have a tabble which records the dependencies of each object

For Example

TableName ObjectType Dependent OnTable
---------------------------------------------------------
Table1 Table NULL Table1
Table2 Table NULL Table1
Table3 Table NULL Table1

Sp1 Storedproc Yes Table1
Sp1 Storedproc Yes Table2
Sp1 Storedproc Yes Table3
Sp1 Storedproc Yes View1
Sp1 Storedproc Yes View2


I want to write a query to find dependent of each Object. I need something recursive. Please help


Thanks in advance

Select Knowledge from LearningProcess

bandi
Flowing Fount of Yak Knowledge

India
2202 Posts

Posted - 12/07/2012 :  00:43:17  Show Profile  Reply with Quote
Can you provide expected result?

--
Chandu
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2202 Posts

Posted - 12/07/2012 :  01:45:09  Show Profile  Reply with Quote
This one?

DECLARE @dep TABLE(TableName varchar(10), ObjectType varchar(30), [Dependent] varchar(10), OnTable varchar(10))
INSERT INTO @dep
SELECT 'Table1', 'Table', NULL, 'Table1' union all
SELECT 'Table2', 'Table', NULL,  'Table1' union all
SELECT 'Table3', 'Table', NULL, 'Table1' union all
SELECT 'Sp1', 'Storedproc', 'Yes', 'Table1' union all
SELECT 'Sp1', 'Storedproc', 'Yes', 'Table2' union all
SELECT 'Sp1', 'Storedproc', 'Yes', 'Table3' union all
SELECT 'Sp1', 'Storedproc', 'Yes', 'View1' union all
SELECT 'Sp1', 'Storedproc', 'Yes', 'View2'

SELECT tablename, STUFF((SELECT ',' + OnTable FROM @dep d2 WHERE d1.TableName = d2.TableName FOR XML PATH('')),1,1, '') AS depObjects
FROM @dep d1
WHERE Tablename != OnTable
GROUP BY tablename

OUTPUT:
tablename	depObjects
Sp1	       Table1,Table2,Table3,View1,View2
Table2	       Table1
Table3	       Table1


--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 12/07/2012 :  07:59:28  Show Profile  Reply with Quote
see

http://visakhm.blogspot.in/2010/01/finding-cross-server-cross-db-object.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.22 seconds. Powered By: Snitz Forums 2000