# How to perform an Update operation in Leapwork using a Database Block?

## Introduction
There are various scenarios where a user wants to connect to any database (using DSN) in order to validate the data getting stored in the database. There are scenarios where you need to insert data into a database through automation. You can achieve this scenario by executing different database commands using Leapwork's Database block. You will learn how to perform an UPDATE operation in Leapwork using a Database Block.

## Perform Update Operation
To begin with, we need to first check some of the prerequisites. For example - setting up ODBC drivers. See [Access SQL-Database via ODBC driver in Leapwork](https://support.leapwork.com/hc/en-us/articles/360000006451-Access-SQL-Database-via-ODBC-driver-in-Leapwork).

Please note that we will be using SQL Server Database to execute the commands in this article.

The input fields: **Connection** & **Query** are mandatory fields and should be populated as below:

### Connection
The "**Data Source Name**" of configured ODBC connection on the computer where the test case will run.

### Query
The query statement will be executed on the excel file. It has a special syntax which is an ODBC-compliant SQL statement. The dynamic fields can be specified and used as Tokens while creating query statements.

**Note: -** We can execute the below query (either through Leapwork or directly in the SQL Server Query Manager) to create a new table in the Database.

```sql
create table LW_USER_DETAILS([ID][int],[FirstName][varchar](25),[LastName][varchar](25),[Email][varchar](25),[Address][varchar](50),[Department][varchar](25))
```

Once the user provides both the above details correctly, click **Refresh** on the Database blocks, it loads up the respective columns present in the table.

In case, any of the 2 provided input fields, i.e. Connection or Query is incorrect, Leapwork throws an error after clicking on the "Refresh" button.

So, once the connection is successfully connected, it is time to say that the user is up and ready to execute the **UPDATE query** through Leapwork.

As per the above image, the method used is the **_First Row_** which will insert/update the values in the database. The other 2 options like **_Iterate_** and **_Row Index_** can also be used as per the scenarios which will read/insert the values from/to and any specific row index provided respectively.

In the image above we can also see the **Log Message** **Block** is fetching dynamic values from the **Database Block**.

The dynamic fields have been specified and used as **_Tokens_** in the Log message block while fetching dynamic contents from the Database block.

Furthermore, if the query is not provided correctly, then the **"Error" connector** will be triggered.

## Quick Start
1. Download the attached **UPDATE.zip** file.
2. Import the attached use case, UPDATE.zip , in the Leapwork Studio. Please refer to [How to Export and Import automation flows.](https://support.leapwork.com/s/article/HowtoExportandImportautomationflows63307ce385193)
3. Open the use case with the title, "Update Operation".

After the flow has been imported, you can move the flow to any specific folder and then execute the same to see the behavior.
Please refer to the following article for more: [Organizing Daily Work in Leapwork.](https://support.leapwork.com/hc/en-us/articles/360015100539-Best-Practices-Organizing-Daily-Work-in-Leapwork)

## Attachments
[UPDATE.zip](https://leapworkaps.file.force.com/sfc/dist/version/download/?oid=00D7Q000003Qjj6&ids=0687Q000004sBe5&d=%2Fa%2F7Q000000cBiK%2FbDekjJatcX5h7ybRLQN8G94kve_Q_GzgCGETnOgU6s4&asPdf=false)

If you have any questions, please reach out to our [Priority Support](mailto:prioritysupport@leapwork.com) team.
