---
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.
---

# :fa-function: 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.*  

## :fa-list-check: 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 :Nodinite: [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 :Nodinite: servers to that SQL Server instance.

## :fa-code: 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.

## :fa-list-ol: 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
```

## :fa-circle-info: 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.

## :fa-hand-point-right: Next Steps

* :fa-server: [Configure SQL DB Lookup][SQL DB Lookup]  
* :fa-magnifying-glass-plus: [Add or manage Search Field][]  
* :fa-flask: [Test Your Expression][]  
* :fa-hard-drive: [Add or manage Log View][]  

## :fa-link-horizontal: Related Topics

* :fa-filter: [Extract functions][Extract Group]  
* :fa-file-csv: [csv][]  
* :fa-sitemap: [jsonPath][]  
* :fa-key: [jsonPathKey][]  
* :fa-text-slash: [regex][]  
* :fa-layer-group: [regexGroup][]  
* :fa-code: [xPath][]  
* :fa-code: [xPath2][]  
* :fa-function: [Formula][]  
* :fa-scroll: [Formula language rules][]  
* :fa-puzzle-piece: [Search Field Expressions][]  

[1]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Extract/Formula_sqllookup.gif

[Add or manage Log View]:{ndr}/Administration/1.%20Log%20Management/1.%20Log%20Views/Features/Add%20or%20Manage%20Log%20View.md
[Add or manage Search Field]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Add%20or%20manage%20Search%20Field.md
[body]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Source/body.md
[Configuration Database]:{ndr}/Core%20Services/Configuration%20Database/Overview.md
[Connection Strings]:{ndr}/Install%20and%20Update/Troubleshooting/SQL%20Server/Connection%20Strings.md
[Context]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Source/context.md
[csv]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/csv.md
[Extract Group]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Overview.md#extract
[Formula]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Overview.md
[Formula language rules]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Overview.md#fa-scroll-formula-language-rules
[jsonPath]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/jsonPath.md
[jsonPathKey]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/JsonPathKey.md
[Logging Service]:{ndr}/Core%20Services/Logging%20Service/Overview.md
[Order-to-Cash demo]:{ndr}/Repository%20Model/BPMs/Examples/Order-to-Cash/Generate-O2C-Demo-Data.md
[regex]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/regex.md
[regexGroup]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/regexGroup.md
[Run the CLI workflow]:{ndr}/Install%20and%20Update/Getting%20started/Version%207/Install%20Nodinite%20v7%20Steps/07%20-%20Install%20the%20Generated%20Package.md
[Search Field Expressions]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Overview.md
[SQL DB Lookup]:{ndr}/Install%20and%20Update/Getting%20started/Version%207/Install%20Nodinite%20v7%20Steps/03%20-%20Infrastructure.md#configure-sql-db-lookup
[Test Your Expression]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Add%20or%20manage%20Search%20Field.md#test-your-expression
[xPath]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/xPath.md
[xPath2]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/xPath2.md