Extract SqlBulkCopy column names using dynamic SQL#4092
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates SqlBulkCopy’s target-column discovery query to use dynamic SQL so that referencing sys.all_columns.graph_type doesn’t cause compilation failures on SQL Server 2016, addressing regression #3714.
Changes:
- Reworks
SqlBulkCopy.CreateInitialQuery()to build and execute thesys.all_columnscolumn-name query viasp_executesql. - Adjusts manual test SQL statistics expectations to account for the additional statements executed.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/CopyAllFromReader.cs | Updates expected SelectCount / SelectRows due to extra statements from dynamic SQL. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs | Uses dynamic SQL for column-name extraction to avoid SQL 2016 compilation errors when graph_type is absent. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4092 +/- ##
==========================================
- Coverage 73.22% 66.51% -6.72%
==========================================
Files 280 274 -6
Lines 43000 65785 +22785
==========================================
+ Hits 31486 43754 +12268
- Misses 11514 22031 +10517
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
5ef395f to
1be29d6
Compare
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
priyankatiwari08
left a comment
There was a problem hiding this comment.
Looks good to me, Just added a suggestion to add comment related to explaining why CatalogName is interpolated directly into the dynamic SQL string rather than parameterized
70269ce
|
Addressed both open review comments in 70269ce:
|
This enables the query to compile correctly when the graph_type column is missing
Description
#3590 allowed SqlBulkCopy to find and work with hidden columns (even if those columns weren't accessible server-side.) It contained a check on the
all_columnsDMV to make sure that we only checked thegraph_typecolumn when querying if that column existed; this was to maintain SQL 2016 compatibility.#3714 highlighted that both queries are compiled and fail at the point of compilation anyway, so this wasn't accomplishing anything. To make this work, we need to use dynamic SQL to run the column queries. This PR does so.
Supersedes #3719. Original PR by @edwardneal. This PR was created as a direct branch of the SqlClient repo so I can run the new SQL 16 and 17 CI pipeline stages manually to prove this fixes the issue.
Issues
Fixes #3714.
Testing
Manual testing of all test cases against a SQL 2016 instance (via the updated CI pipelines).