-- CETAS metadata SELECT TOP (50) ext.object_id , ext.name , schema_name(ext.schema_id) [schema_name] , ext.type_desc , ext.location , ext.data_source_id , ext.file_format_id , ext.max_column_id_used , ext.uses_ansi_nulls , ext.create_date , ext.modify_date FROM sys.external_tables ext
-- CETAS' metadata via sys.all_objects SELECT * FROM sys.all_objects WHERE object_id = object_id('<schema_name>.<CETAS name>')
The data source and file format can be retrieved via the sys.external_data_sources and sys.external_file_formats DMVs. Moreover, it's useful to include the logic into a view, like the one below:
The view can be used then for further queries, for example checking the CETAS created or modified starting with a given date:
The definitions for multiple CETAS can be exported from the source database in one step via the Object Explorer Details >> Tables >> External tables >> (select CETAS) >> Script Table as >> ... .
-- drop view --DROP VIEW IF EXISTS dbo.vAdminExternalTables -- create view CREATE VIEW dbo.vAdminExternalTables AS -- external tables - metadata SELECT ext.object_id , sch.name + '.' + ext.name [unique_identifier] , sch.name [schema] , ext.name [object] , ext.type_desc [type] , ext.max_column_id_used , ext.location , eds.name data_source , eff.name file_format , ext.create_date , ext.modify_date FROM sys.external_tables ext JOIN sys.schemas sch ON ext.schema_id = sch.schema_id JOIN sys.external_data_sources eds ON ext.data_source_id = eds.data_source_id JOIN sys.external_file_formats eff ON ext.file_format_id = eff.file_format_id -- testing the view SELECT top 10 * FROM dbo.vAdminExternalTables
-- external tables created after a certain date SELECT * FROM dbo.vAdminExternalTables ext WHERE ext.create_date >= '20230101' OR ext.modify_date >= '20230101';
Or, when the CETAS are deployed from one environment to another, one can compare the datasets returned by the same view between environments, something like in the below query:
-- comparison external tables metadata between two databases SELECT * FROM ( SELECT * FROM <test_database>.dbo.vAdminExternalTables WHERE [Schema] = '<schema_name>' ) PRD FULL OUTER JOIN ( SELECT * FROM <prod_database>.dbo.vAdminExternalTables WHERE [Schema] = '<schema_name>' ) UAT ON PRD.[unique_identifier] = UAT.[unique_identifier] -- WHERE PRD.[unique_identifier] IS NULL OR UAT.[unique_identifier] IS NULL
Happy coding!
No comments:
Post a Comment