Given what you've described, it would be worth looking at this guide to claiming coupon codes by the excellent Adam Spriggs: https://sprignaturemoves.com/coupon-claiming-in-ampscript/
This utilises the
ClaimRow function in AMPscript.
You'll need a data extension where the field for your coupon/discount code is the primary key (you presumably already have this). You need two extra nullable fields - IsClaimed (this should be boolean, with a default value of False) and EmailAddress (or SubscriberKey if you want to associate with Subscriber Keys - you'll need to update the AMPscript accordingly if so).
Then, here's the AMPscript Adam provides:
var @em, @couponRow, @couponCode
if _messagecontext == "PREVIEW" then
set @couponCode = "XX TEST XX"
/* include your sendable attribute/column here */
set @em = AttributeValue("emailAddr")
set @couponRow = ClaimRow("CouponsCodes", "IsClaimed", "EmailAddress", @em)
if empty(@couponRow) then
/* You can do other error handling here if you want.*/
/* This aborts the send */
RaiseError("No coupons available", false)
SET @couponCode = Field(@couponRow, "CouponCode")
You can then print the retrieved value in your email:
Here's your coupon code: %%=v(@couponCode)=%%
When a code is claimed, the
IsClaimed field in your Codes data extension will be marked as
True and the EmailAddress or SubscriberKey column (depending on what you choose to use) will be populated with the relevant value for the subscriber that claimed the code.
If you're putting this in an automated campaign/journey, you should also think about setting up alerts when you're close to having no codes left, so you know to replenish the codes DE.