Skip to content

Instantly share code, notes, and snippets.

@bgrainger
Created September 15, 2017 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgrainger/718222179a49f2f133bea82537bd5d6b to your computer and use it in GitHub Desktop.
Save bgrainger/718222179a49f2f133bea82537bd5d6b to your computer and use it in GitHub Desktop.
MySQL UPDATE with OUT parameter
// 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