# Data Driven

Data Driven Write Strategy is a set level functionality, which means that the entire incoming dataset must flow through it. It allows a user to stamp a database directive on the record so that when it reaches its destination, it will be loaded according to that directive to perform the specified write action. You can specify multiple rules within the properties of a *Data Driven* write strategy object. These rules are tried against each record from top to bottom. If a record passes the first rule, it will not be tried against the next rule(s).

### Use-Case

Assume a scenario in which *Orders* data from a *Database Table Source* is written to a *Database Table Destination*. We want to DELETE those records where *ShippedDate* is before the year 2000 and declare those records where *Freight* is less than 10 as ERROR. We will use the *Data Driven* write strategy object to achieve this task.

### How to Work with Data Driven Write Strategy

1. Drag-and-drop the *Data Driven* object from *Toolbox > Database Write Strategy > Data Driven* onto the dataflow designer and map the source data to the *Data Driven* object.

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2Fusho5olffzq1jO1x3Vr8%2Fdata-driven-gif.gif?alt=media\&token=c1a14fcd-221a-43cd-990c-5db5395e20b6)

2. Right-click on the header of the *Data Driven* object and select *Properties*.

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FlfGWmTI9ZJRJfsZjQwlX%2F1.png?alt=media\&token=29c09784-1124-4b81-981f-6cda43fe4d35)

3. A *Layout Builder* window will open where you can modify your layout. Click *Next*.

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FAeicZQIFjCICFRHiNmb2%2F2.png?alt=media\&token=80af2db8-7fb1-44d8-9c74-5a17e6656dc5)

4. The next window is the *Data Driven Write Strategy Conditions* window, where you can specify rules to route your data. Click on the *fx* option to enable the Expression Builder.

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2F1R8qiWRG4RrM7ZwC3Pnj%2FDD2.png?alt=media\&token=e3922858-67f8-4182-9651-282c22d5ba6c)

Once you select this option, the Expression Builder will be enabled.

5. Now, specify the following rules in the Expression Builder and select Database *Action Type* as *ERROR* for the *Freight rule* and *DELETE* for the *Date rule*.

* Year(ShippedDate) < 2000
* Freight < 10

<figure><img src="https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FWZNtRicEfTJ1u8lDX4rq%2FData-Driven%20Rules.png?alt=media&#x26;token=6a3b6e52-4183-457e-8f24-9517cd2b5153" alt=""><figcaption></figcaption></figure>

There are five Database actions: *Insert, Update, Delete, Skip* and *Error*. From these, you can select the action you want to be taken for a certain rule.

6. Once you are done specifying the rule(s), click *OK*.

You can now write your data to a *Database Table Destination*.

It is important to note here that while working with Database Write Strategies, *Single Port* is selected. Once you check the *Single Port* option in the *Database Table Destination* object, a box will appear in which you will have to specify a field for matching database record. In our case, we will select *OrderID*.

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FGCPOaT4t5ZVbNnzuCS43%2F4.png?alt=media\&token=47707394-4da4-46de-8348-32c70ee2f42c)

7. We have successfully configured the settings and built the layout.

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FFGZl0NirPp8yokklF3T4%2F5.png?alt=media\&token=9f8ecf10-b790-4a4b-9a93-4b18c5bdc38f)

8. Let’s preview the output.

*Data Driven* output:

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FpX2a9GhIIx5rlZgOmgJ3%2F6.png?alt=media\&token=9308e3b7-36dd-4e29-8ac3-84d734c04843)

You can see that Astera has appended an error message with the records where *Freight* is less than 10. You can create an error log of these records or load them into a database if you want.

{% hint style="info" %}
**Note:** Since, the *Freight rule* is our second rule, only those records which have failed against the first rule would be declared as an error. Which is why the error message only appears with records where both, *ShippedDate* is greater than 2000, and *Freight* is less than 10.
{% endhint %}

Now, whenever you access the same database table, you will see that the records where *ShippedDate* is before the year 2000 would be deleted.

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FVtdh9GFEtYbfz0GsMvZM%2F7.png?alt=media\&token=8c4f8aad-52d7-4756-8b52-ea61f3e642c0)

This concludes using the *Data Driven* Write Strategy in Astera.
