2016-10-03 5 views
1

Sql Server 2008 R2を使用してC#でキュー管理システムを構築しています。顧客ケア、レディースセクション、登録セクションのように、同時に複数の部門にサービスを提供する。例えば。トークン{1-50} 自動インクリエートに最適な方法

  • カスタマーケア::トークン{} 51から350
  • 登録部:トークン{351から550}
  • ノーマルお客様

    • レディースセクションのトークン{551- 999}

    私はこのアプローチを使用しています。まず、どの部門からリクエストを受けているのかを見ています。テーブルのこの部門のトークン範囲をチェックしてから、この部門のトークンの既存の値を取得します。 [次の番号を更新中]テーブルで既存の値を上書きする。

    通常の顧客/登録/顧客/レディースセクションの2つの画面に同じトークン番号が表示されることがあるため、私が使用できる方法はありますか?

    おかげ

  • +3

    。 –

    答えて

    1

    あなたはこのように、出力文でアップデートを使用することができます。私はあなたがそのための自動インクリメント列を使用しないことを望む

    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 
    
    +0

    また、関数やプロシージャに 'update'部分をラップすることをお勧めします。 –

    +0

    @AndreyMorozovが同意しました。私の答えは – sventevit

    +0

    です@sventevitを編集しました。顧客ケア、登録、通常の顧客のために次のトークンを取得する方法を教えてください。 –

    関連する問題