2017-02-20 6 views
0

。関数は機能しますが、私は値を取得する簡単な方法を探しています。以下は、複数の結果をテストするためにクエリアナライザで関数を構築するために使用したロジックです。私は、WHERE句で複数のケース/文を使用して考えたが、かなりそれが仕事を得ることができませんでした - 任意の助けをいただければ幸いです。スカラー値SQL関数は、私は複数のテーブル間で複数の条件に基づいて単一の値を返す関数を書いて参加し、条件

declare 
    @cat varchar(10) = 'A1' 
    ,@stat varchar(10) = '20' 
    ,@level int = 4 
    ,@sop varchar(10) = '' 
    ,@payid varchar(10) = '' 
    ,@paysubid varchar(10) = '' 

    declare @out varchar(10) = '' 

-- status match - sop, payid and subid override 
    select @out = d.[billedstat] 
    from [dbo].[BilledStatusOverride] a 
    inner join [dbo].[cat277] b on a.[cat277_statusId] = b.[COUNTER] 
    left outer join [dbo].[stat277] c on a.[stat277_statusId] = c.[COUNTER] 
    inner join [dbo].[bsname] d on a.[bsname_statId] = d.[counter] 
    where (isnull(a.sourceofpay,'') = @sop and isnull(a.[payid],'') = @payid and isnull(a.[paysubid],'') = @paysubid) 
     and isnull(b.[status],'') = @cat 
     and isnull(c.[status],'') = @stat 
     and a.[csflevel] = @level 

    if @out <> '' 
     begin 
      print 'status match - sop, payid and subid override. out value = '[email protected] 
      return 
     end 

    -- status match - sop only override 
    select @out = d.[billedstat] 
    from [dbo].[BilledStatusOverride] a 
    inner join [dbo].[cat277] b on a.[cat277_statusId] = b.[COUNTER] 
    left outer join [dbo].[stat277] c on a.[stat277_statusId] = c.[COUNTER] 
    inner join [dbo].[bsname] d on a.[bsname_statId] = d.[counter] 
    where (a.sourceofpay = @sop and isnull(a.[payid],'') = '' and isnull(a.[paysubid],'') = '') 
     and isnull(b.[status],'') = @cat 
     and isnull(c.[status],'') = @stat 
     and a.[csflevel] = @level 

    if @out <> '' 
     begin 
      print 'status match - sop override only. out value = '[email protected] 
      return 
     end 

    -- status match - payid and subid only override 
    select @out = d.[billedstat] 
    from [dbo].[BilledStatusOverride] a 
    inner join [dbo].[cat277] b on a.[cat277_statusId] = b.[COUNTER] 
    left outer join [dbo].[stat277] c on a.[stat277_statusId] = c.[COUNTER] 
    inner join [dbo].[bsname] d on a.[bsname_statId] = d.[counter] 
    where (isnull(a.sourceofpay,'') = '' and isnull(a.[payid],'') = @payid and isnull(a.[paysubid],'') = @paysubid) 
     and isnull(b.[status],'') = @cat 
     and isnull(c.[status],'') = @stat 
     and a.[csflevel] = @level 

    if @out <> '' 
     begin 
      print 'status match - payid and subid override only. out value = '[email protected] 
      return 
     end 

    -- status match - no overide (default) 
    select @out = d.[billedstat] 
    from [dbo].[BilledStatusOverride] a 
    inner join [dbo].[cat277] b on a.[cat277_statusId] = b.[COUNTER] 
    left outer join [dbo].[stat277] c on a.[stat277_statusId] = c.[COUNTER] 
    inner join [dbo].[bsname] d on a.[bsname_statId] = d.[counter] 
    where (isnull(a.sourceofpay,'') = '' and isnull(a.[payid],'') = '' and isnull(a.[paysubid],'') = '') 
     and isnull(b.[status],'') = @cat 
     and isnull(c.[status],'') = @stat 
     and a.[csflevel] = @level 

    if @out <> '' 
     begin 
      print 'status match - no overide (default). out value = '[email protected] 
      return 
     end 

    -- no status match - sop, payid and subid override 
    select @out = d.[billedstat] 
    from [dbo].[BilledStatusOverride] a 
    inner join [dbo].[cat277] b on a.[cat277_statusId] = b.[COUNTER] 
    left outer join [dbo].[stat277] c on a.[stat277_statusId] = c.[COUNTER] 
    inner join [dbo].[bsname] d on a.[bsname_statId] = d.[counter] 
    where (isnull(a.sourceofpay,'') = @sop and isnull(a.[payid],'') = @payid and isnull(a.[paysubid],'') = @paysubid) 
     and isnull(b.[status],'') = @cat 
     and a.stat277_statusId is null 
     and a.[csflevel] = @level 

    if @out <> '' 
     begin 
      print 'no status match - sop, payid and subid override. out value = '[email protected] 
      return 
     end 

    -- no status match - sop only override 
    select @out = d.[billedstat] 
    from [dbo].[BilledStatusOverride] a 
    inner join [dbo].[cat277] b on a.[cat277_statusId] = b.[COUNTER] 
    left outer join [dbo].[stat277] c on a.[stat277_statusId] = c.[COUNTER] 
    inner join [dbo].[bsname] d on a.[bsname_statId] = d.[counter] 
    where (a.sourceofpay = @sop and isnull(a.[payid],'') = '' and isnull(a.[paysubid],'') = '') 
     and isnull(b.[status],'') = @cat 
     and a.stat277_statusId is null 
     and a.[csflevel] = @level 

    if @out <> '' 
     begin 
      print 'no status match - sop override only. out value = '[email protected] 
      return 
     end 

    -- no status match - payid and subid only override 
    select @out = d.[billedstat] 
    from [dbo].[BilledStatusOverride] a 
    inner join [dbo].[cat277] b on a.[cat277_statusId] = b.[COUNTER] 
    left outer join [dbo].[stat277] c on a.[stat277_statusId] = c.[COUNTER] 
    inner join [dbo].[bsname] d on a.[bsname_statId] = d.[counter] 
    where (isnull(a.sourceofpay,'') = '' and isnull(a.[payid],'') = @payid and isnull(a.[paysubid],'') = @paysubid) 
     and isnull(b.[status],'') = @cat 
     and a.stat277_statusId is null 
     and a.[csflevel] = @level 

    if @out <> '' 
     begin 
      print 'no status match - payid and subid only override. out value = '[email protected] 
      return 
     end 

    -- no status match - no overide (default) 
    select @out = d.[billedstat] 
    from [dbo].[BilledStatusOverride] a 
    inner join [dbo].[cat277] b on a.[cat277_statusId] = b.[COUNTER] 
    left outer join [dbo].[stat277] c on a.[stat277_statusId] = c.[COUNTER] 
    inner join [dbo].[bsname] d on a.[bsname_statId] = d.[counter] 
    where (isnull(a.sourceofpay,'') = '' and isnull(a.[payid],'') = '' and isnull(a.[paysubid],'') = '') 
     and isnull(b.[status],'') = @cat 
     and a.stat277_statusId is null 
     and a.[csflevel] = @level 

    print 'default ''''' 
+0

あなたはMySQLを使用していますか?それはMS SQL Serverのコードによく似ています。 – jarlh

答えて

0

selectのcaseステートメントを使用して、where句ではなくすべての条件をカバーすることができます。

select @message = 
    case 
    when isnull(a.sourceofpay,'') = @sop 
     and isnull(a.[payid],'') = @payid 
     and isnull(a.[paysubid],'') = @paysubid 
     and isnull(b.[status],'') = @cat 
     and isnull(c.[status],'') = @stat 
     and a.[csflevel] = @level then 
     'status match - sop, payid and subid override. out value = ' 
    when ... 
    else 'default ''''' end, 
    @out = d.billedstat 
    from [dbo].[BilledStatusOverride] a 
    inner join [dbo].[cat277] b on a.[cat277_statusId] = b.[COUNTER] 
    left outer join [dbo].[stat277] c on a.[stat277_statusId] = c.[COUNTER] 
    inner join [dbo].[bsname] d on a.[bsname_statId] = d.[counter] 

明らかに...を他のテスト条件に置き換えてください。これはまだ素晴らしいことではありませんが、少なくとも1つのSQL文です。

関連する問題