views:

570

answers:

2

I want to get a list of result sets and columns that i can expect from a SP. I have been able to get at the parameters, script... but i don't know where to get at the result sets and column names.

using Microsoft.SqlServer.Management.Smo;

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["mydbconn"].ConnectionString))
        {
            conn.Open();
            Server sv = new Server(new Microsoft.SqlServer.Management.Common.ServerConnection(conn));
            Database db = sv.Databases["mydb"];

            foreach (StoredProcedure sp in db.StoredProcedures)
            {
                string[] columns = sp.??????
            }

        }

Is there a way to get at the columns? I'm ultimately trying to write a code generator to automate my writing data access objects. Thanks SO!

EDIT: "Another solution is if you can get at the value of ScalarResult, you may be able to convert it into something useful that you can derive the columns from." : Any ideas how to get at that?

+4  A: 

That is a known difficult area. Some systems try this by executing the stored procedure with SET FMTONLY ON enabled, but that has a range of problems (i.e. it will still execute some code, which can be bad). It is also notoriously hard to get the schema for non-trivial queries (for example, where different branches do different selects).

If you know that the procedures are side-effect-free, and don't SELECT in branches, then try the SET FMTONLY ON trick - but at least understand the possible impact.

For queries, in some ways table-valued-functions (UDFs) can be more practical, since the schema is formalised.

Marc Gravell
thanks for pointing me in the right direction, looks like a hopeless case though, the SP i'm dealing with do execute code)
rizzle
Some of the SP are just selects, but they have required parameters, i'm guessing that i won't be able to exec the SP without paramters even with the FMTONLY ON?
rizzle
The other option would be to begin a tran pass in some bogus params and then roll it back at the end, also far from ideal, but i think its usually better than set fmtonly which borks with temp tables
Sam Saffron
@sambo99 - the problem is, you can't rollback some operations (e-mail, for example).
Marc Gravell
Hmm, there are no emails sent out by the SP, anything else i should look out for?
rizzle
+2  A: 

After browsing the StoredProcedure Members page (http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.storedprocedure_members.aspx) on MSDN, I see two possible solutions.

One, you can use GetTextBody to get the actual script for the stored procedure. After that you can somehow parse the stored procedures. Assuming they were created and can be modified by you or a member of your development team (assuming you have one) then this can be a little easier. If not, then it will get really messy, really fast and won't be 100%... so I don't REALLY recomend that.

Another solution is if you can get at the value of ScalarResult, you may be able to convert it into something useful that you can derive the columns from.

Edit: If you want to get just the parameters for an SP, that would be relatively easy to do. There is (according to the previous link) a collection called Parameters that you can access.

Dalin Seivewright
how would i get at scalarresult, it's protected?
rizzle