Skip to content

sp_HumanEvents: fix stray AS sqlhandle in blocking-table INSERT (#785)#787

Merged
erikdarlingdata merged 1 commit into
devfrom
fix/humanevents-sqlhandle-double-alias-785
May 11, 2026
Merged

sp_HumanEvents: fix stray AS sqlhandle in blocking-table INSERT (#785)#787
erikdarlingdata merged 1 commit into
devfrom
fix/humanevents-sqlhandle-double-alias-785

Conversation

@erikdarlingdata
Copy link
Copy Markdown
Owner

Summary

  • Follow-up to sp_HumanEvents: fix view creation when @output_schema_name is not dbo (#785) #786. With the schema-replace bug fixed, the reporter on [BUG] sp_HumanEvents fails when using a schema name other than dbo. #785 advanced to a new error from the blocking-table INSERT path:

    Msg 156, Level 15, State 1, Line 79 — Incorrect syntax near the keyword 'AS'.

  • Root cause: the Sept 2024 "Cursor variables" refactor (9d3cff3) switched column aliases from `expr AS alias` to `alias = expr`, but for one column it left both forms in place:

    ```sql
    sqlhandle =
    CONVERT
    (
    varbinary(64),
    bd.value('(process/executionStack/frame/@sqLhandLe)[1]', 'nvarchar(260)'),
    1
    ) AS sqlhandle,
    ```

    T-SQL rejects `alias = expr AS alias`. Dropped the trailing `AS sqlhandle`.

  • Was masked for ~14 months because anyone using a non-dbo `@output_schema_name` hit the schema-replace bug first; dbo users probably weren't exercising the blocking + table-output path.

Test plan

  • Run with non-dbo schema and `@event_type = N'blocking'` (or default), confirm the blocking INSERT executes cleanly
  • Run with dbo schema and same params, confirm no regression

Refs #785

🤖 Generated with Claude Code

)

The blocking-table INSERT's dynamic SQL had a leftover from the Sept
2024 "Cursor variables" refactor (9d3cff3) that switched column aliases
from "expr AS alias" to "alias = expr". One column kept both forms:

    sqlhandle =
        CONVERT
        (
            varbinary(64),
            bd.value(''(process/executionStack/frame/@sqLhandLe)[1]'', ...),
            1
        ) AS sqlhandle,

T-SQL rejects the combination — you get "Incorrect syntax near the
keyword 'AS'." on every blocking-table insert.

This was masked by #785's schema-replace bug: anyone using a non-dbo
@output_schema_name died at view creation before ever reaching the
blocking-table INSERT. With that fixed in #786, the reporter advanced
to this next error.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@erikdarlingdata erikdarlingdata merged commit aa02e4d into dev May 11, 2026
5 checks passed
@erikdarlingdata erikdarlingdata deleted the fix/humanevents-sqlhandle-double-alias-785 branch May 11, 2026 12:12
erikdarlingdata added a commit that referenced this pull request May 12, 2026
) (#787)

The blocking-table INSERT's dynamic SQL had a leftover from the Sept
2024 "Cursor variables" refactor (9d3cff3) that switched column aliases
from "expr AS alias" to "alias = expr". One column kept both forms:

    sqlhandle =
        CONVERT
        (
            varbinary(64),
            bd.value(''(process/executionStack/frame/@sqLhandLe)[1]'', ...),
            1
        ) AS sqlhandle,

T-SQL rejects the combination — you get "Incorrect syntax near the
keyword 'AS'." on every blocking-table insert.

This was masked by #785's schema-replace bug: anyone using a non-dbo
@output_schema_name died at view creation before ever reaching the
blocking-table INSERT. With that fixed in #786, the reporter advanced
to this next error.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
erikdarlingdata added a commit that referenced this pull request May 12, 2026
* sp_QuickieStore: add 'failed' to @execution_type_desc that bundles aborted+exception (#784)

When @execution_type_desc = N'failed', the dynamic WHERE clause emits
qsrs.execution_type_desc IN (N'aborted', N'exception') instead of the
single-value parameter binding. Validation, RAISERROR text, and the
parameter docs (description / valid inputs) updated accordingly. The
three original values continue to use the existing parameterized = form.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Fix sp_HumanEvents view creation in non-dbo output schemas (#785) (#786)

The view-creation block stores each view's text as a binary literal in
#view_check.view_definition, then does a 3-way REPLACE to retarget it
to @output_database_name / @output_schema_name. The middle REPLACE was
searching for the bracketed form:

    N'[dbo]' + '.' + QUOTENAME(vc.view_name)

But the stored view definitions were rewritten to the unbracketed form
("CREATE VIEW dbo.HumanEvents_Blocking ...") back in October 2023 and
the REPLACE search pattern was never updated to match. So for any
@output_schema_name other than 'dbo', the schema swap silently no-op'd
and the resulting CREATE/ALTER VIEW statement still referenced 'dbo',
which doesn't exist in the user's output database — leading to:

    Msg 208, Level 16, State 6, Procedure HumanEvents_Blocking
    Invalid object name 'dbo.HumanEvents_Blocking'.

Fix is one line: search for "dbo." + view_name (unbracketed) to match
what's actually in the stored definitions. View bodies don't contain
"dbo.<view_name>" anywhere else, so there's no collision risk.

Fixes #785

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* sp_HumanEvents: drop stray AS sqlhandle from blocking-table INSERT (#785) (#787)

The blocking-table INSERT's dynamic SQL had a leftover from the Sept
2024 "Cursor variables" refactor (9d3cff3) that switched column aliases
from "expr AS alias" to "alias = expr". One column kept both forms:

    sqlhandle =
        CONVERT
        (
            varbinary(64),
            bd.value(''(process/executionStack/frame/@sqLhandLe)[1]'', ...),
            1
        ) AS sqlhandle,

T-SQL rejects the combination — you get "Incorrect syntax near the
keyword 'AS'." on every blocking-table insert.

This was masked by #785's schema-replace bug: anyone using a non-dbo
@output_schema_name died at view creation before ever reaching the
blocking-table INSERT. With that fixed in #786, the reporter advanced
to this next error.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Create Row Goals.sql

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant