Skip to content

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

@nhart12

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions