Display DataType and Size of Column from SQL Server Query Results at Runtime

我与影子孤独终老i 提交于 2019-12-04 03:28:52

What will you do about stored procedures which return different result sets based on their parameters?

In any case, you can configure a SqlDataAdapter.SelectCommand, along with the necessary parameters, then call the FillSchema method. Assuming that the schema can be determined, you'll get a DataTable configured with correct column names and types, and some constraints.

A bit of a long shot, try messing around with SET FMTONLY ON (or off). According to BOL, this "Returns only metadata to the client. Can be used to test the format of the response without actually running the query." I suspect that this will inlcude what you're looking for, as BCP uses this. (I stumbled across this setting when debugging some very oddball BCP problems.)

Could you append another select to your procedure?

If so you might be able to do it by using the sql_variant_property function.

Declare @Param Int
Set @Param = 30

Select sql_variant_property(@Param, 'BaseType')
Select sql_variant_property(@Param, 'Precision')
Select sql_variant_property(@Param, 'Scale')

I posted that on this question.

I am asking how to tell what the sql server types (ie varchar(25), int...) are of what I have been given

You could then print out the type, precision (i.e. 25 if its VarChar(25)), and the scale of the parameter.

Hope that helps... :)

If you are not limited to T-SQL, and obviously you don't mind running the SPs (because SET FMTONLY ON isn't fully reliable), you definitely CAN call the SPs from, say C#, using a SqlDataReader. Then inspect the SqlDataReader to get the columns and the data types. You might also have multiple result sets, you you can also go to the next result set easily from this environment.

This code should fix you up. It returns a schema only dataset with no records. You can use this Dataset to query the columns' DataType and any other metadata. Later, if you wish, you can populate the DataSet with records by creating a SqlDataAdapter and calling it's Fill method (IDataAdapter.Fill).

private static DataSet FillSchema(SqlConnection conn)
{
    DataSet ds = new DataSet();
    using (SqlCommand formatCommand = new SqlCommand("SET FMTONLY ON;", conn))
    {
        formatCommand.ExecuteNonQuery();
        SqlDataAdapter formatAdapter = new SqlDataAdapter(formatCommand);
        formatAdapter.FillSchema(ds, SchemaType.Source);
        formatCommand.CommandText = "SET FMTONLY OFF;";
        formatCommand.ExecuteNonQuery();
        formatAdapter.Dispose();
    }
    return ds;
}

I know this is an old question, I found it through a link from SqlDataAdapter.FillSchema with stored procedure that has temporary table. Unfortunately, neither question had an accepted answer, and none of the proposed answers were able to resolve my issue.

For the sake of brevity, if you are using SQL Server 2012 or later, using the following built-in functions will work in most situations:

However, there are some cases in which these functions will not provide any useful output. In my case, the problem was more similar to the question linked above and therefore, I believe the solution is more appropriately answered under that question. My answer can be found here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!