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

百般思念 提交于 2020-01-12 15:37:25

问题


Is there a way to run a query and then have SQL Server management studio or sqlcmd or something simply display the datatype and size of each column as it was received.

Seems like this information must be present for the transmission of the data to occur between the server and the client. It would be very helpful to me if it could be displayed.

A little background: The reason I ask is because I must interface with countless legacy stored procedures with anywhere from 50 to 5000+ lines of code each. I do not want to have to try and follow the cryptic logic flow in and out of temp tables, into other procedures, into string concatenated eval statement and so on. I wish to maintain no knowledge of the implementation, simply what to expect when they work. Unfortunately following the logic flow seems to be the only way to figure out what exactly is being returned without trying to infer what the actual types of the data string representations om management studio studio or from the native type in .net for example.

To clarify: I am not asking about how to tell the types of a table or something static like that. I'm pretty sure something like sp_help will not help me. I am asking how to tell what the sql server types (ie varchar(25), int...) are of what I have been given. Additionally, changing the implementation of the sprocs is not possible so please consider that in your solutions. I am really hoping there is a command I have missed somewhere. Much appreciation to all.

Update I guess what I am really asking is how to get the schema of the result set when the result set originates from a query using a temp table. I understand this to be impossible but don't find much sense with that conclusion because the data is being transmitted after all. Here is an example of a stored procedure that would cause a problem.

CREATE PROCEDURE [dbo].[IReturnATempTable]
AS

Create table #TempTable 
( 
    MyMysteryColumn char(50)
)

INSERT #TempTable (
    MyMysteryColumn
) VALUES ( 
    'Do you know me?' ) 


select TOP 50 * FROM #TempTable 

回答1:


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.




回答2:


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.)




回答3:


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... :)




回答4:


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.




回答5:


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;
}



回答6:


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:

  • sys.dm_exec_describe_first_result_set
  • sys.dm_exec_describe_first_result_set_for_object

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.



来源:https://stackoverflow.com/questions/1028948/display-datatype-and-size-of-column-from-sql-server-query-results-at-runtime

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