Synonyms & Cross-DB Access

Status: Reviewed (deep-dive v1)
Stack note: PowerShell collectors hit instance DBs; boards and merges live in the warehouse. Keep that boundary clean — synonyms (and disciplined three-part names) so usp_* don’t hardcode server.database soup in every proc. Blazor talks only to warehouse procs (SQL-12).
Pairs with: SQL-01/SPIKE-01 (inventory), SQL-19 (merges), SQL-05 (cross-db rights), SQL-09 (linked/AG naming).
Goal: When to use synonyms, how to shape warehouse↔instance access for PS, and what not to do.


Quick chooser

Need Prefer Avoid
Stable name for an object that moves/renames Synonym in warehouse Find/replace three-part names everywhere
PS collect from many instances Connect per instance → stage → warehouse merge One linked-server cursor mega-proc as default
Board reads Warehouse tables only Blazor querying instance DBs directly
Rare admin reach into instance Controlled linked server / PS remoting App pool with rights on every prod DB
Cross-db same instance Three-part db.schema.object or synonym Dynamic SQL with raw DB names from UI (SQL-14)

Mental model (this estate)

[SQL instances] --PS collectors--> staging / TVP --> [Warehouse] --usp_*--> [Blazor]
                      |                                ^
                      +----- optional synonyms --------+  (local aliases only)

Synonym basics

-- In warehouse: alias a table in another DB on same instance
CREATE SYNONYM dbo.LegacyConfig FOR ConfigDB.dbo.AppSetting;

-- Alias via linked server (use sparingly)
CREATE SYNONYM dbo.RemoteProbe FOR LinkedSql01.msdb.dbo.sysjobs;

-- Use like a local object
SELECT * FROM dbo.LegacyConfig;

Notes:

-- env deploy pattern: script sets target
CREATE SYNONYM dbo.RefCalendar FOR $(RefDb).dbo.Calendar;

Cross-DB same SQL instance

Pattern Use
Warehouse.dbo.usp_… reading InventoryAux.dbo.T Three-part name or synonym
Same-instance reporting Synonym keeps proc text stable if aux DB renamed
Permissions Grant on base objects; test as app/Agent account

Don’t grant Blazor db_owner on aux DBs “so joins work.”


Cross-instance (linked servers)

Prefer PS multi-connect for estate collect:

  1. Inventory list from warehouse (SqlInstance)
  2. PS foreach instance (throttle, timeout, error log)
  3. Bulk load stage → usp_*_Merge (SQL-19)

Linked servers when:

Checklist if you use them:


What Blazor / app pool must not do

Boards = warehouse. Remediation = ops/PS/Agent with separate rights (SPIKE hard rule).


Naming & repo habits

/sql/warehouse/synonyms/001_ref_synonyms.sql
/sql/warehouse/procs/...
/ps/Collect-SqlInventory.ps1   # connects to instances, writes warehouse

Permissions sketch

Principal Warehouse Instance DBs
Blazor app pool EXECUTE on usp_* (+ TVP types) None
Agent merge job Write staging + merge procs None (data already staged)
PS collector Write staging / exec merge Read dmvs / needed views per instance (least priv)
DBA Broader Broader

Cross-db synonym read: grant SELECT on base table to the executing principal (or use module signing — advanced; document if used).


Common failure patterns

Symptom Likely cause
Synonym works for you, fails for app Missing grants on base object
Slow board suddenly Proc started using linked server path
Collector “works in IDE” Different login mapping
Broken after restore/rename Synonym target stale
Security finding App pool reaching instances

Tiny lab

  1. Two DBs on one instance — synonym + proc in warehouse selecting through it
  2. Deny app login on base table — confirm failure; grant least priv
  3. Time PS-per-instance collect vs linked pull for N hosts — pick default

Shortlist wrap (18→23)

# Topic
18 Transactions & isolation
19 MERGE & idempotent upserts
20 Query Store for procs
21 TVPs from Blazor/PS
22 Temporal / history
23 Synonyms & cross-DB (this note)

Done when


SQL Dude — SQL-23 Synonyms & Cross-DB Access v1