Skip to content

Commit

Permalink
Look up last modified date for SQL objects
Browse files Browse the repository at this point in the history
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
  • Loading branch information
joyfullservice committed Aug 2, 2023
1 parent e34f47d commit 4dba7ef
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
42 changes: 24 additions & 18 deletions Version Control.accda.src/queries/qryMsSqlServerObjects.bas
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
20 changes: 16 additions & 4 deletions Version Control.accda.src/queries/qryMsSqlServerObjects.sql
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
Expand Down

0 comments on commit 4dba7ef

Please sign in to comment.