Fix Login.EnumDatabaseMappings query to avoid double evaluation of suser_sname #209
+10
−15
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes issue #37 where
Login.EnumDatabaseMappings()returnsnulleven when database users exist for a login due to an inefficient SQL query that evaluatessuser_sname()twice.Problem
The
Login.EnumDatabaseMappings()method was returningnulleven when database mappings existed. The root cause was in the SQL query defined inDatabaseMappings.xml, which calledsuser_sname(u.sid)twice in the same statement:This double evaluation caused:
Solution
Modified the SQL query in
DatabaseMappings.xmlto use CROSS APPLY, ensuringsuser_sname()is evaluated only once per row:Benefits
Login_apis_work_correctly()testImpact
Low risk, surgical change to the SQL query definition. The fix applies to both SQL Server 2005+ and SQL Server 7/8 compatibility versions of the query.
Files Modified
src/Microsoft/SqlServer/Management/SqlEnum/xml/DatabaseMappings.xmlFixes #37
Original prompt
Fixes #37
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.