SQL Scripting for ETL

From QPR ProcessAnalyzer Wiki
Revision as of 06:57, 25 October 2018 by Ollvihe (talk | contribs)
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.
#_ViewSettings The Settings shown in the right-hand pane in Excel used to generate the selected view. Note that this doesn't include analysis results or QPR ProcessAnalyzer Server related information, such as FilterId, TotalEventCount, or DatabaseId. Represented as a table two column table variable. The columns are:
  • Name: the name of the parameter. NVARCHAR(440)
  • Value: the value of the parameter. NVARCHAR(MAX)
#_Selection Objects user has currently selected on the analysis. Supported objects are the event types and flows in the Flowchart or Path analyses and the Excel cells in the Duration, Profiling, Cases, Variations, Event Types, or Flows analyses. Any filters already applied are included in the selection. The maximum amount for the selection is 1000 separate objects and 10000 cells and their values. Represented as a four column table variable, where columns are:
  • Type: type of the selected object. INT. Types are described in the table below.
  • IntValue: integer representation of the selection value (used for object IDs etc.). Data type is BIGINT.
  • Value: string representation of the selected value. Used for selections that require more complex representation than a single integer value. Data type is NVARCHAR(MAX).
  • Name: the unique name for the selection value. See the table above for the list of name used for each type. Note that there can be multiple rows with the same name if the selection consists of multiple "objects". Data type is NVARCHAR(MAX).
Type Description
0 Common. Values for the Common Type are represented as a Value-IntValue pair. See the table on the right for descriptions.
2 Activity. Has the following names and values:
  • SelectedActivities (IntValue): One activity/event type id in every row.
6 Activity Path. Has the following names and values:
  • SelectedActivityPaths (Value): One activity path in every row.
8 Attribute Value. Has the following names and values:
  • SelectedAttributeValues (Value): One attribute value in every row.
10 Case. Has the following names and values:
  • SelectedCases (IntValue): One case id in every row.
12 Count. Has the following names and values:
  • SelectedCounts (IntValue): One variation event type count in every row.
14 Duration. Has the following names and values:
  • SelectedDurations (IntValue): One duration in every row.
16 Transition. Has the following names and values:
  • SelectedTransition (Value): One transition/flow in every row.
18 Variation. Has the following names and values:
  • SelectedVariations (IntValue): One variation in every row.
20 Trends. Has the following names and values:
  • SelectedEventTypeTrends (Value): One selected event type trend with all its selected period identifiers in every row.
  • SelectedTrendPeriodLevel: Selected period level for trend selection, possible values are 0 = Day, 1 = Week, 2 = Month, 3 = Quarter, 4 = Year.
  • SelectedTrendStartDate: Selected start date in ISO format (e.g. 2016-01-28) for the trend selection.
Common Type Value Description
NumberOfActivities The amount of activities in the selection.
NumberOfActivityPaths The amount of activity paths in the selection.
NumberOfAttributeValues The amount of attribute values in the selection.
NumberOfCases The amount of cases in the selection.
NumberOfCounts See the "Group By Event Count" entry in the Variations Analysis in QPR ProcessAnalyzer Excel Client page.
NumberOfDurations The amount of durations in the selection.
NumberOfTransitions The amount of transitions in the selection.
NumberOfVariations The amount of variations in the selection.
DurationGranularity The duration time unit used in the selection. See the "Group By" entry in the Duration Analysis in QPR ProcessAnalyzer Excel Client page for reference.
DurationMaximum The duration limit in the selection. See the "Duration Limit" entry in the Duration Analysis in QPR ProcessAnalyzer Excel Client page for reference.
ReversedPaths The value is "1" if the "Predecessors" Direction was selected in the Path Analysis in QPR ProcessAnalyzer Excel Client, otherwise "0".
SelectionType What was selected. The value for this parameter is shown on the "Value" column instead of the IntValue column.

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 Pro (i.e. when connected to the QPR ProcessAnalyzer Service), only temporary tables (#) should be used. Note that global temporary tables (##) cannot be used in the SQL scripts, as using them is not allowed. When using QPR ProcessAnalyzer Xpress, these limitations do not apply.

Print Command in SQL Server

The Print SQL statement can be used to generate log entries into the script execution log.

Running Scripts

A script can be run in the following ways:

  • 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.
  • Running scripts from the Manage Scripts dialog
    1. On the QPR tab of the ribbon, 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.

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