SQL Scripting for ETL

From QPR ProcessAnalyzer Wiki
Jump to navigation Jump to search

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 the the script being run. BIGINT. The difference between @_ScriptId and @_CurrentScriptId is apparent with nested scripts. @_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.
@_DatabaseId the Id of the database in use. GUID.
@_EngineScriptingVersion The scripting version that identifies the functionalities available from QPR ProcessAnalyzer server. A script created for newer (bigger) scripting version doesn't necessarily work on a QPR ProcessAnalyzer server supporting older (smaller) version. INT.
@_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 the Id of the filter in whose context the script is being run. BIGINT. Undefined if filter context has not been specified in script execution parameters.
@_ModelId the Id of the model in whose context the script is being run. BIGINT. Undefined if model context has not been specified in script execution parameters.
@_ProjectId the Id of the project in whose context the script is being run. BIGINT. Undefined if project context has not been specified in script execution parameters.
@_QPRProcessAnalyzerVersion the QPR Process Analyzer core dll version as string in the format: <major>.<minor>.<build>.<revision>. NVARCHAR(64).
@_ScriptId Id of the script that was started. BIGINT.
@_ScriptingVersion The scripting version number that identifies the version which was used when script was saved. Can be used for indicating, for example, the version for which the script was originally planned. A script created for newer (bigger) scripting version doesn't necessarily work on a PA server supporting older (smaller) version. INT.
@_UserId the Id of the user running the script. INT.

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
    1. On the QPR tab, click Manage Scripts.
    2. From the dialog, select the context in which the script you wish to run exists.
    3. Select the script you wish to run.
    4. Click Run.
  • Running scripts from the Ribbon
    1. On the QPR tab of the ribbon, click Run.
    2. 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