This is MS Sql Server: I have wrapped the system stored procedure, sp_sequence_get_range, so that I can have it return with a simple select (I was not able to figure out how to deal with the OUTPUT parameters in my C# framework).
This is the entire stored procedure:
CREATE PROCEDURE GetSequenceRange( @name VARCHAR, @counter INTEGER) AS
DECLARE @firstValue SQL_VARIANT = 0;
DECLARE @lastValue SQL_VARIANT = 0;
DECLARE @sql NVARCHAR(MAX);
SET @sql = N'EXEC sp_sequence_get_range @sequence_name = N''' + @name + N''', @range_size = ' + CONVERT(VARCHAR, @counter)
+ N', @range_first_value = @firstValue OUTPUT, @range_last_value = @lastValue OUTPUT;'
EXECUTE sp_executesql @sql, N'@name VARCHAR, @counter INTEGER, @firstValue SQL_VARIANT OUTPUT, @lastValue SQL_VARIANT OUTPUT', @name, @counter, @firstValue OUTPUT, @lastValue OUTPUT
SELECT @firstValue, @lastValue
When I try to execute the procedure like
exec GetSequenceRange 'tablename', 3
I get
Invalid object name 't'.
Whatever name I provide, the first letter is returned in this error message.
I have also tried
exec GetSequenceRange @name='tablename', @counter = 3
The 'tablename'
in all these examples is a qualified sequence name; I have tried both owner.sequence and database.owner.sequence.
I think there is something fundamentally wrong with my approach, but I don't see it.
Bonus for an answer that shows how to get the results from sp_sequence_get_range from a C# call without having this stored procedure wrapper in the first place.