SELECT [s].[name] [SchemaName], [t].[name] [TableName], [c].[name] [ColumnName], [tp].[name], CASE WHEN [tp].[name] IN (‘bigint’, ‘int’, ‘smallint’, ‘tinyint’, ‘real’, ‘float’, ‘bit’, ‘date’, ‘datetime’, ‘geography’, ‘geometry’, ‘hierarchyid’, ‘image’, ‘smallmoney’, ‘money’, ‘ntext’, ‘text’, ‘timestamp’, ‘smalldatetime’, ‘sql_variant’, ‘sysname’, ‘uniqueidentifier’, ‘xml’) THEN [tp].[name] WHEN [tp].[name] IN (‘nchar’, ‘nvarchar’, ‘varbinary’, ‘varchar’ ) AND [c].[max_length] = -1 THEN…
Primary Keys List
This scritp can be used to generate views to UNION ALL rows from all tables within two or more databases with the same schema. Replace $SCHEMA_SOURCE_DB$ with the name of the database you will take the schema from. All databases in the [db] list must have the same schema. Requires…
Check for name of an object in all databases
List Foreign Key Relations
SELECT [ro].[name] [PKtable], [rc].[name] [PKcolumn], [po].[name] [FKtable], [pc].[name] [FKcolumn], [fk].[name] [FKname] FROM [sys].[foreign_keys] [fk] INNER JOIN [sys].[all_objects] [po] ON [po].[object_id] = [fk].[parent_object_id] INNER JOIN [sys].[all_objects] [ro] ON [ro].[object_id] = [fk].[referenced_object_id] INNER JOIN [sys].[foreign_key_columns] [fkc] ON [fkc].[constraint_object_id] = [fk].[object_id] AND [fkc].[parent_object_id] = [fk].[parent_object_id] AND [fkc].[referenced_object_id] = [fk].[referenced_object_id] INNER JOIN [sys].[columns] [pc]…
Compressed Tables List
SELECT CONCAT([s].[name],’.’,[t].[name]) [Table], [fk].[name] [ForeignKey], [c].[name] [Column], [dc].[name] [DefaultConstraint], [dc].[definition] [Default] FROM [sys].[foreign_key_columns] [fkc] INNER JOIN [sys].[foreign_keys] [fk] ON [fkc].[constraint_object_id] = [fk].[object_id] INNER JOIN [sys].[columns] [c] ON [c].[object_id] = [fkc].[parent_object_id] AND [c].[column_id] = [fkc].[parent_column_id] INNER JOIN [sys].[tables] [t] ON [t].[object_id] = [fkc].[parent_object_id] INNER JOIN [sys].[schemas] [s] ON [s].[schema_id] = [t].[schema_id]…