我正在使用Sql Server 2008 R2在C#中构建队列管理系统。一次在许多部门提供服务,如客户服务,女士部门,注册部门。例如。对于
我正在使用这种方法,首先,我正在寻找我收到请求的部门。在表中检查此部门的令牌范围,然后获取该部门的令牌的现有值。使用“更新下一个数字”表覆盖现有值。
我可以使用任何其他方法,因为我遇到的问题是,有时相同的令牌编号会出现在正常客户/注册/客户/女士部门的两个屏幕上。
由于
答案 0 :(得分:1)
您可以将update与output语句一起使用,如下所示:
use tempdb
go
if object_id('Tokens', 'u') is not null drop table Tokens
if object_id('GetNextToken', 'p') is not null drop procedure GetNextToken
go
create table Tokens (
Id int identity(1,1) not null,
Name varchar(50) not null,
TokenFrom int not null,
TokenTo int not null,
LastUsedToken int null,
constraint PK_Tokens primary key clustered (Id),
constraint UQ_Tokens_Name unique (Name)
)
go
insert into Tokens (Name, TokenFrom, TokenTo)
select 'Ladies Section', 1, 50 union
select 'Customer Care', 51, 350 union
select 'Registration Section', 351, 550 union
select 'Normal Customers', 551, 999
go
create procedure GetNextToken
@name varchar(50),
@token int output
as
begin
declare @tokens table (token int)
update Tokens
set LastUsedToken =
case
when LastUsedToken is null then TokenFrom
when LastUsedToken = TokenTo then TokenFrom
else LastUsedToken + 1
end
output inserted.LastUsedToken into @tokens
where Name = @name
set @token = (select top 1 token from @tokens)
end
go
-- To get 'Ladies Section'
declare @name varchar(50), @token int
set @name = 'Ladies Section'
exec GetNextToken @name, @token output
select @token
go
-- To get 'Customer Care'
declare @name varchar(50), @token int
set @name = 'Customer Care'
exec GetNextToken @name, @token output
select @token
go
-- To get 'Registration Section'
declare @name varchar(50), @token int
set @name = 'Registration Section'
exec GetNextToken @name, @token output
select @token
go
-- To get 'Normal Customers'
declare @name varchar(50), @token int
set @name = 'Normal Customers'
exec GetNextToken @name, @token output
select @token