Home > Data Services > Static Variables in Data Services Functions

Static Variables in Data Services Functions


Pitfalls and Opportunities

Data Services functions work in pretty much the same way as most programming language functions do. They take input variables, do some work, and then return a value. Functions in Data Services can even return multiple values, which is a necessary feature since it has no array or hashtable structures found in your purer “functions only return one value” languages, like JAVA, C++, or Python.

In most function-or object-based programming languages, variables that are local to the function are reinitialized (or force the coder to reinitialize) for every instance of the function. These are called dynamic variables. Many languages allow an explicit override of this behavior. But, in Data Services, these internal variables keep their state between instances by default, making them always static.

Example

In this example, a simple row generation ( 3 rows) is hooked to a query transform with the following custom function:

static variables 1

Here’s the mapping with the function:

static variables 2

Our results:

static variables 3

The first time the function is called, our variable is null, so the conditional in the function (returning 1) is not called. The variable is then set, and 0 is returned. On subsequent calls, the interpreter remembers the value of our variable, and triggers the conditional statement to pass.

Therefore, in normal circumstances, be sure to explicitly re-initialize local variables at the top of the function.
This is a good practice anyway, and instances like this remind you why.

To download full PDF and Continue Reading…

Britton HeadshotAbout Britton Gray
Britton Gray has been working in software development and Business Intelligence for the past fourteen years, and has been working with SAP BusinessObjects reporting, analytics, and information management tools for six years. He is certifies in Data Integrator and Web Intelligence, and enjoys developing end-to-end BI solutions that enable customers to gain new insights into their data.

Categories: Data Services
  1. Ajit
    April 7, 2016 at 11:29 am

    Thanks a lot for this information. This has now cleared my confusion. Do you have any post on Local and Global variables defined at job level?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: