|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, April 21, 2011 5:47 AM
Points: 0,
Visits: 73
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, November 09, 2012 12:11 PM
Points: 9,
Visits: 52
|
|
You get a 5 star rate from me.. This was just what I was looking for. We just made our database 100% documented, which on the buildserver for deploy builds will generate database documentation (using redgate sql doc).
Now I can use parts of this script to trow an error/warnings when there are new undocumented columns or tables..
However, the primary key check is broken. If I have time today I will take a look at that and post back here.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, March 21, 2013 3:53 PM
Points: 114,
Visits: 916
|
|
This is the way I've done the PK determination:
Join in sysobjects:
FROM sys.columns AS c INNER JOIN sysobjects so ON c.object_id = so.id
Then use this to display the PK flag:
CASE WHEN c.name IN ( SELECT c.name FROM sysindexes i JOIN sysobjects o ON i.id = o.id JOIN sysobjects pk ON i.name = pk.name AND pk.parent_obj = i.id AND pk.xtype = 'PK' JOIN sysindexkeys ik ON i.id = ik.id AND i.indid = ik.indid JOIN syscolumns c ON ik.id = c.id AND ik.colid = c.colid WHERE o.name = so.name) THEN 'Y' ELSE '' END AS 'PK'
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, November 09, 2012 12:11 PM
Points: 9,
Visits: 52
|
|
Ok, this is showing the Primary key columns correctly:
SELECT DISTINCT t.name AS Table_Nme, ept.value AS Table_Desc, c.name AS Column_Nme, st.name + '(' + CASE WHEN c.max_length = - 1 THEN 'max' ELSE CAST(c.max_length AS varchar(100)) END + ')' AS Column_Data_Type, CASE WHEN c.is_nullable = 0 THEN 'False' ELSE 'True' END AS Null_Allowed_Ind, epc.value AS Column_Desc, CASE WHEN dc.definition LIKE '(getdate())' THEN 'Current Date' ELSE dc.definition END AS Column_Default_Value, CASE WHEN PrimaryKeyColumns.CONSTRAINT_NAME IS NOT null THEN 'Yes' ELSE '' END AS Primary_Key_Ind, CASE WHEN t .object_id = fk.parent_object_id AND c.column_id = fk.parent_column_id THEN 'Yes' ELSE '' END AS Foriegn_Key_Ind, CASE WHEN c.is_identity = 1 THEN 'Yes' ELSE '' END AS Identity_Column_Ind, ft.name AS Foreign_Table, c.column_id FROM sys.columns AS c INNER JOIN sys.systypes AS st ON st.xtype = c.user_type_id LEFT OUTER JOIN sys.extended_properties AS epc ON epc.major_id = c.object_id AND epc.minor_id = c.column_id LEFT OUTER JOIN sys.default_constraints AS dc ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id INNER JOIN sys.tables AS t ON c.object_id = t.object_id LEFT OUTER JOIN sys.extended_properties AS ept ON ept.major_id = t.object_id AND ept.minor_id = t.parent_object_id LEFT OUTER JOIN sys.key_constraints AS pk ON t.object_id = pk.parent_object_id LEFT OUTER JOIN sys.foreign_key_columns AS fk ON fk.parent_object_id = c.object_id AND fk.parent_column_id = c.column_id LEFT OUTER JOIN sys.tables AS ft ON fk.referenced_object_id = ft.OBJECT_ID --start added by hjm LEFT OUTER JOIN ( SELECT Tab.TABLE_NAME, Tab.CONSTRAINT_NAME, K.COLUMN_NAME, K.ORDINAL_POSITION FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE K ON Tab.CONSTRAINT_NAME = K.CONSTRAINT_NAME WHERE Tab.CONSTRAINT_TYPE = 'PRIMARY KEY' ) AS PrimaryKeyColumns ON (PrimaryKeyColumns.COLUMN_NAME = c.name AND t.NAME = PrimaryKeyColumns.TABLE_NAME ) --hjm end WHERE (t.name NOT IN ('sysdiagrams', 'DataDictionary')) AND (st.name NOT LIKE '%sysname%') ORDER BY Table_Nme, c.column_id
I think we can make the rest also a bit more readable by using schema information views, but hey. Its working and I really like the integration of the extended properties...
Cheers, HJ Meulekamp
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, October 22, 2012 7:11 AM
Points: 6,
Visits: 104
|
|
Nice Job everyone. I have been working on creating a DD for a new app for a few days (vendor could not provide).
The last blog code with the sub-query is okay, but I have to mention that in our app's db_name.schema.table two elements indicated as PK. This may not occur with every DB, but I will find out why and post.
Kudos to those who extend their talent.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Saturday, April 13, 2013 6:13 PM
Points: 25,
Visits: 340
|
|
I put one together sometime ago but this is by far much better then what I had pieced together, matter of fact this puts mine to shame.
Thank you for sharing.
-- Samson
|
|
|
|