RAISE_ERROR

Unconditionally raise a runtime error with a message.

Category: conditionalReturns: ANYDialect: Standard

Syntax

RAISE_ERROR(message)

Description

## Overview Raises a runtime error with the provided message when evaluated. The query terminates immediately with an exception. RAISE_ERROR is used inside conditional expressions (CASE, IF, WHEN) to enforce data-quality rules or signal unexpected conditions without falling back to a client-side check. Because RAISE_ERROR never returns a value in practice, the engine treats it as having a polymorphic return type so it can appear in any CASE or IF branch without causing a type mismatch. ## Behavior - Always raises when evaluated; no input produces a return value. - The message is included verbatim in the error output. - Placed inside a CASE/IF branch, it is only evaluated when that branch is selected. - The engine may still type-check the nominal return type of the enclosing expression; if so, RAISE_ERROR adapts to the expected type. ## Compatibility - RAISE_ERROR is not part of ANSI SQL. Equivalent patterns include RAISE EXCEPTION in procedural SQL and user-defined functions that throw. - Pairs naturally with ASSERT_TRUE for row-level and aggregate invariants.

Parameters

NameTypeDescription
messageSpecifies the error message text included in the raised exception. Must be a STRING expression.

Examples

-- Raise a simple error
SELECT RAISE_ERROR('Something went wrong');
-- Use inside a CASE expression as a validation guard
SELECT CASE WHEN status = 'VALID' THEN status
            ELSE RAISE_ERROR('Invalid status: ' || status) END AS checked_status
FROM (VALUES ('VALID'), ('INVALID')) AS t(status);
-- Combine with IF for a conditional abort
SELECT IF(amount < 0, RAISE_ERROR('Negative amount not allowed'), amount) AS amt
FROM (VALUES (100), (-5)) AS t(amount);
-- Dynamic error message
SELECT RAISE_ERROR('Row failed validation: id=' || CAST(id AS STRING)) FROM (VALUES (42)) AS t(id);
-- Realistic: enforce a unique-key invariant
SELECT CASE WHEN cnt = 1 THEN TRUE
            ELSE RAISE_ERROR('Duplicate natural key: ' || natural_key) END AS ok
FROM (SELECT natural_key, COUNT(*) AS cnt FROM ingestion.staging.daily_rows GROUP BY natural_key) t;

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →