# Trace data lineage across SQL scripts and stored procedures

Map where each table's data comes from and goes to, so you can see what breaks before you change a table.

- Topic: Data & Analysis
- Framework: Few-Shot
- Level: Standard
- Tags: Data & Analytics, Writing, Coding, Analyze, Code, Fill-in Template
- Source: https://www.thinkwithprompt.com/prompts/trace-data-lineage-across-sql-scripts-and-stored-procedures

## Prompt

```text
Help me trace data lineage in my database code.

**Database platform:** [e.g. SQL Server, Oracle, Snowflake, PostgreSQL]
**Code:** [PASTE SQL scripts, stored procedures, views, or ETL steps - mask credentials and connection strings]
**Table or column I care about:** [NAME, or "all"]
**Why I'm asking:** [e.g. "planning to change a column type", "debugging a wrong number in a report", "documenting for an audit"]

Analyze the code and give me:
1. A lineage table: source object, target object, operation (INSERT, UPDATE, MERGE, SELECT INTO, view, etc.), the script or procedure where it happens, and the key columns involved.
2. Upstream and downstream chains for my table or column, written as simple arrows (e.g. staging.orders -> dbo.orders -> vw_sales -> report).
3. Column-level transformations: renames, calculations, filters, joins, and type conversions that change the data on the way.
4. Impact assessment for my planned change: every object likely to break or produce different results, ranked by risk.
5. Gaps: dynamic SQL, temp tables, linked servers, external jobs, or objects referenced but not included, where lineage cannot be confirmed from the code I gave you.
6. A Mermaid flowchart of the lineage I can paste into documentation.

Only report dependencies you can see in the code. Mark anything inferred as [INFERRED] and list what extra code or metadata (e.g. system dependency views, job schedules) would confirm it.
```

## What to fill in

- e.g. SQL Server, Oracle, Snowflake, PostgreSQL
- NAME, or "all"
- INFERRED

Free to copy and use. From ThinkWithPrompt (https://www.thinkwithprompt.com/), a library of 2,663 curated AI prompts.
