Get column names/types returned from a stored procedure

后端 未结 3 951
粉色の甜心
粉色の甜心 2020-12-15 22:01

Is there a way via metadata (Information_Schema, perhaps?) to get a list of the columns a sproc will return? I\'m trying to automate some code generation and that would help

3条回答
  •  佛祖请我去吃肉
    2020-12-15 22:23

    Unless you're prepared to parse the contents of ROUTINE_DEFINITION in INFORMATION_SCHEMA.ROUTINES, then your best bet will be to execute the procedures, and read the column information from the records returned.

    In .NET you can do this by reading the results of the stored procedure into a DataTable and querying the Columns property.

    The reason there's no easy way to do this is a stored procedure could potentially return different result sets based on the parameters. There's no fixed result set format like there is with user defined functions.

    Edit

    As mentioned in the other answer, you will need to use SET FMTONLY ON to ensure no data is returned. There are some situations where SET FMTONLY won't work, e.g. when using #temp tables in your stored procedures, but there is a workaround.

提交回复
热议问题