Skip to content

SQL Bulk Copy Fails on SQL Server 2016 / 130 #3714

Closed
1 of 1 issue completed
Closed

Description

@nhart12

Describe the bug

Since this PR #3590
SQL Bulk copy fails on SQL Server 2016. It may need to utilize dynamic sql instead of the IF statement because the query won't compile on older versions.

Invalid column name 'graph_type'.

To reproduce

To reproduce simply run the following query on an older version of SQL

SELECT @@TRANCOUNT;
DECLARE @Column_Names NVARCHAR(MAX) = NULL;
IF EXISTS (SELECT TOP 1 * FROM sys.all_columns WHERE [object_id] = OBJECT_ID('sys.all_columns') AND [name] = 'graph_type')
BEGIN
    SELECT @Column_Names = COALESCE(@Column_Names + ', ', '') + QUOTENAME([name]) FROM {CatalogName}.[sys].[all_columns] WHERE [object_id] = OBJECT_ID('{escapedObjectName}') AND COALESCE([graph_type], 0) NOT IN (1, 3, 4, 6, 7) ORDER BY [column_id] ASC;
END
ELSE
BEGIN
    SELECT @Column_Names = COALESCE(@Column_Names + ', ', '') + QUOTENAME([name]) FROM {CatalogName}.[sys].[all_columns] WHERE [object_id] = OBJECT_ID('{escapedObjectName}') ORDER BY [column_id] ASC;
END

Expected behavior

Bulk copy to succeed on older versions

Further technical details

Microsoft.Data.SqlClient version: found on main branch builds
.NET target: .net 8
SQL Server version: SQL Server 2016

Additional context

Sub-issues

Sub-issues

1 of 1 Issues completed
Loading 1 sub-issues

Activity

  1. changed the title [-]SQL Bulk Copy Fails on SQL Server 130[/-] [+]SQL Bulk Copy Fails on SQL Server 2016 / 130[/+] on Oct 23, 2025
  2. nhart12 commented on Oct 23, 2025

    @nhart12
    Author

    Maybe it's expected for > 7.0 to drop support for this version of sql server.. in which case the IF statement in SQL checking for that column seems redundant

  3. ErikEJ commented on Oct 24, 2025

    @ErikEJ
    Contributor
  4. edwardneal commented on Oct 24, 2025

    @edwardneal
    Contributor

    Thanks. I added that IF statement specifically to account for SQL 2016, I'll fix it. This query works correctly on the earlier version (pending the extra logic to handle escaping of {CatalogName}.)

    SELECT @@TRANCOUNT;
    
    DECLARE @Object_ID INT = OBJECT_ID('{escapedObjectName}')
    DECLARE @Column_Name_Query NVARCHAR(MAX) = NULL;
    DECLARE @Column_Names NVARCHAR(MAX) = NULL;
    IF EXISTS (SELECT TOP 1 * FROM sys.all_columns WHERE [object_id] = OBJECT_ID('sys.all_columns') AND [name] = 'graph_type')
    BEGIN
        SET @Column_Name_Query = N'SELECT @Column_Names = COALESCE(@Column_Names + '', '', '''') + QUOTENAME([name]) FROM {CatalogName}.[sys].[all_columns] WHERE [object_id] = @Object_ID AND COALESCE([graph_type], 0) NOT IN (1, 3, 4, 6, 7) ORDER BY [column_id] ASC;'
    END
    ELSE
    BEGIN
        SET @Column_Name_Query = N'SELECT @Column_Names = COALESCE(@Column_Names + '', '', '''') + QUOTENAME([name]) FROM {CatalogName}.[sys].[all_columns] WHERE [object_id] = @Object_ID ORDER BY [column_id] ASC;'
    END
    
    EXEC sp_executesql @stmt = @Column_Name_Query,
        @params = N'@Column_Names NVARCHAR(MAX) OUTPUT, @Object_ID INT',
        @Column_Names = @Column_Names OUTPUT, @Object_ID = @Object_ID
    
    SELECT @Column_Names
  5. moved this from To triage to In progress in SqlClient Boardon Oct 28, 2025
  6. nhart12 commented on Jan 7, 2026

    @nhart12
    Author

    @edwardneal any update on your PR here?

  7. edwardneal commented on Jan 7, 2026

    @edwardneal
    Contributor

    Nothing as yet unfortunately, but it's ready for code review.

    @dotnet/sqlclientdevteam is there enough time to merge #3719 prior to preview4?

  8. added
    Regression 💥
    on Mar 18, 2026
  9. added this to the 7.1.0-preview1 milestone on Mar 18, 2026
  10. andreascaesar commented on Mar 19, 2026

    @andreascaesar

    I haven't investigated the proposed fix, but it is the same error in SQL Server 2014, which (unfortunately) is in extended support for another year...

    Can someone confirm that this will be fixed in 2014 also?

  11. bifacil commented on Mar 24, 2026

    @bifacil

    This random error appears after upgrading from Microsoft.Data.SqlClient 6.1.4 to 7.0.

    My tables don't have any 'graph_type' column...

  12. 2 remaining items

  13. paulmedynski commented on Mar 26, 2026

    @paulmedynski
    Contributor

    The fix for this issue has moved from #3719 to #4092, which is based on the current main branch.

  14. moved this from In progress to Done in SqlClient Boardon Apr 7, 2026
  15. Welchen commented on Apr 20, 2026

    @Welchen

    Is there a manual workaround for this as 7.0.1 is not released yet? We just ran into this after upgrading to 7.0.0

  16. ErikEJ commented on Apr 20, 2026

    @ErikEJ
    Contributor

    @Welchen use 6.1.4 !?

  17. Welchen commented on Apr 20, 2026

    @Welchen

    @Welchen use 6.1.4 !?

    We upgraded to solve some binary collisions we were having since 7.0.0 started using .NET 10 dependencies.

  18. ErikEJ commented on Apr 20, 2026

    @ErikEJ
    Contributor

    @Welchen 7.0.1 is currently planned for this week, it might actually happen

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

Metadata

Metadata

Assignees

Labels

Regression 💥

Type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions