SQL Server guide

SQL examples for SQL Server


The ZappySys API Driver is a user-friendly interface designed to facilitate the seamless integration of various applications with the Power BI API. With its intuitive design and robust functionality, the ZappySys API Driver simplifies the process of configuring specific API endpoints to efficiently read or write data from Power BI.

On this page you will find some SQL examples which can be used for API ODBC Driver or Data Gateway API Connector.

Workspaces - Get Workspaces

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * 
FROM Workspaces';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Workspaces - Get a Workspace

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * 
FROM Workspaces
WHERE Id=''aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee''';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Datasets - Get Datasets

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * 
FROM Datasets';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Datasets - Get Datasets in a specified Workspace

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * 
FROM Datasets
WITH (WorkspaceId = ''aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'')';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Datasets - Get a Dataset

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * 
FROM Datasets
WHERE Id=''aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee''';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Datasets - Create a Push Dataset

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * 
FROM create_push_dataset
WITH (Definition=''{
  "name": "My Push Dataset Name",
  "defaultMode": "Push",
  "tables": [
	{
      "name": "Products",
      "columns": [
        {
          "name": "Id",
          "dataType": "Int64"
        },
        {
          "name": "Name",
          "dataType": "string"
        },
        {
          "name": "Category",
          "dataType": "string"
        },
        {
          "name": "IsComplete",
          "dataType": "bool"
        },
        {
          "name": "ManufacturedOn",
          "dataType": "DateTime"
        },
        {
          "name": "Sales",
          "dataType": "Int64",
          "formatString": "Currency"
        },        
        {
          "name": "Price",
          "dataType": "Double",
          "formatString": "Currency"
        }
      ]
    }
  ]
}''
)

-- More info on creating a Push Dataset:
-- https://learn.microsoft.com/en-us/rest/api/power-bi/push-datasets/datasets-post-dataset';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Datasets - Create a Push Dataset with 2 Tables

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * 
FROM create_push_dataset
WITH (Definition=''{
  "name": "My Push Dataset Name",
  "defaultMode": "Push",
  "tables": [
     {
      "name": "Customers",
      "columns": [
        {
          "name": "Id",
          "dataType": "Int64"
        },
        {
          "name": "Name",
          "dataType": "string"
        }
      ]
    },
	{
      "name": "Products",
      "columns": [
        {
          "name": "Id",
          "dataType": "Int64"
        },
        {
          "name": "Name",
          "dataType": "string"
        },
        {
          "name": "Category",
          "dataType": "string"
        },
        {
          "name": "IsComplete",
          "dataType": "bool"
        },
        {
          "name": "ManufacturedOn",
          "dataType": "DateTime"
        },
        {
          "name": "Sales",
          "dataType": "Int64",
          "formatString": "Currency"
        },        
        {
          "name": "Price",
          "dataType": "Double",
          "formatString": "Currency"
        }
      ]
    }
  ]
}''
)

-- More info on creating a Push Dataset:
-- https://learn.microsoft.com/en-us/rest/api/power-bi/push-datasets/datasets-post-dataset';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Datasets - Delete a Dataset

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT *
FROM delete_dataset
WHERE Id = ''aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee''

-- More info on deleting a Dataset:
-- https://learn.microsoft.com/en-us/rest/api/power-bi/datasets/delete-dataset';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Datasets - Refresh a Dataset

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT *
FROM refresh_dataset
WHERE Id = ''aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee''
                
-- More info on refreshing a Dataset:
-- https://learn.microsoft.com/en-us/rest/api/power-bi/datasets/refresh-dataset';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Tables - Get Tables

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * 
FROM get_tables';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Tables - Get Tables in a specified Dataset

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * 
FROM get_tables
WITH (DatasetId=''aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'')';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Tables - Get Table Columns

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * 
FROM get_table_columns
WITH (TableName=''MyTable'')';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Tables - Get Table Columns in a specified Dataset

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * 
FROM get_table_columns
WITH (TableName=''MyTable'',
      DatasetId=''aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'')';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Tables - Get Table Rows (Use default Workspace and Dataset)

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * 
FROM MyTable';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Tables - Get Table Rows for a specified Workspace and Dataset

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT *
FROM get_table_rows
WITH(
	  "TableName"=''Products''
	, "DatasetId"=''11b6c287-51d3-4061-bed8-811a4e5f6ce9''
	, "WorkspaceId"=''848353e2-f3b1-4fb4-89d7-44e84b8bdf9f''
)';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Tables - Insert / Update / Delete Rows for a specified Workspace and Dataset

DECLARE @MyQuery NVARCHAR(MAX) = 'INSERT INTO Products 
SOURCE(
  ''MSSQL'',
  ''Data Source=localhost\developer;Initial Catalog=Northwind;Integrated Security=true'',
  ''  SELECT T.* FROM (       SELECT  TOP 50                
      ProductName AS [Name]              
      ,C.CategoryName AS Category              
      ,Discontinued AS IsComplete              
      ,GETDATE() AS ManufacturedOn              
      ,CAST(UnitPrice * ReOrderLevel * 100 AS BIGINT) AS Sales              
      ,CAST(UnitPrice AS DECIMAL) AS Price       
      FROM Northwind.dbo.Products AS P       
      JOIN Northwind.dbo.Categories C ON P.CategoryId = P.CategoryId       
      ) AS T              
      CROSS JOIN GENERATE_SERIES(1, 2000)             
      -- COMMENT:  50 x 2000 = 100 000 rows  
   ''
) 
CONNECTION(
  Parameters = ''[{ Name: "TokenUrl",Value:"https://login.microsoftonline.com/organizations/oauth2/v2.0/token"}                         
  ,{ Name: "DatasetId",Value: "6a0e04da-a6e4-4533-abe4-30fcabd0e2a5"},                      
  { Name: "WorkspaceId",Value: "848353e2-f3b1-4fb4-89d7-44e84b8bdf9f"}]''
)';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Tables - Using an INSERT statement

DECLARE @MyQuery NVARCHAR(MAX) = 'INSERT INTO MyTable(MyColumn1, MyColumn2, MyColumn3, MyColumn4, MyColumn5)
VALUES (1001, ''Glass'', true, ''2001-02-03'', 195.95)';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Tables - Using an INSERT statement in a specified Dataset

DECLARE @MyQuery NVARCHAR(MAX) = 'INSERT INTO MyTable(MyColumn1, MyColumn2, MyColumn3, MyColumn4, MyColumn5)
VALUES (1001, ''Glass'', true, ''2001-02-03'', 195.95)
WITH (DatasetId=''aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'')';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Tables - Get Table rows

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * 
FROM get_table_rows 
WITH (TableName=''MyTable'')';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Tables - Get Table rows in a specified Dataset

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * 
FROM get_table_rows 
WITH (TableName=''MyTable'', DatasetId=''aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'')';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Tables - Truncate a Push Dataset Table

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT *
FROM truncate_push_dataset_table
WITH (TableName=''MyTable'')
                
-- More info on truncating a Push Dataset Table:
-- https://learn.microsoft.com/en-us/rest/api/power-bi/push-datasets/datasets-delete-rows';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Tables - Truncate a Push Dataset Table in a specified Dataset

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT *
FROM truncate_push_dataset_table
WITH (DatasetId=''aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'', TableName=''MyTable'')
                
-- More info on truncating a Push Dataset Table:
-- https://learn.microsoft.com/en-us/rest/api/power-bi/push-datasets/datasets-delete-rows';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Execute a DAX query - Evaluating a Table

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * 
FROM execute_dax_query
WITH (Query=''EVALUATE ''''MyTable'''''')

-- More info on ''EVALUATE'' statement and DAX queries:
-- https://dax.guide/st/evaluate/
-- https://learn.microsoft.com/en-us/dax/dax-queries
-- https://learn.microsoft.com/en-us/dax/filter-functions-dax
-- https://learn.microsoft.com/en-us/dax/dax-syntax-reference';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Execute a DAX query - Using FILTER function with simple expression

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * 
FROM execute_dax_query 
WITH (Query=''EVALUATE FILTER(''''MyTable'''', [MyColumn] = "MyValue"'')

-- More info on ''EVALUATE'' statement and DAX queries:
-- https://dax.guide/st/evaluate/
-- https://dax.guide/filter/
-- https://dax.guide/operators/
-- https://learn.microsoft.com/en-us/dax/dax-queries
-- https://learn.microsoft.com/en-us/dax/dax-syntax-reference';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Execute a DAX query - Using FILTER function with AND and OR operators

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * 
FROM execute_dax_query 
WITH (Query=''EVALUATE FILTER(''''MyTable'''', [MyColumn1] = "MyValue" && ([MyColumn2] > 0 || [MyColumn3] <= 1000))'')

-- More info on ''EVALUATE'' statement and DAX queries:
-- https://dax.guide/operators/
-- https://learn.microsoft.com/en-us/dax/dax-queries
-- https://learn.microsoft.com/en-us/dax/dax-syntax-reference';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Execute a DAX query - Selecting specific columns from a Table

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * 
FROM execute_dax_query
WITH (Query=''EVALUATE 
             SELECTCOLUMNS (
                ''''MyTable'''',
                "MyColumn1 alias", [MyColumn1],
                "MyColumn2 alias", [MyColumn2]
             )
             ORDER BY "MyColumn2 alias"''
    )
    
-- More info on ''EVALUATE'' statement and DAX queries:
-- https://dax.guide/st/evaluate/
-- https://dax.guide/selectcolumns/   
-- https://learn.microsoft.com/en-us/dax/dax-queries
-- https://learn.microsoft.com/en-us/dax/dax-syntax-reference';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Execute a DAX query - Selecting and sorting TOP N rows

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * 
FROM execute_dax_query
WITH (Query=''EVALUATE 
             TOPN(1000, ''''MyTable'''', [MyColumnOrExpression], ASC)'')

-- More info on ''EVALUATE'' statement and DAX queries:
-- https://dax.guide/st/evaluate/
-- https://dax.guide/topn/
-- https://learn.microsoft.com/en-us/dax/dax-queries
-- https://learn.microsoft.com/en-us/dax/dax-syntax-reference';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Execute a DAX query - A complicated query

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * 
FROM execute_dax_query
WITH (Query=''
DEFINE
    VAR MinimumAmount = 2000000
    VAR MaximumAmount = 8000000
EVALUATE
FILTER (
    ADDCOLUMNS (
        SUMMARIZE (Sales, Products[Category]),
        "CategoryAmount", [Sales Amount]
    ),
    AND (
        [CategoryAmount] <= MinimumAmount,
        [CategoryAmount] >= MaximumAmount
    )
)
ORDER BY [CategoryAmount]"'')

-- More info on ''EVALUATE'' statement and DAX queries:
-- https://dax.guide/st/evaluate/
-- https://dax.guide/addcolumns/
-- https://dax.guide/summarize/
-- https://dax.guide/st/order-by/
-- https://learn.microsoft.com/en-us/dax/dax-queries
-- https://learn.microsoft.com/en-us/dax/filter-functions-dax
-- https://learn.microsoft.com/en-us/dax/dax-syntax-reference';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Generics - A simple generic API request

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * 
FROM generic_request
WITH (Url=''/groups'',
Filter=''$.value[*]'')


/*
EXPLANATION:
- This configuration calls Power BI REST API "Get Groups" endpoint and gets the Workspaces back.
- This is achieved by "/groups" value in the "Url" parameter.
- The SQL query parameter "Filter" uses JsonPath "$.value[*]".
- This gets JSON objects from "value" array and transforms them into SQL rows.

MORE INFORMATION:
- About "Get Groups" REST API endpoint:
  https://learn.microsoft.com/en-us/rest/api/power-bi/groups/get-groups
- About JsonPath used in "Filter" parameter:
  https://zappysys.com/blog/jsonpath-examples-expression-cheetsheet  
*/';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Generics - A generic API request with URL parameter

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * 
FROM generic_request
WITH (Url=''/groups?$filter=contains(name,''''MyWorkspace'''') or name eq ''''My Blue Workspace'''''',
Filter=''$.value[*]'')


/*
EXPLANATION:
- This configuration calls Power BI REST API "Get Groups" endpoint and gets the Workspaces back.
- This is achieved by "/groups" value in the "Url" parameter.
- Workspaces are filtered on the Power BI REST API side by using the "$filter" URL parameter.
- Only those Workspaces are returned that:
  > contain a string value "MyWorkspace" or
  > if the Workspace name is "My Blue Workspace" (each single quote is escaped with two single quotes).
- The SQL query parameter "Filter" uses JsonPath "$.value[*]".
- This gets JSON objects from "value" array and transforms them into SQL rows.

MORE INFORMATION:
- About "Get Groups" REST API endpoint:
  https://learn.microsoft.com/en-us/rest/api/power-bi/groups/get-groups
- About JsonPath used in "Filter" parameter:
  https://zappysys.com/blog/jsonpath-examples-expression-cheetsheet  
*/';

EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY];

Learn more about this SQL query.

Getting Started with Examples

ZappySys API Driver is a powerful software solution designed to facilitate the extraction and integration of data from a wide range of sources through APIs. Its intuitive design and extensive feature set make it an essential asset for any organization dealing with complex data integration tasks.

To get started with examples using ZappySys API Driver, please click on the following applications:

SQL Server Connect Power BI in SQL Server
Power BI Connect Power BI in Power BI
SSRS Connect Power BI in SSRS
Informatica Connect Power BI in Informatica
MS Access Connect Power BI in MS Access
MS Excel Connect Power BI in MS Excel
SSAS Connect Power BI in SSAS
C# Connect Power BI in C#
Python Connect Power BI in Python
JAVA Connect Power BI in JAVA
Tableau Connect Power BI in Tableau
SAP Crystal Reports Connect Power BI in SAP Crystal Reports
Azure Data Factory (Pipeline) Connect Power BI in Azure Data Factory (Pipeline)
Talend Studio Connect Power BI in Talend Studio
UiPath Connect Power BI in UiPath
PowerShell Connect Power BI in PowerShell
ODBC Connect Power BI in ODBC

Key features of the ZappySys API Driver include:

The API ODBC driver facilitates the reading and writing of data from numerous popular online services (refer to the complete list here) using familiar SQL language without learning complexity of REST API calls. The driver allows querying nested structure and output as a flat table. You can also create your own ODBC / Data Gateway API connector file and use it with this driver.

  1. Intuitive Configuration: The interface is designed to be user-friendly, enabling users to easily set up the specific API endpoints within Power BI without requiring extensive technical expertise or programming knowledge.

  2. Customizable Endpoint Setup: Users can conveniently configure the API endpoint settings, including the HTTP request method, endpoint URL, and any necessary parameters, to precisely target the desired data within Power BI.

  3. Data Manipulation Capabilities: The ZappySys API Driver allows for seamless data retrieval and writing, enabling users to fetch data from Power BI and perform various data manipulation operations as needed, all through an intuitive and straightforward interface.

  4. Secure Authentication Integration: The driver provides secure authentication integration, allowing users to securely connect to the Power BI API by inputting the necessary authentication credentials, such as API tokens or other authentication keys.

  5. Error Handling Support: The interface is equipped with comprehensive error handling support, ensuring that any errors or exceptions encountered during the data retrieval or writing process are efficiently managed and appropriately communicated to users for prompt resolution.

  6. Data Visualization and Reporting: The ZappySys API Driver facilitates the seamless processing and presentation of the retrieved data from Power BI, enabling users to generate comprehensive reports and visualizations for further analysis and decision-making purposes.

Overall, the ZappySys API Driver serves as a powerful tool for streamlining the integration of applications with Power BI, providing users with a convenient and efficient way to access and manage data, all through a user-friendly and intuitive interface.