Created
September 15, 2017 18:43
-
-
Save bgrainger/718222179a49f2f133bea82537bd5d6b to your computer and use it in GitHub Desktop.
MySQL UPDATE with OUT parameter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Make sure to set AllowUserVariables=true in connection string | |
private string SqlLogUpdate => _sqlLogUpdate ?? (_sqlLogUpdate = $@" | |
Update {_tableName} | |
Set DuplicateCount = DuplicateCount + @DuplicateCount, | |
GUID = (@newGUID := GUID) -- set the variable here | |
Where ErrorHash = @ErrorHash; -- AND ... | |
Select @newGUID; -- still have to select it to get it back"; | |
protected override bool LogError(Error error) | |
{ | |
using (var c = GetConnection()) | |
{ | |
if (Settings.RollupPeriod.HasValue && error.ErrorHash.HasValue) | |
{ | |
var queryParams = GetUpdateParams(error); | |
if (Guid.TryParse(c.QueryFirst<string>(SqlLogUpdate, queryParams), out var guid) | |
{ | |
// if we found an exception that's a duplicate, jump out | |
error.GUID = guid; | |
return true; | |
} | |
} | |
// ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment