什么是自动起泡的最佳方法 [英] What is Best Approach for Auto Increament

查看:74
本文介绍了什么是自动起泡的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Sql Server 2008 R2在C#中构建队列管理系统.一次在许多部门提供服务,例如客户服务部,女士部,注册部.例如.对于

I am building a Queue Management System in C# using Sql Server 2008 R2. Providing Services in many departments at a time Like Customer Care, Ladies Section, Registration Section. For Example. For

  • 女士专区:令牌{1-50}
  • 客户服务:令牌{51-350}
  • 注册科:令牌{351-550}
  • 普通客户:令牌{551-999}

我正在使用这种方法,首先,我正在从哪个部门获得请求.在表中检查该部门的令牌范围,然后获取该部门的令牌的现有值.使用更新下一个号码"表覆盖现有值.

I am using this Approach, First of all i am looking from which department I am getting request. Check Token Range of this department in Table, then getting existing value of the Token for this department. Overriding existing value with Updating Next Number table.

我是否可以使用其他方法,因为我遇到的问题是,正常客户/注册/客户/女士部分的两个屏幕上有时会出现相同的令牌编号.

Is there any other approach i can use because i am facing problem that sometimes same Token Number is coming on Two Screens of Normal Customer's/Registration/Customer/Ladies Sections.

谢谢

推荐答案

您可以对输出语句使用update,如下所示:

You could use update with output statement, like this:

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

这篇关于什么是自动起泡的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆