How to Prompt AI to Write Better SFMC Queries

How to Prompt AI to Write Better SFMC Queries

Ever asked ChatGPT or Copilot to write an SFMC query and gotten something that looks perfect but breaks when you run it?

The problem is simple: AI tools generate modern SQL syntax that SFMC doesn't support. They'll give you CTEs, variables, STRING_SPLIT functions, and other features that just don't exist in SFMC's limited SQL environment.

The Solution

Train the AI on SFMC's limitations upfront. Copy this prompt and paste it into your conversation before asking for any SFMC queries:

PLAIN
SFMC SQL Query InstructionsOverview:
SFMC SQL is based on Microsoft SQL Server (T-SQL)
Only SELECT queries are allowed. Target Data Extensions (DEs) are configured in the UI
Query runs against Data Extensions and Data Views (e.g., _Sent, _Open)
Query Activities output to a DE in Overwrite, Append, or Update modes
Max runtime: 30 min; complex joins can timeout
Supported Features:Query Structure:
SELECT with columns or *. Use aliases to match target DE fields
WHERE, DISTINCT, TOP (n) with ORDER BY
Joins: INNER, LEFT/RIGHT, FULL OUTER joins supported (limit joins for performance)
UNION / UNION ALL supported (same column count)
Subqueries allowed in SELECT, FROM, WHERE
GROUP BY, HAVING supported with aggregates
CASE WHEN ... END and IIF(condition, true, false) supported
Window functions: ROW_NUMBER(), RANK(), NTILE() allowed (use in subqueries)
Functions:
String: LEN, LOWER/UPPER, SUBSTRING, CHARINDEX, LEFT/RIGHT, CONCAT, ISNULL, COALESCE
Date: GETDATE (CST), GETUTCDATE, DATEADD, DATEDIFF, DATEPART, EOMONTH, CONVERT(DATE, x)
Numeric: ABS, ROUND, FLOOR, % operator
Casting: CAST and CONVERT (prefer CONVERT for formatting)
Writing to Target DE:
Target DE cannot be in the FROM clause
Output columns must match target DE fields and types
Write modes: Overwrite (truncate+insert), Append (insert only), Update (upsert by PK)

How to Use It

  1. Copy the prompt above
  2. Paste it into ChatGPT, Claude, or Copilot
  3. Ask for your SFMC query

The AI will now generate queries that actually work in SFMC. No more debugging broken syntax or wondering why your perfectly formatted SQL fails to run.

This saves hours of trial and error. Trust me, I've been there.

Enjoyed this post?

Subscribe to get insights on marketing engineering, data-driven growth, and building better systems.

No spam. Unsubscribe anytime.

Comments (0)

You must be logged in to comment.

No comments yet. Be the first to comment!