views:

1060

answers:

6

Is there an application , which can parse a given set of stored procedures (SQL Server 2000) and gets all tables and associated columns that are being used in it. The stored procedure can have tables from different databases.

Output should be like TableA columnA columnC columnD

TableB columnE columnF columnG

I have written an small application using regular expression but its not 100% reliable.

+1  A: 

Not a concrete solution - but a line of thought.

Considered sysdepends as a potential solution - but it is notoriously unreliable at containing all the dependent object information.

but how about something Lex/Yacc derived? There are a few commercial parsers e.g.

http://www.sqlparser.com/download.php

Not looked for open-source implementation but I think I would look for that route. Here's how I started my search:

http://www.google.com/search?hl=en&q=sql+lex+yacc+parse

BNF syntax for ANSI SQL can be found here:

http://savage.net.au/SQL/

With a lex implementation of choice, this seems a relatively straight forward engineering problem from here. (albeit with some heavylifting if you want to support MS SQL extensions)

stephbu
http://www.sqlparser.com/download.php Looks great but cannot buy it. Need some sort of Open source or freeware.
rsapru
stephbu
A: 

You could call the stored procedures programmatically (in a development environment) and get the resulting columns. Maybe you have a naming convention in order to avoid calling insert and update procedure. You'll have to find way to set the right parameters too.

Note: I think a 100% reliable solution is technically impossible, because of the way stored procedure (can) work.

Look at this example:

[...]
@MyDate datetime

AS

    IF (day(@MyDate) = 1)
    BEGIN
        SELECT * FROM MyFirstTable
        RETURN
    END

    IF (@MyDate > getdate())
        SELECT MyID, MyText FROM MySecondTable WHERE ADate > @MyDate
    ELSE
        EXEC Other_StoredProcedure @MyType, @MyDate

So there are two problems: the resulting columns could differ and you'll have to follow recursively other stored procedures.

splattne
The parsing of output has way too many flaws to be viable. Agree with the general note tho' - assuming that you'd parse all explicitly declared proc's there is still potential to implicitly invoke procedures through through dynamic SQL strings - which again you'd have to parse etc.
stephbu
Sorry, maybe I was not clear. I don't mean "parsing" the output, but getting the resulting columns in a normal way, simply calling the stored procedure. I'll edit my answer.
splattne
Ok, maybe I've misunderstood the question. But it was not clear for me, what rsapru means by "tell me what all tables and columns within that table are being used.".
splattne
Sorry about my explanation, i just wanted application to get all the table and associated columns as output
rsapru
A: 

You can use SHOWPLAN_ALL setting and parse the output.

Sunny
Is a good suggestion, but it does not work for me.
rsapru
please define "does not work". or better yet - define what works :)
Sunny
Why it did not work for me is because most of the stored procedures that i am working on , i do not have all the DB they refer to.and SHOWPLAN_ALL seems to work only if you have all the DB present.Its a kind of analysis work that i need to do.
rsapru
Why mark it as the answer if it doesn't work then?
stephbu
A: 

sp_depends should help

no this doesn't work for me tried that out
rsapru
A: 

Decided to create an small application using Regex to satisfy my current needs.

Thank you all for your responses.

rsapru
A: 

sp_depends Worked perfectly for me. It showed what table or SP might be affected by my changes

DonP
This works a lot better now (SQL Server 2008). The Q is tagged SQL Server 2000 though where dependencies are very ropey.
Martin Smith
Sorry, didn't see that
DonP