在运行时显示 SQL Server 查询结果的数据类型和列大小

发布于 2024-07-25 08:56:13 字数 967 浏览 6 评论 0原文

有没有办法运行查询,然后让 SQL Server Management Studio 或 sqlcmd 或其他工具简单地显示收到的每列的数据类型和大小。

似乎必须存在此信息才能在服务器和客户端之间进行数据传输。 如果可以显示的话对我很有帮助。

一点背景: 我问这个问题的原因是因为我必须与无数遗留存储过程交互,每个存储过程有 50 到 5000 多行代码。 我不想尝试遵循进出临时表、其他过程、字符串连接的 eval 语句等的神秘逻辑流。 我希望不了解实施情况,只了解它们工作时会发生什么。 不幸的是,遵循逻辑流似乎是弄清楚到底返回什么的唯一方法,而无需尝试推断 Management Studio Studio 或 .net 中的本机类型的数据字符串表示的实际类型。

澄清一下:我并不是问如何辨别表格的类型或类似的静态内容。 我很确定像 sp_help 这样的东西不会帮助我。 我问如何判断我所得到的 sql server 类型(即 varchar(25)、int...)。 此外,无法更改存储过程的实现,因此请在您的解决方案中考虑这一点。 我真的希望有一个命令我在某个地方错过了。 非常感谢大家。

更新 我想我真正要问的是当结果集源自使用临时表的查询时如何获取结果集的架构。 我知道这是不可能的,但觉得这个结论没有多大意义,因为毕竟数据正在传输。 下面是一个可能导致问题的存储过程的示例。

CREATE PROCEDURE [dbo].[IReturnATempTable]
AS

Create table #TempTable 
( 
    MyMysteryColumn char(50)
)

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


select TOP 50 * FROM #TempTable 

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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

勿忘心安 2024-08-01 08:56:14

有点遥远,尝试用 SET FMTONLY ON(或关闭)来搞乱。 根据 BOL 的说法,这“仅向客户端返回元数据。可用于测试响应的格式,而无需实际运行查询。” 我怀疑这将包含您正在寻找的内容,因为 BCP 使用它。 (我在调试一些非常奇怪的 BCP 问题时偶然发现了这个设置。)

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

究竟谁懂我的在乎 2024-08-01 08:56:14

您能否将另一个选择添加到您的程序中?

如果是这样,您也许可以使用sql_variant_property函数来做到这一点。

Declare @Param Int
Set @Param = 30

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

我在这个上发布了该内容 问题。

我问的是如何判断sql是什么
服务器类型(即 varchar(25)、int...)
是我所得到的

您可以打印出参数的类型、精度(即 25,如果是 VarChar(25))和小数位数。

希望有帮助... :)

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

单调的奢华 2024-08-01 08:56:14

如果您不限于 T-SQL,并且显然您不介意运行 SP(因为 SET FMTONLY ON 并不完全可靠),那么您绝对可以调用使用 SqlDataReader 的 SP(例如 C#)。 然后检查 SqlDataReader 以获取列和数据类型。 您可能还有多个结果集,您也可以从此环境轻松转到下一个结果集。

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.

李不 2024-08-01 08:56:14

这段代码应该可以解决你的问题。 它返回没有记录的仅模式数据集。 您可以使用此数据集来查询列的数据类型和任何其他元数据。 稍后,如果您愿意,可以通过创建 SqlDataAdapter 并调用它的 Fill 方法 (IDataAdapter.Fill) 来用记录填充 DataSet。

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

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 2024-08-01 08:56:14

我知道这是一个老问题,我通过 SqlDataAdapter.FillSchema 以及具有临时表的存储过程。 不幸的是,两个问题都没有公认的答案,并且提出的答案都无法解决我的问题。

为简洁起见,如果您使用的是 SQL Server 2012 或更高版本,则在大多数情况下使用以下内置函数都可以:

但是,在某些情况下,这些函数不会提供任何有用的输出。 就我而言,问题与上面链接的问题更相似,因此,我相信该问题下的解决方案得到更合适的回答。 我的答案可以在此处找到。

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.

熊抱啵儿 2024-08-01 08:56:13

对于根据参数返回不同结果集的存储过程,您将采取什么措施?

在任何情况下,您都可以配置 SqlDataAdapter.SelectCommand 以及必要的参数,然后调用 FillSchema 方法。 假设可以确定架构,您将获得一个配置了正确的列名称和类型以及一些约束的 DataTable。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文