|
| 1 | +/* |
| 2 | +<documentation> |
| 3 | + <summary>Create JOIN query between multiple tables dynamically.</summary> |
| 4 | + <returns>SELECT statement from input table with INNER JOINS for all tables having foreign consttraints with input table.</returns> |
| 5 | + <issues>Does not properly generate alias for case with multiply foreign keys for one table</issues> |
| 6 | + <author>Konstantin Taranov</author> |
| 7 | + <created>2019-04-24</created> |
| 8 | + <modified>2019-04-25 by Konstantin Taranov</modified> |
| 9 | + <version>1.0</version> |
| 10 | + <sourceLink>https://github.com/ktaranov/sqlserver-kit/blob/master/Scripts/Get_Identity_Column_Information.sql</sourceLink> |
| 11 | +</documentation> |
| 12 | +*/ |
| 13 | + |
| 14 | + |
| 15 | +IF OBJECT_ID('tempdb..#IdentityInfo', 'U') IS NOT NULL DROP TABLE #IdentityInfo; |
| 16 | +IF OBJECT_ID('tempdb..#IdentityStats', 'U') IS NOT NULL DROP TABLE #IdentityStats; |
| 17 | +GO |
| 18 | + |
| 19 | +DECLARE @tsql nvarchar(max) = N''; |
| 20 | + |
| 21 | +SELECT IC.object_id AS ObjectID |
| 22 | + , SS.name AS SchemaName |
| 23 | + , TN.name AS TableName |
| 24 | + , IC.name AS ColumnName |
| 25 | + , TYPE_NAME(IC.system_type_id) AS ColumnDataType |
| 26 | + , DTM.DataTypeMinValue |
| 27 | + , DTM.DataTypeMaxValue |
| 28 | + , IC.seed_value AS IdentitySeed |
| 29 | + , IC.increment_value AS IdentityIncrement |
| 30 | + , IC.last_value AS LastIdentityValue |
| 31 | + , DPS.NumberOfRows |
| 32 | + INTO #IdentityInfo |
| 33 | + FROM sys.identity_columns AS IC |
| 34 | + INNER JOIN sys.tables AS TN ON IC.object_id = TN.object_id |
| 35 | + INNER JOIN sys.schemas AS SS ON TN.schema_id = SS.schema_id |
| 36 | + INNER JOIN ( |
| 37 | + VALUES |
| 38 | + ('tinyint', 0, 255), |
| 39 | + ('smallint', -32768, 32767), |
| 40 | + ('int', -2147483648, 2147483647), |
| 41 | + ('bigint', -9223372036854775808, 9223372036854775807) |
| 42 | + ) AS DTM(DataType, DataTypeMinValue, DataTypeMaxValue) ON TYPE_NAME(IC.system_type_id) = DTM.DataType |
| 43 | + INNER JOIN ( |
| 44 | + SELECT object_id |
| 45 | + , SCHEMA_NAME(object_id) AS SchemaName |
| 46 | + , OBJECT_NAME(object_id) AS TableName |
| 47 | + , SUM(row_count) AS NumberOfRows |
| 48 | + FROM sys.dm_db_partition_stats |
| 49 | + WHERE index_id < 2 |
| 50 | + GROUP BY object_id, SCHEMA_NAME(object_id), OBJECT_NAME(object_id) |
| 51 | + ) AS DPS ON IC.object_id = DPS.object_id; |
| 52 | + |
| 53 | +IF (SELECT COUNT(1) FROM #IdentityInfo) = 0 |
| 54 | +THROW 50001, 'You do not have any table with identity column in current database.', 1; |
| 55 | + |
| 56 | +CREATE TABLE #IdentityStats( |
| 57 | + ObjectID bigint |
| 58 | + , SchemaName sysname |
| 59 | + , TableName sysname |
| 60 | + , IdentityColumnMinValue bigint |
| 61 | + , IdentityColumnMaxValue bigint |
| 62 | + , IdentityColumnAvgValue bigint |
| 63 | +); |
| 64 | + |
| 65 | +SELECT @tsql = @tsql + N'INSERT INTO #IdentityStats SELECT ' + |
| 66 | + CAST(t.ObjectID AS nvarchar(30)) + N' AS ObjectID, ''' + |
| 67 | + t.SchemaName + N''' AS SchemaName, ''' + t.TableName + N''' AS TableName' + |
| 68 | + N', MIN([' + t.ColumnName + N']) AS IdentityColumnMinValue' + |
| 69 | + N', MAX([' + t.ColumnName + N']) AS IdentityColumnMaxValue' + |
| 70 | + N', AVG(CAST([' + t.ColumnName + N'] AS BIGINT)) AS IdentityColumnAvgValue' + |
| 71 | + N' FROM ' + QUOTENAME(t.SchemaName) + N'.' + QUOTENAME(t.TableName) + N' (NOLOCK);' + CHAR(13) |
| 72 | +FROM #IdentityInfo AS t; |
| 73 | + |
| 74 | +/* |
| 75 | + // PRINT truncate strings to 4000 char limit, below we using XML trick to solve this issue |
| 76 | +*/ |
| 77 | +SELECT @tsql FOR XML PATH(''); |
| 78 | + |
| 79 | +EXEC sp_executesql @tsql; |
| 80 | + |
| 81 | +SELECT ii.* |
| 82 | + , ids.IdentityColumnMaxValue |
| 83 | + , ids.IdentityColumnMinValue |
| 84 | + , ids.IdentityColumnAvgValue |
| 85 | + , (CONVERT(decimal(15,2), CONVERT(bigint, ii.LastIdentityValue) * 100 / ii.DataTypeMaxValue)) AS ReachMaxValuePercent |
| 86 | +FROM #IdentityInfo AS ii |
| 87 | +INNER JOIN #IdentityStats AS ids ON ii.ObjectID = ids.ObjectID |
| 88 | +ORDER BY ReachMaxValuePercent DESC; |
0 commit comments