Skip to content

Feature, Public API | Enable SqlDataRecord support for TVPs with computed columns - #4654

Draft
edwardneal wants to merge 5 commits into
dotnet:mainfrom
edwardneal:feat/tvp-with-computed-column
Draft

Feature, Public API | Enable SqlDataRecord support for TVPs with computed columns#4654
edwardneal wants to merge 5 commits into
dotnet:mainfrom
edwardneal:feat/tvp-with-computed-column

Conversation

@edwardneal

Copy link
Copy Markdown
Contributor

Description

The MS-TDS spec supports a client recognising that a field in a table-valued parameter might well be computed by the server. An example of a TVP which does this is:

CREATE TYPE [dbo].[MyType] AS TABLE
(
    [Value1] INT NOT NULL,
    [Value2] INT NOT NULL,
    [Added] AS ([Value1] + [Value2])
)

Under such circumstances, the client can set the fComputed bit in the TVP_COLMETADATA definition. We also need to set the fDefault bit - without setting this, SQL Server expects the client to send a value for the computed column, and throws an exception indicating that I'm trying to overwrite a computed column with a value when we send one.

Once the fComputed bit is set, the computed columns in the TVP are populated by SQL Server as we'd expect.

Enabling the client to set this necessitates a public API change of some form. In this case, I've added an IsComputed property to SqlMetaData:

public sealed class SqlMetaData
{
    public bool IsComputed { get; init; }
}

This is a slightly different approach to the public API than the current set of constructors, but I think it's the most practical one - I think most of the constructors would want to be able to specify whether the field is computed, and we'd essentially double the number of constructors if adding a separate parameter to all of them.

I'm leaving this PR in draft until there's time for the public API to be reviewed. Once we're happy with it, I'll adjust the API surface accordingly.

@mdaigle - this intersects with some of the work you'd been doing on the issue and provides a fix for SqlDataRecord.

Issues

Fixes #3242. It doesn't introduce anything to enable DataTable- or DbDataReader-based support, but I don't think there's a way to enable that support client-side.

Testing

New manual tests have been added, and all pass.

Note that a computed column will (by definition) always have a default value, so setting IsComputed also implies the same effect as UseServerDefault.
Note that the previous commit means that IsComputed will also result in the fDefault bit being set.
This is an init property, so requires a shim on net462 and netstandard.
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

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

Labels

None yet

Projects

Status: To triage

Development

Successfully merging this pull request may close these issues.

Limited SQL Computed column support when using SqlParameter.Structured

1 participant