Skip to content

Instantly share code, notes, and snippets.

@jglozano
Created March 2, 2018 16:07
Show Gist options
  • Save jglozano/ddc39b238633a1c6b26c34281024bae8 to your computer and use it in GitHub Desktop.
Save jglozano/ddc39b238633a1c6b26c34281024bae8 to your computer and use it in GitHub Desktop.
Get Next ID from Sequence / StoredProc
CREATE PROCEDURE [Schema].[sp_GetNextId]
AS
BEGIN
DECLARE @nextID INT;
SET @nextID = NEXT VALUE FOR Schema.SequenceName
RETURN @nextID;
END
CREATE SEQUENCE Schema.SequenceName AS INT INCREMENT BY 1 ;
public async Task<int> GetNextIdAsync()
{
using (var connection = GetDbConnection())
{
connection.Open();
var parameters = new DynamicParameters();
parameters.Add("@nextId", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);
await connection.ExecuteAsync("[Schema].[sp_GetNextId]", parameters, commandType: CommandType.StoredProcedure).ConfigureAwait(false);
var result = parameters.Get<int>("@nextId");
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment