Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Tables referenced by a stored proc (2000) Expand / Collapse
Author
Message
Posted Friday, January 2, 2009 6:09 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, January 26, 2011 1:32 PM
Points: 146, Visits: 327
David,

Perhaps I need to clarify - it listed a table the SP does not use at all.

Best,
Doug
Post #628747
Posted Friday, January 2, 2009 6:12 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, January 26, 2011 1:32 PM
Points: 146, Visits: 327
David,

What I think happens it that you code finds a word in the comments that is a valid table in the database eg
-- Patients is linked to the Accounts table

Where Accounts is a table, but it is not referenced in SQL code.

Doug
Post #628748
Posted Friday, January 2, 2009 6:16 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, January 26, 2011 1:32 PM
Points: 146, Visits: 327
Carlo,

No go with that call - it still only show an SP that my Stored Procedure calls, not any tables.

It says the oType is 16, if that is any help.

Doug
Post #628750
Posted Friday, January 2, 2009 6:19 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, January 26, 2011 1:32 PM
Points: 146, Visits: 327
Carlo,

I altered th SP - your routine still only lists a dependent SP, it lists not tables. SQL 2005.

Doug
Post #628752
Posted Friday, January 2, 2009 6:23 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Monday, December 15, 2014 9:36 AM
Points: 652, Visits: 1,901
Hi Doug,

I confirm (as I mentioned in the article) that table names which are commented will be picked up.

The script does a purely textual search on the stored procedure (the text being stored in syscomments.) It doesn't at all consider the context of the matches (whether it is in comments etc.)

Regards,

David.
Post #628755
Posted Friday, January 2, 2009 6:42 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, January 26, 2011 1:32 PM
Points: 146, Visits: 327
David,

Missed that in your text. My bad.:D

Couldn't you double parse by writing all the text you encounter prior to -- to a temp table and then process that?

Ah - but then you'd also have to handle /* and */ - oh well - perhaps a second article.;)

Doug
Post #628772
Posted Friday, January 2, 2009 1:43 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 4:21 PM
Points: 7,928, Visits: 9,653
[b]David McKinney (1/2/2009)

ANY SOLUTION based on sysdepends isn't going to work.



The obvious solution based on sysdepends - running alterproc for the SP with no changes before using sysdepends - works better than your proposed solution: it avoids listing tables that are mentioned only in comments.

Your solution suffers from a rather obvious defect: it doesn't handle views; that can be fixed rather trivially; avoiding comments is rather harder; handling dependencies arising from access to other databases or by the many indirect access mechanisms is quite hard too

If one really wants to understand dependencies between SPs and tables, one has to consider (i) tables in the current database accessed by the SP; (ii) tables in other databases accessed by the SP; (iii) tables (in this or other databases) involved in views accessed by this SP; (iv) tables (in this or other databases) used by SPs, or UDFs called by the SP; and (v) tables accessed (including indirect access through views or further triggers of SP or Function calls) by triggers fired by actions of this SP. Your current script is useful in a single database world with no triggers, views or functions and where no SP calls any other SPs, but not much use elsewhere.

It's quite (but not very) hard to write something that does the job properly in SQLS 2000; it's a bit easier in SQLS 2005 (recursive queries help).



Tom
Post #629084
Posted Friday, January 2, 2009 1:47 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, January 26, 2011 1:32 PM
Points: 146, Visits: 327
Tom,

Saying everyone is wrong really doesn't help us solve this. :)

Give us an example of your solution please.

Doug

Post #629086
Posted Saturday, January 3, 2009 2:23 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 8:51 AM
Points: 2,622, Visits: 2,471
Last idea: follow the rule of KISS (Keep It Simple & Stupid) write store procedure formatting it with the following rule: every command that reference a table should be on one line and should be the only one, e.g.
UPDATE table1
..
SELECT a,b,c
FROM dbo.table2
CROSS JOIN server1.mydb.dbo.TABLE3
FULL JOIN zzzzz
DELETE FROM #kkk
INSERT INTO somedb.dbo.tableX
(
A,b,c
) select x,y,z
FROM ANOTHERTABLE

So, your scanning routine should discard comments and searching only for DML. At last, replace commands with '', trim it and you’ll get the tablename + eventually alias for the table.

Here you are a sample:

create table #a (t varchar(8000))

delete #a
insert #a exec sp_helptext ‘mysp_elab’

update #a set
t = rtrim(ltrim(replace(t,char(9),' ')))

-- write here code to cut out comments
-- ...
-- ...
-- end

delete from #a
WHERE t not like 'delete %'
and t not like 'update %'
and t not like 'insert %'
and t not like 'from %'
and t not like 'JOIN %'

SELECT
ltrim
(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(t,'delete ','')
,'update ','')
,'insert ','')
,'from ','')
,'JOIN ','')
,'INNER ','')
,'LEFT ','')
,'RIGHT ','')
,'FULL ','')
)
FROM #a

With this method you can find also table in quoted dynamic sql.
Post #629238
Posted Monday, January 5, 2009 12:47 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Monday, December 15, 2014 9:36 AM
Points: 652, Visits: 1,901

Your solution suffers from a rather obvious defect: it doesn't handle views


Look at the title...Tables referenced by a stored proc!

It does what it says on the tin....and there's a warning on the tin regarding commented tables.

You've said it's not very hard to do better...I look forward to seeing it.

Best,

David.
Post #629616
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse