I am trying to get some data relevant to a stored procedure (or function) back from a database using .Net.
The first thing I need to be able to do, is get the stored proc from the database and turn it into string format.
The information I need is: The return set of columns, tables used within the SP, Stored Procedures called from the SP. The only way of doing this at the moment that I can think of, is though parsing the text and looking for keyword matches. Is there a better way of doing this?
Any ideas?
Assuming you’re aware that the returned tables may be dependant on the inputs to the function, so there won’t always be a single output for a given stored procedure, but…
To get a list of stored procedures you can call
sp_help. You can then get the source of the stored procedure usingsp_helptext(with@objnamebeing the name of the stored procedure).To find the referenced tables, columns, et al, you’d be wanting to parse the SQL script – you could do this yourself, or there are more established projects such as ANTLR that may simplify matters for you.