codex/payvast-mapping-discovery-20261023
add-index-html-10623938925177053894 arena/094f314c-aram chatgpt-phase1b2-autopilot codex/adr-future-connectors-pi-v1 codex/aram-account-note-source-trace-20261031 codex/aram-actions-node24-maintenance-20261031 codex/aram-analyst-review-workbook-20261031 codex/aram-bs-candidate-statement-20261031 codex/aram-cash-flow-direct-candidate-20261031 codex/aram-clean-reference-year-integrity-20261030 codex/aram-cross-statement-controls-20261031 codex/aram-equity-changes-candidate-20261031 codex/aram-five-statement-cli-20261031 codex/aram-five-statement-integration-20261031 codex/aram-five-statement-orchestrator-20261031 codex/aram-gate3-coverage-target-guard-20261030 codex/aram-gate3-hidden-xlsx-guard-20261030 codex/aram-gate3-integration-validation-20261030 codex/aram-gate3-mapping-change-preview-20261030 codex/aram-gate3-stale-note-guard-20261030 codex/aram-income-statement-candidate-20261031 codex/aram-manifest-referential-integrity-20261031 codex/aram-mapping-source-parser-20261101 codex/aram-multidimensional-mapping-20261101 codex/aram-multistatement-mapping-20261031 codex/aram-pipeline-manifest-schema-20261031 codex/aram-readonly-mapping-coverage-20261031 codex/aram-reconciliation-review-gate-20261031 codex/aram-same-flush-fiscal-lock-20261030 codex/aram-synthetic-windows-demo-20261031 codex/aram-trial-balance-diagnostic-20261031 codex/aram-windows-background-detail-search-20261031 codex/aram-windows-detail-review-20261031 codex/aram-windows-five-statement-pilot-20261031 codex/aram-windows-statement-figures-20261031 codex/gate-2a-pilot-workbook codex/gate-2a-pilot-workbook-6864754244772568979 codex/gate3-authenticated-principal-boundary-20261030 codex/gate3-authored-draft-staging-20261030 codex/gate3-carry-forward-preview-20261023 codex/gate3-draft-staging-audit-20261030 codex/gate3-mapping-batch-freshness-20261030 codex/gate3-mapping-batch-review-20261030 codex/gate3-mapping-core codex/gate3-mapping-coverage-20261023 codex/gate3-mapping-excel-20261023 codex/gate3-note-mapping-review-20261023 codex/gate3-signed-year-scope-20261030 codex/gate3-unapproved-proposal-storage-20261030 codex/payvast-mapping-discovery-20261023 codex/phase-2a-backend-foundation codex/phase-2b-post-merge-record codex/phase-2b-source-registration codex/phase-2b-source-registration-12955190277775369594 codex/post-gate2a-control-reconciliation codex/post-pr18-acceptance-record codex/project-control-foundation codex/t1-repo-hygiene-20261023 codex/t2-ci-hardening-20261023 codex/t3-explicit-migrations-20261023 codex/t6a-readonly-diagnostics-20261023 codex/v1-01-safety-gate-hardening codex/v1-year-isolation-followup-20261023 codex/windows-native-pilot-shell-20261030 docs/aram-master-control-plan-20261111 docs/aram-roadmap-dashboard-20261030 docs/financial-statement-reconciliation-1402-1404-20261110 feat/gate3-governed-mapping-20261101 feat/gate3-package9-1-security-20261103 feat/gate3-package9-isolated-20261103 fix/password-unicode-validation-20261111 governance/aram-5-1-3-controls-20261030 governance/role-based-operating-model jules-1168663913887856558-73b8a885 jules-gate-2b-mapping-workflow-foundation-4137724387867065330 phase-1b1-audit-report-7365389436990903239 phase-2a-backend-foundation-10237597358138521261 phase1b2-postmerge-metadata-fix refactor/drg-parent-year-id-15088443704684163304 source-data-cleanup
sql 166 lines 6.14 KB
Raw
1 /*
2 Aram / Peyvast: metadata-only discovery aid for a DBA.
3 Target syntax: Microsoft SQL Server 2019 / 2022.
4
5 UNEXECUTED: this file has received static content/syntax review only.
6 It has NOT been run against SQL Server, a test instance, or a customer database.
7 It is not a connector and makes no claim about the customer's Peyvast schema.
8
9 In SSMS, select the intended customer database before running this script.
10 Use the customer's approved read-only identity. This script does not switch
11 databases, query application/financial rows, or alter data, objects, permissions,
12 or server/database settings. Every executable statement is a metadata SELECT.
13
14 Metadata visibility depends on the executing identity's permissions. Results
15 may be incomplete or empty. Empty output is NOT evidence that an object, key,
16 relationship, or column does not exist. Do not share credentials. Metadata can
17 identify a customer; redact output consistently before sharing if needed.
18
19 Documentation basis: Microsoft SQL Server documentation, including
20 SERVERPROPERTY, sys.columns joined to sys.types, and metadata visibility:
21 https://learn.microsoft.com/en-us/sql/t-sql/functions/serverproperty-transact-sql
22 https://learn.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-columns-transact-sql
23 https://learn.microsoft.com/en-us/sql/relational-databases/security/metadata-visibility-configuration
24 */
25
26 -- 1. Engine version and edition reported by the connected SQL Server instance.
27 SELECT
28 CONVERT(nvarchar(128), SERVERPROPERTY('ProductVersion')) AS product_version,
29 CONVERT(nvarchar(128), SERVERPROPERTY('ProductMajorVersion')) AS product_major_version,
30 CONVERT(nvarchar(128), SERVERPROPERTY('ProductLevel')) AS product_level,
31 CONVERT(nvarchar(128), SERVERPROPERTY('ProductUpdateLevel')) AS product_update_level,
32 CONVERT(nvarchar(128), SERVERPROPERTY('Edition')) AS edition,
33 CONVERT(int, SERVERPROPERTY('EngineEdition')) AS engine_edition;
34
35 -- 2. Only the currently selected database and its compatibility/collation.
36 SELECT
37 DB_NAME() AS current_database_name,
38 DB_ID() AS current_database_id,
39 d.compatibility_level,
40 d.collation_name
41 FROM sys.databases AS d
42 WHERE d.database_id = DB_ID();
43
44 -- 3. Visible user tables and views in the current database.
45 SELECT
46 s.name AS schema_name,
47 o.name AS object_name,
48 o.object_id,
49 o.type_desc AS object_type
50 FROM sys.objects AS o
51 INNER JOIN sys.schemas AS s
52 ON s.schema_id = o.schema_id
53 WHERE o.type IN ('U', 'V')
54 AND o.is_ms_shipped = 0
55 ORDER BY s.name, o.name, o.object_id;
56
57 -- 4. Visible column definitions for those tables and views.
58 -- max_length is in bytes; -1 denotes a MAX/large-value type where applicable.
59 -- declared_type preserves alias/CLR type identity; base_type may be NULL.
60 SELECT
61 s.name AS schema_name,
62 o.name AS object_name,
63 o.type_desc AS object_type,
64 c.column_id,
65 c.name AS column_name,
66 ts.name AS declared_type_schema,
67 ut.name AS declared_type,
68 bt.name AS base_type,
69 c.max_length AS max_length_bytes,
70 c.precision AS numeric_precision,
71 c.scale AS numeric_scale,
72 c.is_nullable,
73 c.is_identity,
74 c.is_computed,
75 c.collation_name
76 FROM sys.columns AS c
77 INNER JOIN sys.objects AS o
78 ON o.object_id = c.object_id
79 INNER JOIN sys.schemas AS s
80 ON s.schema_id = o.schema_id
81 INNER JOIN sys.types AS ut
82 ON ut.user_type_id = c.user_type_id
83 INNER JOIN sys.schemas AS ts
84 ON ts.schema_id = ut.schema_id
85 LEFT JOIN sys.types AS bt
86 ON bt.system_type_id = c.system_type_id
87 AND bt.user_type_id = bt.system_type_id
88 WHERE o.type IN ('U', 'V')
89 AND o.is_ms_shipped = 0
90 ORDER BY s.name, o.name, c.column_id;
91
92 -- 5. Declared primary/unique keys and other unique indexes, one row per key
93 -- column in key order. Included non-key columns are intentionally omitted.
94 -- Flags distinguish PK/UQ constraints from standalone unique indexes.
95 -- A filtered or disabled unique index must not be assumed to identify all rows.
96 SELECT
97 s.name AS schema_name,
98 o.name AS object_name,
99 o.type_desc AS object_type,
100 i.name AS index_name,
101 kc.name AS constraint_name,
102 kc.type_desc AS constraint_type,
103 i.is_primary_key,
104 i.is_unique_constraint,
105 i.is_unique,
106 i.has_filter,
107 i.is_disabled,
108 i.is_hypothetical,
109 ic.key_ordinal,
110 c.name AS column_name,
111 ic.is_descending_key
112 FROM sys.indexes AS i
113 INNER JOIN sys.objects AS o
114 ON o.object_id = i.object_id
115 INNER JOIN sys.schemas AS s
116 ON s.schema_id = o.schema_id
117 INNER JOIN sys.index_columns AS ic
118 ON ic.object_id = i.object_id
119 AND ic.index_id = i.index_id
120 INNER JOIN sys.columns AS c
121 ON c.object_id = ic.object_id
122 AND c.column_id = ic.column_id
123 LEFT JOIN sys.key_constraints AS kc
124 ON kc.parent_object_id = i.object_id
125 AND kc.unique_index_id = i.index_id
126 WHERE o.type IN ('U', 'V')
127 AND o.is_ms_shipped = 0
128 AND i.is_unique = 1
129 AND ic.key_ordinal > 0
130 ORDER BY s.name, o.name, i.name, ic.key_ordinal;
131
132 -- 6. Declared foreign keys only, one row per paired column in FK order.
133 -- This does not infer undeclared relationships or accounting semantics.
134 SELECT
135 ps.name AS parent_schema,
136 pt.name AS parent_table,
137 fk.name AS foreign_key_name,
138 fkc.constraint_column_id AS column_pair_ordinal,
139 pc.name AS parent_column,
140 rs.name AS referenced_schema,
141 rt.name AS referenced_table,
142 rc.name AS referenced_column,
143 fk.is_disabled,
144 fk.is_not_trusted,
145 fk.delete_referential_action_desc,
146 fk.update_referential_action_desc
147 FROM sys.foreign_keys AS fk
148 INNER JOIN sys.foreign_key_columns AS fkc
149 ON fkc.constraint_object_id = fk.object_id
150 INNER JOIN sys.tables AS pt
151 ON pt.object_id = fkc.parent_object_id
152 INNER JOIN sys.schemas AS ps
153 ON ps.schema_id = pt.schema_id
154 INNER JOIN sys.columns AS pc
155 ON pc.object_id = fkc.parent_object_id
156 AND pc.column_id = fkc.parent_column_id
157 INNER JOIN sys.tables AS rt
158 ON rt.object_id = fkc.referenced_object_id
159 INNER JOIN sys.schemas AS rs
160 ON rs.schema_id = rt.schema_id
161 INNER JOIN sys.columns AS rc
162 ON rc.object_id = fkc.referenced_object_id
163 AND rc.column_id = fkc.referenced_column_id
164 WHERE pt.is_ms_shipped = 0
165 AND rt.is_ms_shipped = 0
166 ORDER BY ps.name, pt.name, fk.name, fkc.constraint_column_id;