Microsoft Learn

Tabular Model Scripting Language (T-SQL)

The Tabular Model Scripting Language (T-SQL) is a powerful, JSON-based scripting language used to define and manage tabular models in SQL Server Analysis Services (SSAS) and Azure Analysis Services (AAS). It allows for automation, version control, and programmatic manipulation of your tabular data models.

What is T-SQL for Analysis Services?

T-SQL for Analysis Services is an object model that represents the metadata of a tabular model. You can interact with this model by sending JSON-formatted scripts to the Analysis Services engine. This language enables operations such as:

Key Concepts

The T-SQL scripting language is structured around a hierarchical JSON object model. Some core components include:

Getting Started with T-SQL

You can execute T-SQL scripts using various tools:

Example: Creating a Simple Table

Here's a basic example of a T-SQL script to create a simple "Products" table:

json { "script": "{\n \"create\": {\n \"object\": {\n \"database\": \"AdventureWorksDW2022\",\n \"schema\": \"\\\"dbo\\\"\",\n \"table\": \"\\\"Products\\\"\"\n },\n \"elements\": [\n {\n \"column\": {\n \"name\": \"ProductID\",\n \"dataType\": \"int\",\n \"sourceColumn\": \"ProductID\",\n \"isKey\": true\n }\n },\n {\n \"column\": {\n \"name\": \"ProductName\",\n \"dataType\": \"string\",\n \"sourceColumn\": \"Name\"\n }\n },\n {\n \"column\": {\n \"name\": \"ProductNumber\",\n \"dataType\": \"string\",\n \"sourceColumn\": \"ProductNumber\"\n }\n }\n ]\n }\n}" }

Example: Creating a Measure

This script adds a measure to calculate the total sales amount:

json { "script": "{\n \"alter\": {\n \"object\": {\n \"database\": \"AdventureWorksDW2022\",\n \"table\": \"\\\"Sales\\\"\"\n },\n \"elements\": [\n {\n \"measure\": {\n \"name\": \"Total Sales\",\n \"expression\": \"SUM(Sales[SalesAmount])\"\n }\n }\n ]\n }\n}" }

Resources