From 4dba7ef8b389754389a6377fed9177485c718c60 Mon Sep 17 00:00:00 2001 From: joyfullservice Date: Wed, 2 Aug 2023 17:07:00 -0500 Subject: [PATCH] Look up last modified date for SQL objects Extend this query to reflect the date of any dependent objects. (Returns the max modified date of the object itself, or any dependent object.) This way if you modify a trigger on the SQL server, it will perform an export of the (parent) table object, which includes the trigger DDL. #415 --- .../queries/qryMsSqlServerObjects.bas | 42 +++++++++++-------- .../queries/qryMsSqlServerObjects.sql | 20 +++++++-- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/Version Control.accda.src/queries/qryMsSqlServerObjects.bas b/Version Control.accda.src/queries/qryMsSqlServerObjects.bas index 4b33127c..6d8eb7e3 100644 --- a/Version Control.accda.src/queries/qryMsSqlServerObjects.bas +++ b/Version Control.accda.src/queries/qryMsSqlServerObjects.bas @@ -1,21 +1,27 @@ -dbMemo "SQL" ="SELECT o.[name],\015\012 SCHEMA_NAME(o.[schema_id]) AS [schema],\015\012 o" - ".modify_date AS last_modified, c.modify_date AS test,\015\012\011o.type_desc,\015" - "\012\011CASE o.[type]\015\012\011\011WHEN 'V' THEN 'views'\015\012\011\011WHEN '" - "U' THEN 'tables'\015\012\011\011WHEN 'IT' THEN 'tables'\011\011-- Internal table" - "s\015\012\011\011WHEN 'TR' THEN 'tables'\015\012\011\011WHEN 'P' THEN 'procedure" - "s'\015\012\011\011WHEN 'FN' THEN 'functions'\011-- Scalar function\015\012\011\011" - "WHEN 'IF' THEN 'functions'\011-- Inline table valued function\015\012\011\011WHE" - "N 'TF' THEN 'functions'\011-- Table valued function\015\012\011\011WHEN 'TT' THE" - "N 'types'\011\011-- Type table\015\012\011\011WHEN 'SO' THEN 'sequences'\011-- S" - "equence object\015\012\011\011WHEN 'SN' THEN 'synonymns'\011-- Synonyms\015\012\011" - "\011ELSE 'unknown'\015\012\011END as folder,\015\012\011o.[type] AS object_type\015" - "\012 -- ,*\015\012FROM sys.objects o\015\012-- Join child objects to find gre" - "atest last_modified date\015\012LEFT JOIN \015\012sys.objects c ON c.object_id =" - " o.parent_object_id\015\012WHERE 1 = 1\015\012--AND o.type = 'TT'\015\012AND o.p" - "arent_object_id = 0\015\012AND o.[type] NOT IN (\015\012\011 'S' -- System Tabl" - "es\015\012\011,'SQ' -- Service queues\015\012\011,'TR' -- Triggers saved from t" - "ables\015\012\011,'IT' -- Internal tables\015\012\011,'TT' -- Type tables\015\012" - "\011,'SO' -- Sequence objects\015\012\011)\015\012" +dbMemo "SQL" ="-- On Microsoft SQL Server, return a listing of all parent level objects\015\012" + "SELECT o.[name],\015\012 SCHEMA_NAME(o.[schema_id]) AS [schema],\015\012\011C" + "ASE\015\012\011\011-- Return the most recent modfied date of the object or any d" + "ependent object\015\012\011\011WHEN isnull(c.max_modified, 0) > o.modify_date TH" + "EN c.max_modified\015\012\011\011ELSE o.modify_date\015\012\011END AS last_modif" + "ied,\015\012\011o.type_desc,\015\012\011CASE o.[type]\015\012\011\011WHEN 'V' TH" + "EN 'views'\015\012\011\011WHEN 'U' THEN 'tables'\015\012\011\011WHEN 'IT' THEN '" + "tables'\011\011-- Internal tables\015\012\011\011WHEN 'TR' THEN 'tables'\015\012" + "\011\011WHEN 'P' THEN 'procedures'\015\012\011\011WHEN 'FN' THEN 'functions'\011" + "-- Scalar function\015\012\011\011WHEN 'IF' THEN 'functions'\011-- Inline table " + "valued function\015\012\011\011WHEN 'TF' THEN 'functions'\011-- Table valued fun" + "ction\015\012\011\011WHEN 'TT' THEN 'types'\011\011-- Type table\015\012\011\011" + "WHEN 'SO' THEN 'sequences'\011-- Sequence object\015\012\011\011WHEN 'SN' THEN '" + "synonymns'\011-- Synonyms\015\012\011\011ELSE 'unknown'\015\012\011END as folder" + ",\015\012\011o.[type] AS object_type\015\012 -- ,*\015\012FROM sys.objects o\015" + "\012LEFT JOIN \015\012\011-- Get most recent modified date of any child object\015" + "\012\011(select \015\012\011\011parent_object_id,\015\012\011\011max(modify_date" + ") AS max_modified\015\012\011\011from sys.objects\015\012\011\011WHERE parent_ob" + "ject_id > 0\015\012\011\011GROUP BY parent_object_id\015\012\011)AS c \015\012\011" + "ON c.parent_object_id = o.object_id\015\012WHERE 1 = 1\015\012--AND o.type = 'TT" + "'\015\012AND o.parent_object_id = 0\015\012AND o.[type] NOT IN (\015\012\011 'S'" + " -- System Tables\015\012\011,'SQ' -- Service queues\015\012\011,'TR' -- Trigg" + "ers saved from tables\015\012\011,'IT' -- Internal tables\015\012\011,'TT' -- " + "Type tables\015\012\011,'SO' -- Sequence objects\015\012\011)\015\012" dbMemo "Connect" ="ODBC;" dbBoolean "ReturnsRecords" ="-1" dbInteger "ODBCTimeout" ="60" diff --git a/Version Control.accda.src/queries/qryMsSqlServerObjects.sql b/Version Control.accda.src/queries/qryMsSqlServerObjects.sql index a2c6e26d..e7020076 100644 --- a/Version Control.accda.src/queries/qryMsSqlServerObjects.sql +++ b/Version Control.accda.src/queries/qryMsSqlServerObjects.sql @@ -1,6 +1,11 @@ -SELECT o.[name], +-- On Microsoft SQL Server, return a listing of all parent level objects +SELECT o.[name], SCHEMA_NAME(o.[schema_id]) AS [schema], - o.modify_date AS last_modified, c.modify_date AS test, + CASE + -- Return the most recent modfied date of the object or any dependent object + WHEN isnull(c.max_modified, 0) > o.modify_date THEN c.max_modified + ELSE o.modify_date + END AS last_modified, o.type_desc, CASE o.[type] WHEN 'V' THEN 'views' @@ -19,9 +24,16 @@ o.[type] AS object_type -- ,* FROM sys.objects o --- Join child objects to find greatest last_modified date LEFT JOIN -sys.objects c ON c.object_id = o.parent_object_id + -- Get most recent modified date of any child object + (select + parent_object_id, + max(modify_date) AS max_modified + from sys.objects + WHERE parent_object_id > 0 + GROUP BY parent_object_id + )AS c + ON c.parent_object_id = o.object_id WHERE 1 = 1 --AND o.type = 'TT' AND o.parent_object_id = 0