Schultz9999
Schultz9999

Reputation: 8936

T-SQL: how to output/print types of fields/columns returned by a simple SELECT statement?

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?

Upvotes: 1

Views: 648

Answers (1)

gbn
gbn

Reputation: 432521

SELECT ..INTO.. and examine the definition of the new 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.

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:

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.

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

Upvotes: 2

Related Questions