How to use LookupRows in AMPscript (and when to switch to Lookup or LookupOrderedRows)

List several Data Extension rows in an email with LookupRows, handle the no-results case and learn when to use Lookup or LookupOrderedRows.

Yellow Tutorial Marketing Cloud Engagement 3 min Published

When a subscriber can have several related rows in a Data Extension (orders, courses, appointments), you need to fetch all of them and build a list inside the email. That is what LookupRows is for.

The three lookup functions

Function What it returns When to use it
Lookup One value from one column, from the first matching row You only need one piece of data, such as the plan name
LookupRows A set of rows (rowset), up to 2,000 You need several rows
LookupOrderedRows An ordered rowset with a row limit You want "the 3 most recent orders"

In all three, the first parameter is the Data Extension name, not its external key.

LookupRows syntax

AMPscript
LookupRows("DataExtensionName", "Column1", "Value1" [, "Column2", "Value2", ...])

Each column/value pair is an equality filter, and the pairs are combined with "AND".

Example: the subscriber's orders

The Orders Data Extension has the columns Email, Number, Amount and Status.

AMPscript
%%[
SET @email  = AttributeValue("EmailAddress")
SET @orders = LookupRows("Orders", "Email", @email, "Status", "Shipped")
SET @total  = RowCount(@orders)

IF @total > 0 THEN
]%%
<p>You have %%=v(@total)=%% order(s) on the way:</p>
<ul>
%%[
  FOR @i = 1 TO @total DO
    SET @row    = Row(@orders, @i)
    SET @number = Field(@row, "Number")
    SET @amount = Field(@row, "Amount")
]%%
  <li>Order %%=v(@number)=%%: $%%=FormatNumber(@amount, "N2", "en-US")=%%</li>
%%[
  NEXT @i
]%%
</ul>
%%[ ELSE ]%%
<p>No orders on the way right now.</p>
%%[ ENDIF ]%%

The pieces:

  • RowCount(@rowset) tells you how many rows came back;
  • Row(@rowset, @i) gets row number @i (counting starts at 1);
  • Field(@row, "Column") reads a column from that row.

Sort and limit with LookupOrderedRows

To show only the 3 most recent orders, switch to LookupOrderedRows, which takes the row count and the sort order before the filters:

AMPscript
%%[
SET @latest = LookupOrderedRows("Orders", 3, "OrderDate DESC", "Email", @email)
SET @total  = RowCount(@latest)
]%%

The sort order accepts more than one column, separated by commas: "OrderDate DESC, Number ASC".

Performance and limits

  • LookupRows returns at most 2,000 rows. If you need more than that, an email is not the right place for the list.
  • Every lookup function runs once per subscriber in the send. In large sends, avoid repeating the same lookup: store the result in a variable and reuse it.
  • Filtering on columns that are part of the Data Extension's primary key is usually faster.

Summary

Use Lookup for a single value, LookupRows for several rows and LookupOrderedRows when order or limit matters. Check RowCount before looping through the result and remember the 2,000-row limit.

Related

Related
Title Type Platform Belt Reading
AttributeValue and empty fields: AMPscript personalization that never breaks a send Tutorial Marketing Cloud Engagement White 3 min
Data Views in SQL: who opened or clicked in the last 30 days Tutorial Marketing Cloud Engagement Green 3 min
Deduplicate records with ROW_NUMBER() in a Query Activity Snippet Marketing Cloud Engagement Green 2 min
Marketing Cloud REST API: OAuth 2.0 authentication and writing to a Data Extension Tutorial Marketing Cloud Engagement Brown 3 min