{"id":85774,"date":"2019-12-03T18:41:04","date_gmt":"2019-12-03T18:41:04","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=85774"},"modified":"2026-05-08T10:26:06","modified_gmt":"2026-05-08T10:26:06","slug":"the-gloop-an-easier-way-of-managing-sql-server-documentation","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/other\/the-gloop-an-easier-way-of-managing-sql-server-documentation\/","title":{"rendered":"Managing SQL Server Documentation as JSON: Per-Object Extracts, MongoDB Tooling, and Collective Editing"},"content":{"rendered":"<b>A standard way to document a SQL Server database is using extended properties (metadata attached directly to database objects via sp_addextendedproperty). An alternative approach &#8211; which this article calls &#8216;the Gloop&#8217; &#8211; is to extract each object&#8217;s metadata as an individual JSON file and manage the collection outside the database: per-table JSON files, per-stored-procedure JSON files, per-index JSON files, etc. <\/b>\n\n<b>The collection can then be edited in bulk using MongoDB-compatible tooling like Studio 3T, queried with MongoDB-style expressions, and version-controlled alongside the schema source code. <\/b>\n\n<b>When to choose this over extended properties: when you need bulk editing of documentation across many objects, when documentation needs to live in source control alongside schema migrations, when cross-object queries over documentation are useful (for example, &#8216;find all columns where the description mentions PII&#8217;), or when the documentation has structure (nested notes, links, author history) that doesn&#8217;t fit extended-property free text cleanly. <\/b>\n\n<b>This article walks through the PowerShell extraction scripts, the MongoDB-import workflow in Studio 3T, and the workflow patterns that make the approach practical.<\/b>\n\nHere, in this blog, I\u2019m continuing a theme that I started in a previous blog, \u2018<a href=\"https:\/\/www.red-gate.com\/simple-talk\/blogs\/whats-in-that-database-getting-information-about-routines\/\">What\u2019s in that database? Getting information about routines<\/a>\u2019.\n\nIn that blog, I just wanted to provide a few examples of extracting metadata from SQL Server into Powershell and hinting about why one might want to do it. I\u2019ll now show how to save the details of the metadata of your database, including tables and routines, in JSON files. Then I\u2019ll demonstrate how to change and add to the descriptions of database objects and saving them to the database.\n\nMetadata extract files are handy for documentation, study, cataloguing and change-tracking. This type of file supplements source because it can record configuration, permissions, dependencies and documentation much more clearly. It is a good way of making a start with documenting your database.\n\nHere is a sample of a json metadata file (from AdventureWorks 2016). It was generated using GloopCollectionOfObjects.sql that is <a href=\"https:\/\/github.com\/Phil-Factor\/TheGloopSQLServerDatabaseDocumenter\">here in Github<\/a>, and is being viewed in JSONBuddy. I use this format of JSON, a collection of documents representing SQL Server base objects (no parent objects) when I need to read the contents into MongoDB. The term \u2018Gloop\u2019 refers to a large query that, you\u2019d have thought, would be better off as a procedure. Here is a typical sample of the output.\n\n<img loading=\"lazy\" decoding=\"async\" width=\"509\" height=\"706\" class=\"wp-image-85775\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/12\/word-image-18.png\" \/>\n\nOk. Those descriptions make a huge difference to readability. I want to be able to edit them in JSON and save them back to the database. OK. We can do that. I use <a href=\"https:\/\/github.com\/Phil-Factor\/TheGloopSQLServerDatabaseDocumenter\/blob\/master\/ParseJSONMetadataToUpdateTheDocumentation.sql\">#ParseJSONMetadataToUpdateTheDocumentation<\/a> which is in the GitHub folder. It is a temporary stored procedure. We can either use this from a SQL batch or call it in SQL using PowerShell.\n\nWithout wishing to change our AdventureWorks file, We\u2019ll open up a sample file called \u2018Customers\u2019 and add some descriptions. In this next screendump, I\u2019m adding a description to a table called Customer.note\n\n<img loading=\"lazy\" decoding=\"async\" width=\"545\" height=\"513\" class=\"wp-image-85776\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/12\/word-image-19.png\" \/>\n\nThen we save the file and execute this code.\n<pre class=\"theme:ssms2012-simple-talk font-size:13 line-height:13 nums-toggle:false wrap:true lang:tsql decode:true \">USE customers\nDECLARE @JSON NVARCHAR(MAX);\nSELECT @JSON = BulkColumn\n  FROM\n  OpenRowset(BULK 'PathToTheData\\customers.json', SINGLE_NCLOB)\n  AS MyJSONFile;\nDECLARE @howManyChanges INT;\nEXECUTE #ParseJSONMetadataToUpdateTheDocumentation @JSON, @howManyChanges OUTPUT;\nSELECT Convert(VARCHAR(5),@howManyChanges)+ ' descriptions were either changed or added'\n<\/pre>\n<img loading=\"lazy\" decoding=\"async\" width=\"268\" height=\"70\" class=\"wp-image-85777\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/12\/word-image-20.png\" \/>\n\nWhy 2? I added a description to a column as well.\n\n\u2026 and we can check to make sure that the right thing got changed.\n\n<img loading=\"lazy\" decoding=\"async\" width=\"778\" height=\"214\" class=\"wp-image-85778\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/12\/word-image-21.png\" \/>\n\nIf we delete all the extended properties and then re-run the batch we get this\n\n<img loading=\"lazy\" decoding=\"async\" width=\"279\" height=\"65\" class=\"wp-image-85779\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/12\/word-image-22.png\" \/>\n\nPhew, they are all back in there.\n\nWhat\u2019s the best way to execute and write out the SQL Gloop query to get the metadata extract in the first place? It can be done in SQL, but once you\u2019ve decided that you need to work on a whole collection of servers and their databases, or you have a regular database chore to do that involves saving to files, it is worth considering doing the chore in SQL, but<a href=\"https:\/\/github.com\/Phil-Factor\/TheGloopSQLServerDatabaseDocumenter\/blob\/master\/RunSQLScript.ps1\"> using a PowerShell task<\/a> that can be scheduled. I&#8217;ve included the code for this on Github\u00a0called<a href=\"https:\/\/github.com\/Phil-Factor\/TheGloopSQLServerDatabaseDocumenter\/blob\/master\/RunSQLScript.ps1\"> RunSQLScript.ps1<\/a>\n\nIn my previous Blog, I introduced the idea of getting metadata about your database using SQL, and saving the contents into a directory, but I didn\u2019t really go into the details about how you run such code. I also mentioned that, if you are running the same task on a number of databases, there is no shame attached to running a large query to extract the metadata from your databases. I referred to this as a \u2018Gloop\u2019. The reason for doing so is that you can run it on a number of databases but leave no trace of utility procedures on them, not even a temporary stored procedure. <a href=\"https:\/\/github.com\/Phil-Factor\/TheGloopSQLServerDatabaseDocumenter\/blob\/master\/GloopCollectionOfObjects.sql\">GloopCollectionOfObjects.sql<\/a> is one of these and I\u2019ve added others to the Github site that were written for other purposes.\n<h1>Developing other uses for Metadata extracts<\/h1>\nThere are several good uses for metadata extracts of database objects and their associated columns, indexes, parameters and return values. The main reason I have for wanting to do this is to see what\u2019s been properly documented in the database and where something is missing, adding it. There are plenty of other uses. What you collect depends on the nature and purpose of your task. I\u2019ve added some sample Gloop queries to deliver different formats of JSON files. Beware, though, that you can\u2019t save the documentation without altering the SQL code in the temporary procedure that shreds it into a relational table.\n\nWhatever use that one puts these metadata extracts to, one thing always happens: When you are looking at metadata in another format than the build script, then some things, mostly mistakes and omissions, that you have just never noticed before will now stick out clearly. It seems to help to get a different view of your database.\n\nWe must also decide on the format we want for our data. If we are predominately using it for documentation, then readability is important. For doing comparisons or keeping a record of database changes in source control, maybe you want something that can be read by input routines more easily. Although ordered arrays are legal JSON, they aren\u2019t easy to produce in SQL Server because SQL Server\u2019s JSON library is geared to produce key\/value pairs. Tables have names that translate easily to keys since the schema\/name combination is unique. The same is true of columns and parameters. The natural way of recording these might be to have an array of schemas, each of which have arrays of table objects using their name as a key. However, they are ordered arrays of objects. It isn\u2019t a major difficulty because we can do what SQL Server does, and put the name as a value assigned to a \u2018name\u2019 key. It just looks clunky if you are reading the JSON. However, it makes it easier to query in MongoDB, and to read into relational table format.\n<h2>Editing JSON-based metadata With Studio3T<\/h2>\nWith Studio3T, you can just import each file as a collection into a database. I use a MongoDB database Called<strong> SQLServerMetadata<\/strong>, and import each database as a collection under its SQL Server name. This provides me with a separate collection for each database, with a document representing a base object such as a Function, Table, Procedure or View. This allows me to edit each object individually and save it back to the collection. Then I can export it back out.\n\n<img loading=\"lazy\" decoding=\"async\" width=\"654\" height=\"395\" class=\"wp-image-85780\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/12\/word-image-23.png\" \/>\n\nHere, I\u2019m just beginning to document the table itself. I can edit the documentation for each table individually because each base object (Function, Table, Procedure, Rule, View or Default) is a document in MongoDB terms and I can, with Studio3T, edit each document separately and have it checked instantly as I save the edits. This means I can even edit the documentation for procedures and functions, their parameters and columns (table-valued functions and views have them- it is helpful to your team members to document them)\n\nHaving made my changes, I then save them back to the file. I do it via Studio3T\u2019s collection-export facilities. I use\n\n<img loading=\"lazy\" decoding=\"async\" width=\"546\" height=\"200\" class=\"wp-image-85781\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/12\/word-image-24.png\" \/>\n\n\u2026 and on the next page in the wizard \u2026\n\n<img loading=\"lazy\" decoding=\"async\" width=\"489\" height=\"296\" class=\"wp-image-85782\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/12\/word-image-25.png\" \/>\n\nI can even paste individual documents from Studio3T into SQL and see that I\u2019ve made all the necessary changes. Unfortunately, MongoDB inserts surrogate primary keys in a form of \u2018extended JSON\u2019 that isn\u2019t compatible. You need to nick them out. To do this you can use a Regex. In a file, or in SSMS, use \u2026\n\u2018&#8221;_id&#8221;.+\u2019\n\u2026as the regex expression (without the single bracket delimiters!), and a blank replacement text\n\nYou can then inspect the results using the SQL that that is used within the <a href=\"https:\/\/github.com\/Phil-Factor\/TheGloopSQLServerDatabaseDocumenter\/blob\/master\/ParseJSONMetadataToUpdateTheDocumentation.sql\">ParseJSONMetadataToUpdateTheDocumentation.sql on Github<\/a>.\n<pre class=\"theme:ssms2012-simple-talk font-size:13 line-height:13 nums-toggle:false wrap:true lang:tsql decode:true\">DECLARE @JSON NVARCHAR(MAX);\nSELECT @JSON = BulkColumn\n  FROM\n  OpenRowset(BULK 'PathToTheData\\customers.json', SINGLE_NCLOB)\n  AS MyJSONFile;\n\nDROP TABLE IF EXISTS #TheObjects;\nCREATE TABLE #TheObjects\n  (\n  Name sysname NOT NULL,\n  Type NVARCHAR(30) NOT NULL,\n  Description NVARCHAR(3750) NULL,\n  ParentName sysname NULL,\n  [Contains] NVARCHAR(MAX) NULL\n  );\n\nINSERT INTO #TheObjects (Name, Type, Description, ParentName, [Contains])\n  SELECT BaseObjects.Name, BaseObjects.Type, BaseObjects.Description, NULL,\n    [Contains]\n    FROM\n    OpenJson(@JSON)\n    WITH\n      (\n      Name NVARCHAR(80) '$.Name', Type NVARCHAR(80) '$.Type',\n      Parent NVARCHAR(80) '$.Parent',\n      Description NVARCHAR(MAX) '$.Description',\n      [Contains] NVARCHAR(MAX) '$.contains' AS JSON\n      ) AS BaseObjects;\n\nINSERT INTO #TheObjects (Name, Type, Description, ParentName, [Contains])\n  SELECT objvalues.Name, obj.[Key] AS Type, objvalues.Description,\n    #TheObjects.Name AS ParentName, NULL AS [contains]\n    FROM #TheObjects\n      OUTER APPLY OpenJson(#TheObjects.[Contains]) AS child\n      OUTER APPLY OpenJson(child.Value) AS obj\n      OUTER APPLY\n    OpenJson(obj.Value)\n    WITH (Name NVARCHAR(80) '$.Name', Description NVARCHAR(MAX) '$.Description') AS objvalues;\n\n\nDROP TABLE IF EXISTS #EPParentObjects;\nCREATE TABLE #EPParentObjects\n  (\n  TheOneToDo INT IDENTITY(1, 1),\n  level0_type VARCHAR(128) NULL,\n  level0_Name sysname NULL,\n  level1_type VARCHAR(128) NULL,\n  level1_Name sysname NULL,\n  level2_type VARCHAR(128) NULL,\n  level2_Name sysname NULL,\n  [Description] NVARCHAR(3750),\n  );\n\nINSERT INTO #EPParentObjects\n  (level0_type, level0_Name, level1_type, level1_Name, level2_type,\nlevel2_Name, Description)\n  SELECT 'schema' AS level0_type, ParseName(Name, 2) AS level0_Name,\n      CASE WHEN Type LIKE '%FUNCTION%' THEN 'FUNCTION'\n        WHEN Type LIKE '%TABLE%' THEN 'TABLE'\n        WHEN Type LIKE '%PROCEDURE%' THEN 'PROCEDURE'\n        WHEN Type LIKE '%RULE%' THEN 'RULE'\n        WHEN Type LIKE '%VIEW%' THEN 'VIEW'\n        WHEN Type LIKE '%DEFAULT%' THEN 'DEFAULT'\n        WHEN Type LIKE '%AGGREGATE%' THEN 'AGGREGATE'\n        WHEN Type LIKE '%LOGICAL FILE NAME%' THEN 'LOGICAL FILE NAME'\n        WHEN Type LIKE '%QUEUE%' THEN 'QUEUE'\n        WHEN Type LIKE '%RULE%' THEN 'RULE'\n        WHEN Type LIKE '%SYNONYM%' THEN 'SYNONYM'\n        WHEN Type LIKE '%TYPE%' THEN 'TYPE'\n        WHEN Type LIKE '%XML SCHEMA COLLECTION%' THEN 'XML SCHEMA COLLECTION' \n\t    ELSE'UNKNOWN' \n\t  END AS level1_type,\n    ParseName(Name, 1) AS level1_Name, NULL AS level2_type,\n    NULL AS level2_Name, Description\n    FROM #TheObjects\n    WHERE ParentName IS NULL;\n\nINSERT INTO #EPParentObjects\n  (level0_type, level0_Name, level1_type, level1_Name, level2_type,\nlevel2_Name, Description)\n  SELECT level0_type, level0_Name, level1_type, level1_Name,\n      CASE WHEN Type LIKE '%COLUMN%' THEN 'COLUMN'\n        WHEN Type LIKE '%CONSTRAINT%' THEN 'CONSTRAINT'\n        WHEN Type LIKE '%EVENT NOTIFICATION%' THEN 'EVENT NOTIFICATION'\n        WHEN Type LIKE '%INDEX%' THEN 'INDEX'\n        WHEN Type LIKE '%PARAMETER%' THEN 'PARAMETER'\n        WHEN Type LIKE '%TRIGGER%' THEN 'TRIGGER' \n\t\tELSE 'UNKNOWN' \n\t  END AS Level2_type,\n    #TheObjects.Name AS Level2_name, #TheObjects.Description\n    FROM #EPParentObjects\n      INNER JOIN #TheObjects\n        ON level1_Name = ParseName(ParentName, 1) \n\t\t  AND level0_Name =ParseName(ParentName, 2);\n\nSELECT * FROM #EPParentObjects AS EPO<\/pre>\nIf you think that this table looks suspiciously like the parameters you\u2019d need to use for the various SQL system procedures that you\u2019d need to use, you\u2019re right. That is the underlying purpose of the code!\n\n<img loading=\"lazy\" decoding=\"async\" width=\"873\" height=\"213\" class=\"wp-image-85783\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/12\/word-image-26.png\" \/>\n\nHmm. Looks good.\n\nStudio3T has the option of putting the resulting JSON for a database on the clipboard so you can even do the job without having to think of a way of reading the JSON into SQL Server. However, I&#8217;ve already demonstrated an easy way of doing that.\n\nSo with\u00a0<a href=\"https:\/\/github.com\/Phil-Factor\/TheGloopSQLServerDatabaseDocumenter\/blob\/master\/ParseJSONMetadataToUpdateTheDocumentation.sql\">ParseJSONMetadataToUpdateTheDocumentation.sql,<\/a> along with the Gloop, you have the means to get a report of your database in JSON, edit it to add or change the documentation and then read it back into the database to update it.\n<h1>Conclusions<\/h1>\nThis is an approach to documentation that suits me fine. I can see a great deal about each table or routine while I check through the documentation. I can add information or make changes and save the results back to the development database.\u00a0 If you know of a better way, let me know! There are a number of different ways that you can do this, so all you need to do is to edit the Gloop to be closer to what you need. Make sure that, if you change the structure of the JSON produced by the Gloop, you make the equivalent changes to the SQL that shreds the JSON into a relational format for updating your documentation.\n\nJust as a thought. If you put the JSON for your documentation in source control, you can then use it for inserting the documentation into a database build. It is a lot easier than the conventional way. It means that, if you keep it up to date, it also allows you to track the changes in the database even if you are using a migrations-scripts-first approach.\n\n\n<section id=\"faq\" class=\"faq-block my-5xl\">\n    <h2>FAQs<\/h2>\n\n            <h3 class=\"mt-4xl\">1. What are the options for documenting a SQL Server database?<\/h3>\n        <div class=\"faq-answer\">\n            <p>Five main approaches: (1) Extended properties &#8211; metadata attached directly to database objects via sp_addextendedproperty; simple but limited to free-text values per property and hard to edit in bulk. (2) Per-object JSON or YAML files extracted from the database and managed outside it (the approach in this article); flexible, version-controllable, bulk-editable, queryable with JSON or document-database tooling. (3) Structured headers in schema source &#8211; documentation blocks embedded in CREATE TABLE or CREATE PROCEDURE scripts; keeps documentation next to the code but doesn&#8217;t survive if the source is lost. (4) External documentation tools like Redgate SQL Doc or ApexSQL Doc that generate documentation from schema plus extended properties. (5) Wiki-style documentation in Confluence, Notion, or similar; most flexible but most prone to drift from the actual schema. The right choice depends on editing patterns, version control discipline, and team tooling.<\/p>\n        <\/div>\n            <h3 class=\"mt-4xl\">2. How do I document a SQL Server database using JSON files?<\/h3>\n        <div class=\"faq-answer\">\n            <p>Write a PowerShell (or .NET) script that iterates over the database&#8217;s objects &#8211; tables, views, stored procedures, functions, indexes, columns &#8211; and extracts each object&#8217;s metadata plus any existing documentation (from extended properties or inferred from naming) into a JSON file per object. Structure the JSON with fields for name, type, description, author, version history, related objects, and any custom fields relevant to your team. Store the JSON files in a version-controlled folder alongside your schema source. For editing, use any JSON editor, or import the files into a MongoDB-style collection and use document-database tooling (like Studio 3T) for bulk queries and edits. For publishing, template-render the JSON into HTML or markdown documentation.<\/p>\n        <\/div>\n            <h3 class=\"mt-4xl\">3. What is Studio 3T and how does it help with SQL Server documentation?<\/h3>\n        <div class=\"faq-answer\">\n            <p>Studio 3T (formerly Studio Robomongo) is a GUI tool for MongoDB databases. For SQL Server documentation managed as JSON files, Studio 3T is useful because MongoDB&#8217;s native data format is JSON documents in collections &#8211; so importing your per-object JSON files as a MongoDB collection gives you MongoDB-style querying, bulk editing, and export over the collection. Queries like &#8216;find all columns where the description mentions PII&#8217; or &#8216;list all stored procedures without a description&#8217; become trivial MongoDB aggregations. The tool doesn&#8217;t integrate with SQL Server directly; it&#8217;s a complementary workflow where JSON is the interchange format between SQL Server (source of metadata) and MongoDB (bulk-edit workspace).<\/p>\n        <\/div>\n            <h3 class=\"mt-4xl\">4. Should I use extended properties or JSON files for SQL Server documentation?<\/h3>\n        <div class=\"faq-answer\">\n            <p>Extended properties are simpler and live inside the database &#8211; good for basic descriptions, use-case notes, and anything that should stay with the object through backup\/restore. JSON files (external) are better when documentation has structure beyond free text, when bulk editing across many objects is routine, when documentation needs to live in source control, or when cross-object queries over documentation are useful. Many teams use both: extended properties for concise, stable descriptions that belong with the object; JSON files for richer metadata (author history, related objects, detailed use-case notes, compliance tags). The two can coexist &#8211; the JSON extract can include extended-property content as one field among many.<\/p>\n        <\/div>\n    <\/section>\n","protected":false},"excerpt":{"rendered":"<p>An alternative approach to SQL Server database documentation: extract each object&#8217;s metadata (tables, routines, columns, indexes) as individual JSON files, then manage the collection using MongoDB-compatible tools like Studio 3T for bulk editing, querying, and cross-object analysis. Includes the PowerShell extraction scripts and a discussion of when this approach beats extended-properties-in-database documentation.&hellip;<\/p>\n","protected":false},"author":154613,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2,1],"tags":[],"coauthors":[6813],"class_list":["post-85774","post","type-post","status-publish","format-standard","hentry","category-other","category-uncategorized"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/85774","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/154613"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=85774"}],"version-history":[{"count":4,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/85774\/revisions"}],"predecessor-version":[{"id":110414,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/85774\/revisions\/110414"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=85774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=85774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=85774"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=85774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}