Skip to content

A trigger on SQL Server has to be written inside EXEC, and the contract cannot write a statement with line breaks into a string constant #156

Open

Description

@CaffeinatedCoder

From spike D (#19, spikes/D.ScriptsAndBundles). Before a trigger ships on SQL Server: the row guard (#47) is the first. The throwaway SQL Server contributor of #119 writes no trigger, so it does not meet this.

Problem

A CREATE TRIGGER written as a statement fails in both scripts EF Core 11 generates for SQL Server. A migration's statements are one batch there. CREATE TRIGGER must start its batch, so migrations script fails (Msg 111), and it cannot stand inside the idempotent script's IF … BEGIN … END, so migrations script --idempotent fails (Msg 156). The batch does not compile and nothing of the migration is applied; sqlcmd exits 0 unless it is run with -b. A bundle applies the same migration. EF Core 11.0.0 writes the same two scripts as rc.3 (compared as text; the spike's consumer builds on 11.0.0 only without its reference to System.Security.Cryptography.Pkcs 9.0.23, NU1605 otherwise).

Inside EXEC(N'…') the trigger survives every path, and the contract can write that for a statement on one line only:

var trigger = sql.Fragment($"CREATE TRIGGER {name} ON {table} AFTER INSERT, UPDATE AS BEGIN … END");
sql.Statement($"EXEC({sql.Literal(trigger.ToString())})");

With line breaks in the statement, ISchemaDialect.Literal returns EF Core's literal for such a string, CONCAT(CAST(N'…' AS nvarchar(max)), nchar(10), N'…'), and EXEC takes constants and variables, no function call: Msg 102 on every path, the bundle included.

One constant with the line breaks inside it applies on every path. A contributor can write it only by doubling the quotes by hand, with the names written into the constant as they are. Inside a constant, "nothing reaches the DDL unquoted" does not hold: a name with a quote in it ends the constant.

Form, SQL Server 2022 migrations script --idempotent, twice bundle, twice
CREATE TRIGGER … Msg 111 Msg 156 applies
EXEC(N'…'), one line, through Literal applies applies applies
EXEC(…) with line breaks, through Literal Msg 102 Msg 102 Msg 102
EXEC(N'…'), line breaks inside the constant, quotes doubled by hand applies applies applies

A trigger a script creates through sqlcmd is not the trigger a bundle creates. SQL Server keeps with a trigger whether QUOTED_IDENTIFIER was on when it was created (sys.sql_modules.uses_quoted_identifier). Measured for the two EXEC forms that apply: 0 through sqlcmd's defaults, 1 through sqlcmd -I, through SqlClient and from a bundle. What follows from it, for a trigger that later writes to a table with a filtered index or an indexed view, is not measured.

Change, to decide

A way for a contributor to say "this statement, as a string constant": for instance a method of SchemaSql that takes a statement's fragment and returns the constant, written by the dialect, so that a quote in a name or in a literal is doubled and line breaks stay inside the constant. Going from a fragment to text through SqlFragment.ToString(), as the sample above does, is a way around the typed holes of the same kind as #109.

The other half: whether a provider's package refuses a statement that must start its batch when it is written outside EXEC. The kernel cannot know T-SQL's list.

Done when

A contributor written against the public contract creates a trigger with a body of several lines on SQL Server 2022. It applies from migrations script, from migrations script --idempotent applied twice and from a bundle run twice, and a name with a quote in it is escaped inside the constant. The sqlserver-trigger-exec-raw consumer of spike D is the measurement it replaces.

Activity

  1. added this to the 11.0.0 milestone on Nov 16, 2026
  2. added
    enhancement
    package/core
    package/sqlserver
    area/migrations
    on Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/migrationsenhancementpackage/corepackage/sqlserver

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions