AMPscript is Marketing Cloud Engagement's scripting language for emails, SMS and CloudPages. A few lines of it can turn a generic email into one that feels written for each reader. This guide goes past "Hello %%FirstName%%" and covers the patterns that make personalization reliable: safe lookups, loops, conditional content, formatting and what to do when data is missing.
The two ways to write AMPscript
AMPscript comes in blocks and inline expressions.
%%[
/* A block: logic, no output */
VAR @firstName, @greeting
SET @firstName = AttributeValue("FirstName")
IF Empty(@firstName) THEN
SET @greeting = "Hi there"
ELSE
SET @greeting = Concat("Hi ", ProperCase(@firstName))
ENDIF
]%%
<!-- Inline: prints a value -->
<p>%%=v(@greeting)=%%,</p> Do the work in blocks at the top of the email, and keep inline expressions short. Your content stays readable, and it's easy to see where each value comes from.
AttributeValue() is safer than a plain %%FirstName%%. If the field doesn't exist for that send, it returns an empty value instead of failing.
Look up a single value
Lookup() returns one column from the first row that matches. Because it returns the first match, use it with values that are unique in the data extension, like a subscriber key or customer ID.
%%[
VAR @subKey, @tier
SET @subKey = AttributeValue("_subscriberkey")
SET @tier = Lookup("Loyalty_Members", "Tier", "SubscriberKey", @subKey)
IF Empty(@tier) THEN
SET @tier = "Member"
ENDIF
]%%
<p>As a %%=v(@tier)=%% member, you get early access this week.</p> Always plan for "not found". Here, anyone without a loyalty row is treated as a regular member, and the email still makes sense.
Loop over several rows
LookupRows() returns every matching row, up to 2,000, in no particular order. When order matters, like "latest orders first", use LookupOrderedRows(). It takes the number of rows you want and a sort column with ASC or DESC.
%%[
VAR @subKey, @orders, @count, @i, @row, @product, @orderDate
SET @subKey = AttributeValue("_subscriberkey")
SET @orders = LookupOrderedRows("Orders", 3, "OrderDate DESC", "SubscriberKey", @subKey)
SET @count = RowCount(@orders)
]%%
%%[ IF @count > 0 THEN ]%%
<h2>Your recent orders</h2>
<ul>
%%[
FOR @i = 1 TO @count DO
SET @row = Row(@orders, @i)
SET @product = Field(@row, "ProductName")
SET @orderDate = FormatDate(Field(@row, "OrderDate"), "MMMM d, yyyy")
]%%
<li>%%=v(@product)=%% on %%=v(@orderDate)=%%</li>
%%[ NEXT @i ]%%
</ul>
%%[ ELSE ]%%
<p>Browse this week's new arrivals.</p>
%%[ ENDIF ]%% Two things to notice:
RowCount()is checked before the loop. An empty rowset gets its own message instead of an empty list.Row()andField()read values out of each row.
💡 Lookups are case-insensitive
Lookup, LookupRows and LookupOrderedRows don't care about upper or lower case in the search value. If you need an exact case match, use the CS versions, like LookupRowsCS().
Conditional content
Use IF, ELSEIF and ELSE to show different content to different people. Keep the conditions simple and put the most specific one first.
%%[
VAR @segment, @headline, @offer
SET @segment = AttributeValue("Segment")
IF @segment == "VIP" THEN
SET @headline = "Your VIP preview is open"
SET @offer = "25% off everything"
ELSEIF @segment == "New" THEN
SET @headline = "Welcome! Here's a little something"
SET @offer = "15% off your first order"
ELSE
SET @headline = "New arrivals are here"
SET @offer = "Free shipping this week"
ENDIF
]%%
<h1>%%=v(@headline)=%%</h1>
<p>%%=v(@offer)=%%</p> For a quick one-line choice, IIF() is handy:
%%=IIF(Empty(@firstName), "Hi there", Concat("Hi ", @firstName))=%% If a block gets long, move the variations into Content Builder blocks and choose the block with AMPscript, or use Dynamic Content in Content Builder so marketers can edit the rules without touching code.
Format values for people
Raw data rarely looks good in an email. A few functions fix most of it:
ProperCase("jOHN")gives "John"FormatDate(@date, "MMMM d, yyyy")gives a readable dateFormatNumber(@points, "N0")adds thousands separatorsConcat()joins text, andTrim()removes extra spaces
Stop the send when data is missing
Sometimes it's better not to send than to send something broken. RaiseError() stops processing. When the second parameter is true, it skips only the current subscriber and the send continues for everyone else.
%%[
VAR @subKey, @code
SET @subKey = AttributeValue("_subscriberkey")
SET @code = Lookup("Coupon_Assignments", "CouponCode", "SubscriberKey", @subKey)
IF Empty(@code) THEN
RaiseError("No coupon code for this subscriber", true)
ENDIF
]%% ⚠️ RaiseError is for errors, not segmentation
Salesforce recommends using RaiseError() only to handle errors. Build your audience with query activities and exclusion lists instead. Skipped subscribers still show up in tracking, and in a journey RaiseError() only removes the person from that one send, not from the journey.
AMPscript and Marketing Cloud Next
If you are planning a move to Marketing Cloud Next, check each function's availability. The reference pages now show whether a function works in Engagement, Next or both. For example, Lookup() became available in Next in the Summer '26 release, while LookupRows() and LookupOrderedRows() are listed as Engagement only.
Checklist
- Logic sits in blocks at the top, output stays inline
AttributeValue()is used for fields that might not exist- Every lookup has a fallback for "not found"
Lookup()searches on unique values only- Loops check
RowCount()first - Dates and numbers are formatted for people
RaiseError(..., true)is used only when data is truly missing