Background
Had a good meeting this morning while we try to figure out how to better support our Developers.
One of the issues they brought up was an inability to view the datatype of linked Server tables.
Pictorial
Here is the deepest granularity when we connect to our Linked Server.
Image
Explanation
- We see the following
- Server
- Databases
- Objects
- Tables
- Views
View Columns
Script
Outline
Here are avenues we can explore to view column metadata :-
- sp_columns_ex
- Openquery/sys
- sys.all_columns
- Openquery/informational_schema
- informational_schema.columns
Choices
exp_columns_ex
Syntax
exec sp_columns_ex @table_server , @table_catalog , @table_schema , @table_name
Sample
declare @linkedServer sysname declare @linkedDatabase sysname declare @linkedSchema sysname declare @linkedTable sysname set @linkedServer= 'AWS-JobBuilder' set @linkedDatabase = 'acs_ap' set @linkedSchema = 'dbo' set @linkedTable = 'ap_user' exec sp_columns_ex @table_server = @linkedServer , @table_catalog = @linkedDatabase , @table_schema = @linkedSchema , @table_name = @linkedTable
Output
openquery/sys.*
Syntax
select top 10 * from openquery ( [AWS-JobBuilder] , ' select [server] = serverproperty(''servername'') , [database] = db_name() , [schema] = tblSS.name , [object] = tblSAO.name , [column] = tblSAC.name , [type] = tblST.[name] , tblST.max_length , tblST.is_nullable from sys.schemas tblSS inner join sys.all_objects tblSAO on tblSS.schema_id = tblSAO.schema_id inner join sys.all_columns tblSAC on tblSAO.object_id = tblSAC.object_id inner join sys.types tblST on tblSAC.system_type_id = tblST.system_type_id and tblSAC.user_type_id = tblST.user_type_id ' )
Output
openquery/information_schema.columns
Syntax
select top 10 * from openquery ( [AWS-JobBuilder] , ' select top 100 [server] = serverproperty(''servername'') , [database] = tblSIC.[TABLE_CATALOG] , [schema] = tblSIC.[TABLE_SCHEMA] , [object] = tblSIC.[TABLE_NAME] , [column] = tblSIC.[COLUMN_NAME] , [position] = tblSIC.[ORDINAL_POSITION] , [dataType] = tblSIC.[DATA_TYPE] , [charMaxLength] = tblSIC.[CHARACTER_MAXIMUM_LENGTH] , [charOctetLength] = tblSIC.[CHARACTER_OCTET_LENGTH] from information_schema.columns tblSIC ' )
Output
Summary
Unfortunately, SQL Server Management Studio ( SSMS ) v17.x does not let us view columns on Linked Servers.
To gather column level metadata, one has to write code.