Knowledge · Free tool

See nested functions in SQL Server as a call tree

You are investigating a slow query, and the execution plan shows a multi-statement table-valued function that seems to cost almost nothing? The Function Tree shows which functions, views and procedures depend on each other, how deep the chain goes and where a look at the actual plan pays off.

Updated Nov 27, 2026 · Script version 1 · MIT license

Download SQL script

Your script output stays in your browser.

The analysis runs entirely in JavaScript on your computer. Whatever you paste is not uploaded, not stored and not transmitted to PSG. PSG receives no data from your analysis. Once the page has loaded, you can also explore the tree offline.

Technical details and how to check for yourself

The page only loads its own files. A Content Security Policy blocks script connections such as fetch, XHR and beacons (connect-src 'none'), form submissions and third-party scripts. No code on the page transmits or stores your input. No tracking, no cookies; the PDF report and the image are also created locally. Feel free to check: open the developer tools and go to the “Network” tab, or disconnect from the network after the page has loaded.

If the object names themselves are confidential, the script replaces them with numbers via @anonym = 1 before you copy anything.

How it works

  1. 1
    Get the script. psg-funktionsbaum.sql reads the SQL Server catalog and searches module definitions for features such as WHILE. It changes nothing except its own temporary tables. The output contains neither definition texts nor table contents. SQL Server 2016 or later; for a complete picture, VIEW DEFINITION and SELECT on sys.sql_expression_dependencies.
  2. 2
    Run it. In a new query window in the database you are investigating. Optionally set a function as @objekt; otherwise you get the whole database.
  3. 3
    Paste. In the second result set, click the XML cell psg_funktionsbaum. Copy the entire text from the window that opens and paste it below.

Paste the script output

Paste text or drag a file here.

What the tree does not show

  • Only static references from sys.sql_expression_dependencies. Dynamic SQL, CLR code and calls from the application are missing.
  • Whether a function is evaluated once or per row is not part of the dependencies. A scalar function in the SELECT list can run per row; the plan shows whether SQL Server inlines it.
  • References to other databases or through linked servers end as a leaf. Run the script again there.
  • Features such as cursors or WHILE come from a text search; matches in comments are counted too.
  • “Inlineable” is the catalog value is_inlineable. Only the plan shows whether SQL Server actually inlines a scalar function into the query.
  • Whatever the executing account is not allowed to see is missing: without VIEW DEFINITION, a module’s calls are missing; with DENY VIEW DEFINITION, the module is missing entirely, without any notice. Ideally, run the script with an account that is allowed to see all definitions.
  • No runtimes. You can find those in Query Store or in the actual plan. How to find the statements inside multi-statement functions there is covered in the series Nested MSTVFs.