Run custom SQL writes
A schema operation requires SQL that Prisma's model API does not express directly.
Prisma · Module
$executeRaw runs one raw SQL operation and returns the number of affected rows, not records. Its tagged-template form supports parameterized values for safer execution.
Use Prisma Client's tagged-template method to execute one raw SQL write operation against a relational database. It returns the affected-row count rather than records, while interpolated values are escaped and sent as prepared-statement parameters.
$executeRaw<T = unknown>(query: TemplateStringsArray | Prisma.Sql, ...values: any[]): PrismaPromise<number>;| Name | Type | Description |
|---|---|---|
query | TemplateStringsArray | Prisma.Sql | The SQL template or a Prisma.Sql object representing the raw statement. |
values | any[] | Values interpolated into the raw SQL statement. |
PrismaPromise<number>, resolving to the number of affected rows.
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
async function main() {
const affected = await prisma.$executeRaw`
UPDATE User
SET active = true
WHERE emailValidated = true
`;
console.log(affected);
}
main()
.catch(console.error)
.finally(() => prisma.$disconnect());Runs a parameter-safe raw SQL update and logs the number of affected rows.
A schema operation requires SQL that Prisma's model API does not express directly.
An UPDATE or DELETE should report how many rows changed.
A write uses interpolated application values.
$executeRaw executes raw SQL intended for operations such as UPDATE or DELETE and resolves to the number of affected rows. It does not return database records.
Follow-up: How should application values be supplied? Use the tagged-template form so Prisma escapes variables and sends the statement as a prepared statement.
Its signature accepts a template-string array or Prisma.Sql plus variadic values and returns a PrismaPromise<number>. The resolved number represents affected rows.
Follow-up: What limitation applies to the SQL text? One call can execute only one query, so multiple semicolon-separated statements are unsupported.
The recommended form is a tagged template, for example prisma.$executeRawUPDATE ... WHERE id = ${id}. Tagged templates allow variables to be escaped and queries to be sent as prepared statements.
Follow-up: Why avoid manually concatenating values? String construction can undermine the protections provided by parameterized tagged-template execution.
$queryRaw$executeRawUnsafePrisma.SqlTypedSQL