---
title: "Formula - sqllookup: Look Up Values in a SQL Server Table"
description: The sqllookup Formula function looks up a value in a SQL Server reference table through a named SQL DB Lookup connection string. Includes prerequisites, syntax, parameters, and city-to-area-code examples.
---

# <i class="fal fa-function" aria-hidden="true"></i> Formula - sqllookup

The `sqllookup` [Formula][] function looks up the content value in a SQL Server reference table and returns the value of another column from the matching row, for example the area code for a city name.

![Look up the area code of an order's ship-to city with sqllookup, animated walkthrough (7.12)][1]  
*Animated walkthrough: on the Order Received event of order ORD-10005 in the [Order-to-Cash demo][], `sqllookup` takes the ship-to city from the body with [jsonPath][] and returns `08`, the area code of Stockholm, from the table `AreaCodes`. Looking up the customer name instead finds no row: the [Test Expression][Test Your Expression] tab shows a warning, *The Expression yields no result*, not an error.*  

## <i class="fal fa-list-check" aria-hidden="true"></i> Prerequisites

* A **SQL DB Lookup** entry exists: a **Name** and a **Connection String** for the reference database, such as `Server=sql01;Database=Directory;Integrated Security=true;TrustServerCertificate=true` (see [Connection Strings][]). Add it in the **SQL DB Lookup** section of the Portal's Infrastructure step ([Configure SQL DB Lookup][SQL DB Lookup]), or provide it with the CLI when you install or update ([Run the CLI workflow][]); the [Logging Service][] gets it as a named connection string. The **Name** is the first `sqllookup` parameter. The Test Expression tab reads the settings on each test, so a new entry needs no restart to test.
* The connection string doesn't point to the <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> [Configuration Database][]. A lookup through it is refused (*Lookups to the Nodinite Configuration Database are not allowed*), and an unknown name is an error (*No connection string found for key: …*).
* The account in the connection string can read the database and table you look up in.
* The network allows access from the <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> servers to that SQL Server instance.

## <i class="fal fa-code" aria-hidden="true"></i> Syntax

```text
sqllookup('connectionStringId', 'database', 'table', 'searchColumn', 'resultColumn', content)
```

### Parameters

The parameters are positional; give all six, in this order.

|Parameter|Required|Description|  
|---|---|---|  
|`'connectionStringId'`|Yes|The **Name** of the SQL DB Lookup entry, such as `'SqlDbLookupLocal'`. Don't rename an entry that expressions already use.|  
|`'database'`|Yes|The database that holds the reference table.|  
|`'table'`|Yes|The reference table, such as `'AreaCodes'`.|  
|`'searchColumn'`|Yes|The column to compare with the content value, such as `'Name'`.|  
|`'resultColumn'`|Yes|The column whose value is returned, such as `'AreaCode'`.|  
|`content`|Yes|The value to look up: [body][], [context][Context], or the result of another function (see [Formula language rules][]).|  

### Returns

The `resultColumn` value of the row whose `searchColumn` equals the content value. When several rows match, every value is returned. When no row matches, nothing is returned, not an error: the Test Expression tab shows *Processed with warning: The Expression yields no result*.

Each content value runs one parameterized query, `SELECT [resultColumn] FROM [table] WHERE [searchColumn] = @value`: the table and column names are quoted as names, and the content value is passed as a parameter.

## <i class="fal fa-list-ol" aria-hidden="true"></i> Examples

All examples use this reference table in the database `Directory`:

```sql
CREATE TABLE dbo.AreaCodes (
  Name      NVARCHAR(128) NOT NULL,
  AreaCode  NVARCHAR(8)   NOT NULL
);

INSERT INTO dbo.AreaCodes (Name, AreaCode) VALUES ('KARLSTAD', '054'), ('STOCKHOLM', '08');
```

### Example 1: City name in the body to area code

#### Input

```text
KARLSTAD
```

#### Formula

```text
sqllookup('SqlDbLookupLocal', 'Directory', 'AreaCodes', 'Name', 'AreaCode', body)
```

#### Result

```text
054
```

### Example 2: City name in a context property to area code

#### Input

|Key|Value|  
|---|---|  
|city|STOCKHOLM|  

#### Formula

```text
sqllookup('SqlDbLookupLocal', 'Directory', 'AreaCodes', 'Name', 'AreaCode', context('city'))
```

#### Result

```text
08
```

## <i class="fal fa-circle-info" aria-hidden="true"></i> Notes

* Each evaluation queries SQL Server, during logging and during re-index. Index the search column (for example `CREATE NONCLUSTERED INDEX IX_AreaCodes_Name ON dbo.AreaCodes(Name);`) and keep the table compact.
* Use a read-only account in the connection string.

## <i class="fal fa-hand-point-right" aria-hidden="true"></i> Next Steps

* <i class="fal fa-server" aria-hidden="true"></i> [Configure SQL DB Lookup][SQL DB Lookup]  
* <i class="fal fa-magnifying-glass-plus" aria-hidden="true"></i> [Add or manage Search Field][]  
* <i class="fal fa-flask" aria-hidden="true"></i> [Test Your Expression][]  
* <i class="fal fa-hard-drive" aria-hidden="true"></i> [Add or manage Log View][]  

## <i class="fal fa-link-horizontal" aria-hidden="true"></i> Related Topics

* <i class="fal fa-filter" aria-hidden="true"></i> [Extract functions][Extract Group]  
* <i class="fal fa-file-csv" aria-hidden="true"></i> [csv][]  
* <i class="fal fa-sitemap" aria-hidden="true"></i> [jsonPath][]  
* <i class="fal fa-key" aria-hidden="true"></i> [jsonPathKey][]  
* <i class="fal fa-text-slash" aria-hidden="true"></i> [regex][]  
* <i class="fal fa-layer-group" aria-hidden="true"></i> [regexGroup][]  
* <i class="fal fa-code" aria-hidden="true"></i> [xPath][]  
* <i class="fal fa-code" aria-hidden="true"></i> [xPath2][]  
* <i class="fal fa-function" aria-hidden="true"></i> [Formula][]  
* <i class="fal fa-scroll" aria-hidden="true"></i> [Formula language rules][]  
* <i class="fal fa-puzzle-piece" aria-hidden="true"></i> [Search Field Expressions][]  

[1]:https://docs.nodinite.com/media/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Extract/Formula_sqllookup.gif

[Add or manage Log View]:https://docs.nodinite.com/administration/log-management/log-views/features/add-or-manage-log-view/
[Add or manage Search Field]:https://docs.nodinite.com/administration/log-management/search-fields/features/add-or-manage-search-field/
[body]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/formula/source/body/
[Configuration Database]:https://docs.nodinite.com/core-services/configuration-database/overview/
[Connection Strings]:https://docs.nodinite.com/install-and-update/troubleshooting/sql-server/connection-strings/
[Context]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/formula/source/context/
[csv]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/formula/extract/csv/
[Extract Group]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/formula/overview/#extract
[Formula]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/formula/overview/
[Formula language rules]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/formula/overview/#fa-scroll-formula-language-rules
[jsonPath]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/formula/extract/jsonpath/
[jsonPathKey]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/formula/extract/jsonpathkey/
[Logging Service]:https://docs.nodinite.com/core-services/logging-service/overview/
[Order-to-Cash demo]:https://docs.nodinite.com/repository-model/bpms/examples/order-to-cash/generate-o2c-demo-data/
[regex]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/formula/extract/regex/
[regexGroup]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/formula/extract/regexgroup/
[Run the CLI workflow]:https://docs.nodinite.com/install-and-update/getting-started/version-7/install-nodinite-v7-steps/07-install-the-generated-package/
[Search Field Expressions]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/overview/
[SQL DB Lookup]:https://docs.nodinite.com/install-and-update/getting-started/version-7/install-nodinite-v7-steps/03-infrastructure/#configure-sql-db-lookup
[Test Your Expression]:https://docs.nodinite.com/administration/log-management/search-fields/features/add-or-manage-search-field/#test-your-expression
[xPath]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/formula/extract/xpath/
[xPath2]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/formula/extract/xpath2/