Skip to content

Render TVP type name correctly in ToQueryString for SQL Server - #38251

Merged
AndriySvyryd merged 2 commits into
dotnet:mainfrom
m-x-shokhzod:fix/sqlserver-tvp-querystring
Jun 9, 2026
Merged

AndriySvyryd merged 2 commits into
dotnet:mainfrom
m-x-shokhzod:fix/sqlserver-tvp-querystring

Conversation

@m-x-shokhzod

Copy link
Copy Markdown
Contributor

When a SqlParameter has SqlDbType.Structured (table-valued parameter), SqlServerQueryStringFactory previously emitted invalid T-SQL of the form:

DECLARE @data structured = ;

— both the literal "structured" instead of the actual TVP type name, and a trailing = ; that cannot be valid since TVPs cannot be initialized inline in T-SQL.

This change:

  • Uses sqlParameter.TypeName for structured parameters so the actual TVP type appears in the DECLARE
  • Skips the = <value> segment for TVPs since they cannot be initialized inline; the DECLARE alone keeps ToQueryString() output pasteable into SSMS for plan analysis

Other parameter rendering is unchanged.

Before:

DECLARE @data structured = ;

SELECT * FROM @data WHERE [Region] = @region

After:

DECLARE @data dbo.IntList;
DECLARE @region nvarchar(32) = N'EMEA';

SELECT * FROM @data WHERE [Region] = @region

Tests added in SqlServerQueryStringFactoryTest:

  • Returns_command_text_unchanged_when_no_parameters
  • Renders_DECLARE_with_value_for_simple_int_parameter (regression guard)
  • Renders_DECLARE_with_TypeName_and_no_value_for_structured_parameter
  • Renders_structured_parameter_alongside_other_parameters

The fix shape was suggested by the issue reporter and the direction was confirmed by @roji in the issue thread.

Fixes #33849

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes SQL Server ToQueryString() output for table-valued parameters (TVPs) by rendering the actual TVP type name (via SqlParameter.TypeName) and omitting inline initialization (since TVPs can’t be assigned inline in T-SQL), keeping output pasteable into SSMS for plan inspection.

Changes:

  • Update SqlServerQueryStringFactory to emit DECLARE @p <tvp_type>; (no = <value>) for SqlDbType.Structured parameters.
  • Update type-name rendering for SqlDbType.Structured to use SqlParameter.TypeName instead of the literal structured.
  • Add unit tests covering no-parameter behavior, normal scalar parameter rendering, and mixed TVP + scalar scenarios.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/EFCore.SqlServer/Query/Internal/SqlServerQueryStringFactory.cs Renders structured parameters using their TVP type name and skips inline assignment to avoid invalid T-SQL.
test/EFCore.SqlServer.Tests/Query/SqlServerQueryStringFactoryTest.cs Adds coverage for scalar DECLARE rendering and the corrected TVP DECLARE output (including mixed-parameter commands).

m-x-shokhzod added a commit to m-x-shokhzod/efcore that referenced this pull request May 31, 2026
Addresses Copilot review on dotnet#38251: Assert.DoesNotContain("structured", result)
is too broad and could fail for unrelated reasons (command text or a TVP type
name containing the substring "structured"). Match the exact regression instead
— the old code emitted `DECLARE @DaTa structured;` — so assert the DECLARE line
specifically. This still catches the bug but can't false-positive on a type name
like dbo.StructuredList.
@AndriySvyryd

Copy link
Copy Markdown
Member

@m-x-shokhzod Please rebase on the current main

When a SqlParameter has SqlDbType.Structured (TVP), SqlServerQueryStringFactory
emitted "DECLARE @p1 structured = ;" — both the literal "structured" type and
a trailing "= ;" that's not valid T-SQL.

- Use sqlParameter.TypeName for structured parameters so the actual TVP
  type name appears in the DECLARE
- Skip the "= <value>" segment for TVPs since they cannot be initialized
  inline in T-SQL; the DECLARE alone keeps the output pasteable into
  SSMS for plan analysis

Other parameter rendering is unchanged.

Fixes dotnet#33849
Addresses Copilot review on dotnet#38251: Assert.DoesNotContain("structured", result)
is too broad and could fail for unrelated reasons (command text or a TVP type
name containing the substring "structured"). Match the exact regression instead
— the old code emitted `DECLARE @DaTa structured;` — so assert the DECLARE line
specifically. This still catches the bug but can't false-positive on a type name
like dbo.StructuredList.
@m-x-shokhzod
m-x-shokhzod force-pushed the fix/sqlserver-tvp-querystring branch from 780e25d to c73642f Compare June 9, 2026 10:57
@AndriySvyryd
AndriySvyryd merged commit 0925821 into dotnet:main Jun 9, 2026
13 checks passed
@AndriySvyryd

Copy link
Copy Markdown
Member

Thanks for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ToQueryString with Table Valued Parameter is missing the type name

3 participants