We have a SQL query that pulls a large number of fields from many tables/views from a database. We need to put a spec together for integration with a 3rd party, what is the quickest way to compile the data types of the result set?
Clarifications:
You can get the MySQL table columns data type with the help of “information_schema. columns”. SELECT DATA_TYPE from INFORMATION_SCHEMA. COLUMNS where table_schema = 'yourDatabaseName' and table_name = 'yourTableName'.
Use TYPE_NAME() to Get the Name of a Data Type in SQL Server In SQL Server, you can use the TYPE_NAME() function to return the name of a data type, based on its ID. This can be useful when querying a system view such as sys. columns that returns the type's ID but not its name.
Once you connect to a database in SSMS, you can view these data types by navigating to Programmability-> Types->System Data Types.
In general for an existing table or view you'd want to use the system catalogs or INFORMATION_SCHEMA.COLUMNS
, but if it's an arbitrary query you're looking at it's a bit more difficult.
I would use the sys.dm_exec_describe_first_result_set
dynamic management view. Often, the biggest pain is having to single-quote escape your query.
DECLARE @tsql nvarchar(max) = N'SELECT YourFields FROM YourQuery';
SELECT *
FROM sys.dm_exec_describe_first_result_set(@tsql, null, 0);
See the documentation for more information.
Alternatively, you can use the sp_describe_first_result_set
stored procedure and get basically the same results. (The dynamic management view is nice because you can more easily filter with a WHERE or sort with an ORDER BY.)
DECLARE @tsql nvarchar(max) = N'SELECT YourFields FROM YourQuery';
EXEC sp_describe_first_result_set @tsql, null, 0;
Again, see the doc for more info.
You might use few quick SQL statements to view result column type, by using temp table.
Temp tables is a little better then a view, as they are connection-local scope and will be cleared once disconnect.
All you need is inject few keyword as follow
SELECT
TOP 0 -- to speed up without access data
your,original,columns
INTO #T -- temp table magic
FROM originalTablesJoins
Order by anything
exec tempdb.sys.sp_columns #T
drop table #T
or;
SELECT TOP 0 *
INTO #T
FROM (
select your,original,columns from originalTablesJoins -- remove order by if any
) x
exec tempdb.sys.sp_columns #T
drop table #T
Note: inspired by View schema of resultset in SQL Server Management Studio
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With