T-SQL:如何输出/打印简单SELECT语句返回的字段/列类型?

时间:2022-09-30 19:56:36

While using SqlDataReader, it's necessary to know the types of the fields returned in order to call appropriate GetXXX method. So is it possible to output this info in Sql Management Studio?

在使用SqlDataReader时,有必要知道返回的字段类型,以便调用适当的GetXXX方法。那么可以在Sql Management Studio中输出这些信息吗?

1 个解决方案

#1


2  

SELECT ..INTO.. and examine the definition of the new tabke

SELECT ..INTO ..并检查新tabke的定义

The WHERE 1 = 0 bit will by shortcircuited here so it should be quick. Of course, you'll need to add your own conditions.

WHERE 1 = 0位将在此处短路,因此它应该很快。当然,您需要添加自己的条件。

SELECT
 ...
INTO dbo.TempTable
FROM ...
WHERE 1 = 0
GO
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'TempTable'
GO
DROP TABLE dbo.TempTable

If you have a single table in the FROM:

如果FROM中有一个表:

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'SourceTable'

Which method depends on complexity. For example, a calculation on decimal column changes precision and scale. Or varchar processing can change length or char to varchar.

哪种方法取决于复杂性。例如,十进制列的计算会更改精度和比例。或者varchar处理可以将length或char更改为varchar。

You'd be running the SQL anyway to make sure it's OK before calling it the client code...

无论如何你都要运行SQL以确保它在调用客户端代码之前没问题...

#1


2  

SELECT ..INTO.. and examine the definition of the new tabke

SELECT ..INTO ..并检查新tabke的定义

The WHERE 1 = 0 bit will by shortcircuited here so it should be quick. Of course, you'll need to add your own conditions.

WHERE 1 = 0位将在此处短路,因此它应该很快。当然,您需要添加自己的条件。

SELECT
 ...
INTO dbo.TempTable
FROM ...
WHERE 1 = 0
GO
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'TempTable'
GO
DROP TABLE dbo.TempTable

If you have a single table in the FROM:

如果FROM中有一个表:

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'SourceTable'

Which method depends on complexity. For example, a calculation on decimal column changes precision and scale. Or varchar processing can change length or char to varchar.

哪种方法取决于复杂性。例如,十进制列的计算会更改精度和比例。或者varchar处理可以将length或char更改为varchar。

You'd be running the SQL anyway to make sure it's OK before calling it the client code...

无论如何你都要运行SQL以确保它在调用客户端代码之前没问题...