SQL Scripting for ETL: Difference between revisions
Line 51: | Line 51: | ||
* ModelId: Specifies the id of the model in which to operate. | * ModelId: Specifies the id of the model in which to operate. | ||
* ProjectId: Specifies the id of the project in which to operate. | * ProjectId: Specifies the id of the project in which to operate. | ||
* ProjectName: Specifies the name of the project in which to operate (ignored if ProjectId defined). Can not be used when importing data since the possible new project name resulting from the import operation is given using ProjectName parameter. | |||
* ModelName: Specifies the name of the project in which to operate (ignored if ModelId defined). Can not be used when importing any model data, such as events, since the possible new model name resulting from the import operation is given using ModelName parameter. | |||
* FilterName: Specifies the name of the project in which to operate (ignored if FilterId defined). If multiple context parameters have been defined, they all must point to the same context (intersection of all the defined contexts is not empty). | |||
When running a script, these parameters can be given either for the whole script run or for each operation separately. | |||
'''Model import context''' defines the context in which the whole script is being run and it changes as new imports are made during the operation of the script as follows. When a script run is started, all the context parameters in the script run parameters are copied into model import context. Whenever running import operations, such as ImportEvents or ImportDataTable the context of the target of that import (model or data table) replaces the earlier value in the model import context. | |||
When running any operation, the running context of that operation is determined as follows: | |||
* If the operation itself is given any context parameters, those will be used as the context of the operation. | |||
* Otherwise, the current model import context will be used. | |||
== Script Variable Examples == | == Script Variable Examples == |
Revision as of 22:34, 10 June 2020
This page describes how to extract data from source systems and load data into QPR ProcessAnalyzer, i.e. how to do ETL (extract, transform and load). The supported commands and their descriptions are listed in QPR ProcessAnalyzer Scripting Commands. It is possible to load raw data into QPR ProcessAnalyzer, and do the data transformation and loading into QPR ProcessAnalyzer Service via scripts using temporary database tables, so that the resulting transformed data can be used for analyses in QPR ProcessAnalyzer. Scripts can be written in the Manage Scripts dialog in the Excel Client. The script consists of SQL statements and QPR ProcessAnalyzer commands that take the preceding SQL statements as parameters.
Script Variables
The scripts have variables available listed in the table below. Even when a script context hasn't been selected, the variables get their values from a default filter. The default filter is the filter that was last used by the user in the model the user has last opened. If the user hasn't opened any model before, the default filter is taken from the last created model to which the user has access rights.
Variable name | Description |
---|---|
@_CurrentScriptId | Id of the script where the execution currently is. BIGINT. @_ScriptId gives the id of the script that was started and which can call other scripts, @_CurrentScriptId gives the id of the script that is being run after the script has been called by the original script. That is, if you have nested scripts, @_ScriptId doesn't change, whereas @_CurrentScriptId is updated when the original script calls some other script. |
@_ExceptionDetails | If there was an exception when running the script, contains the details that would have been displayed, including the system stack trace, NVARCHAR(MAX), otherwise NULL. |
@_ExceptionMessage | If there was an exception when running the script, contains a message that would have been displayed, NVARCHAR(MAX), otherwise NULL. |
@_ExceptionOccurred | If there was an exception when running the script, then this value is 1, otherwise 0. INT |
@_ExceptionType | If there was an exception when running the script, shows the C# class name for the exception, NVARCHAR(MAX), otherwise NULL. |
@_FilterId | Id of the filter in whose context the script is run. BIGINT. Undefined if filter context has not been specified in script execution parameters. |
@_ModelId | Id of the model in whose context the script is run. BIGINT. Undefined if model context has not been specified in script execution parameters. |
@_ProjectId | Id of the project in whose context the script is run. BIGINT. Undefined if project context has not been specified in script execution parameters. |
@_QPRProcessAnalyzerVersion | QPR ProcessAnalyzer version in the format: <major>.<minor>.<build>.<revision>. NVARCHAR(64). |
@_ScriptId | Id of the script that was started. BIGINT. |
@_UserId | Id of the user running the script. INT. |
Resolving operation context
Context of an operation can be defined using the following parameters:
- FilterId: Specifies the id of the filter in which to operate.
- ModelId: Specifies the id of the model in which to operate.
- ProjectId: Specifies the id of the project in which to operate.
- ProjectName: Specifies the name of the project in which to operate (ignored if ProjectId defined). Can not be used when importing data since the possible new project name resulting from the import operation is given using ProjectName parameter.
- ModelName: Specifies the name of the project in which to operate (ignored if ModelId defined). Can not be used when importing any model data, such as events, since the possible new model name resulting from the import operation is given using ModelName parameter.
- FilterName: Specifies the name of the project in which to operate (ignored if FilterId defined). If multiple context parameters have been defined, they all must point to the same context (intersection of all the defined contexts is not empty).
When running a script, these parameters can be given either for the whole script run or for each operation separately.
Model import context defines the context in which the whole script is being run and it changes as new imports are made during the operation of the script as follows. When a script run is started, all the context parameters in the script run parameters are copied into model import context. Whenever running import operations, such as ImportEvents or ImportDataTable the context of the target of that import (model or data table) replaces the earlier value in the model import context.
When running any operation, the running context of that operation is determined as follows:
- If the operation itself is given any context parameters, those will be used as the context of the operation.
- Otherwise, the current model import context will be used.
Script Variable Examples
The following script fragment defines the ProjectId, ModelId, and FilterId parameters by using the script variables:
SELECT @_ProjectId as ProjectId, @_ModelId as ModelId, @_FilterId as FilterId;
The following script gets the View Settings currently in use in the Settings pane in Excel and shows them as a table in a new Excel sheet:
(SELECT * FROM #_ViewSettings) (SELECT 'SheetName', 'ViewSettings') --#ShowReport
The following script gets the current selection on the analysis and shows the data as a table in a new Excel sheet:
(SELECT * FROM #_Selection) (SELECT 'SheetName' , 'Selection') --#ShowReport
The following script gets various information about the environment and shows them in a new Excel sheet:
SELECT @_QPRProcessAnalyzerVersion as QPRProcessAnalyzerVersion, @_ScriptingVersion as ScriptingVersion, @_EngineScriptingVersion as EngineScriptingVersion, @_UserId as Userid, @_DatabaseId as DatabaseId (SELECT 'SheetName', 'Info') --#ShowReport
Exception Handling
In general, scripts are meant to be developed in such a way that in the end you can run the scripts without any errors. However, sometimes there may be some system issues (timeouts SAP etc.) that can cause for example data extraction scripts to fail. For these kind of situations and for development time and troubleshooting purposes, you can use the CatchOperationExceptions parameter and the @_ExceptionOccurred, @_ExceptionType, @_ExceptionMessage, and @_ExceptionDetails script variables with the QPR ProcessAnalyzer script commands to handle exceptions in ProcessAnalyzer. Out of these, the @_ExceptionOccurred is handy for example in defining some other command to be run in case there was an exception. For SQL errors, the TRY-CATCH error handling should be used.
Note that the CatchOperationExceptions parameter is in effect only for the command it is used with, i.e. it isn't in effect in child scripts or scripts that are run via the --#Exit command. In addition, when there are multiple ProcessAnalyzer script commands in the script, the @_ExceptionOccurred, @_ExceptionType, @_ExceptionMessage, and @_ExceptionDetails script variables get updated each time, i.e. the variable values are available only until the next ProcessAnalyzer command is executed. To use the same variable values in multiple ProcessAnalyzer commands in the script, place the values into a temporary table:
SELECT @_ExceptionOccurred 'ExceptionOccurred', @_ExceptionType 'ExceptionType', @_ExceptionMessage 'ExceptionMessage', @_ExceptionDetails 'ExceptionDetails' INTO #PACommandExceptions
Using temporary tables in SQL Server
When transforming data in QPR ProcessAnalyzer, temporary tables can be used (temporary tables names start with #). Note that using the global temporary tables (tables names start with ##) is not allowed.
Print Command in SQL Server
The Print SQL statement can be used to generate log entries into the script execution log.
Running Scripts
Scripts can be run in the following ways:
- Running scripts from the Manage Scripts dialog
- On the QPR tab, click Manage Scripts.
- From the dialog, select the context in which the script you wish to run exists.
- Select the script you wish to run.
- Click Run.
- Running scripts from the Ribbon
- On the QPR tab of the ribbon, click Run.
- Select the script to be run from the opening Script Gallery.
Terminating Scripts
Scripts can be terminated in the following ways:
- The user running the script can click the Cancel button when the script is running.
- The script can use the --#Exit command to stop script execution.
- A QPR ProcessAnalyzer Administrator user can terminate scripts in the Operation Log.
- The SQL Server System Administrator can kill the session that uses the script by using e.g. SQL Server Management Studio.
- The Internet Information Services Administrator can recycle the application pool if the script has caused it to hang. This may also cause other requests by other users being processed at the same time to be aborted.
- The Windows Administrator can kill the w3wp.exe process processing a problematic script. This may also cause other requests by other users being processed at the same time to be aborted.
Note that terminating a script will not revert any changes the script has already done in the database before the Cancel button is clicked.
Things to Note About Scripts
When writing and using scripts, take the following things into account:
- Only those lines in the script that start with "--#" (without the quotes) are treated as QPR ProcessAnalyzer Commands, i.e. if there are leading whitespaces before the command, the line is treated as a comment.
- If you don't define a value for the MaximumCount parameter, 1000 will be used as default, i.e. only the 1000 first rows from a given table or model will be used.
- When doing more advanced operations with scripts, you may run into the error messages such as: "The data types sql_variant and varchar are incompatible in the add operation.", "Argument data type sql_variant is invalid for argument 1 of like function.", "Argument data type sql_variant is invalid for argument 1 of left function.". This is because case attributes, event attributes, and data inside data tables are sql_variant type data. In order to use them with more advanced operations (e.g. Add), you need to CONVERT or CAST them into some other data type before the operations. See this example.
- For certain characters in attribute values, you need to use escaping in order to have them interpreted correctly in the script. For more information, see Escaping for Attribute Values.
Supported Transport Layer Security Protocols
The following transport layer security protocols are supported when using for example the CallWebService, ImportOdbcQuery, ImportOleDbQuery, ImportSapQuery, ImportSqlQuery, and SendEmail script commands:
- TLS 1.0
- TLS 1.1
- TLS 1.2
SSL2 and SSL3 are not supported.
See Also
- Troubleshooting QPR ProcessAnalyzer Scripts
- For a list of supported commands and their descriptions, see page QPR ProcessAnalyzer Scripting Commands
- There is also an ETL tutorial for learning the basics of creating ETL scripts in SQL and using them in QPR ProcessAnalyzer
- For more examples, see ETL Script Examples