2017-04-06 15 views
1

以下のストアドプロシージャでcaseステートメントを作成しようとしています。私は@OHDeptは、次の操作を実行したい:ケースIF THENステートメントを含める方法SQL

部門= 15場合は、そのような指定された非ブール型の式」として、私が試した16

すべては私にエラーのいくつかの種類を与える示して条件は、近く「他」が期待されるコンテストで。

私は私が何か間違ったことをやっている知っているが、私はよく分からない。誰も私を助けてくださいことはできますか? ありがとう!

(@JCCo bCompany, @BeginContract bContract ='', @EndContract bContract= 'zzzzzzzzz', @ThroughMth bDate,@BegMth bDate,@BegDept bDept='', @EndDept bDept='zzzzzzzzzz', @Status char(1)='A', @BegMthClosed bDate, @EndMthClosed bDate) 

With Recompile 
as 
set nocount on 

declare @OHDept varchar(2) 


declare @CT1Desc varchar(5), @CT2Desc varchar(5), @CT3Desc varchar(5), 
@CT4Desc varchar(5), @CT5Desc varchar(5), @CT6Desc varchar(5), 
@CT7Desc varchar(5), @CT8Desc varchar(5), @CT9Desc varchar(5) 

select @CT1Desc=JCCT.Abbreviation 
from HQCO 
join JCCT WITH (NOLOCK) on HQCO.PhaseGroup=JCCT.PhaseGroup 
Where [email protected] and JCCT.CostType=1 
select @CT2Desc=JCCT.Abbreviation 
from HQCO 
join JCCT WITH (NOLOCK) on HQCO.PhaseGroup=JCCT.PhaseGroup 
Where [email protected] and JCCT.CostType=2 
select @CT3Desc=JCCT.Abbreviation 
from HQCO 
join JCCT WITH (NOLOCK) on HQCO.PhaseGroup=JCCT.PhaseGroup 
Where [email protected] and JCCT.CostType=3 
select @CT4Desc=JCCT.Abbreviation 
from HQCO 
join JCCT WITH (NOLOCK) on HQCO.PhaseGroup=JCCT.PhaseGroup 
Where [email protected] and JCCT.CostType=4 
select @CT5Desc=JCCT.Abbreviation 
from HQCO 
join JCCT WITH (NOLOCK) on HQCO.PhaseGroup=JCCT.PhaseGroup 
Where [email protected] and JCCT.CostType=5 
select @CT6Desc=JCCT.Abbreviation 
from HQCO 
join JCCT WITH (NOLOCK) on HQCO.PhaseGroup=JCCT.PhaseGroup 
Where [email protected] and JCCT.CostType=6 
select @CT7Desc=JCCT.Abbreviation 
from HQCO 
join JCCT WITH (NOLOCK) on HQCO.PhaseGroup=JCCT.PhaseGroup 
Where [email protected] and JCCT.CostType=7 
select @CT8Desc=JCCT.Abbreviation 
from HQCO 
join JCCT WITH (NOLOCK) on HQCO.PhaseGroup=JCCT.PhaseGroup 
Where [email protected] and JCCT.CostType=99 
select @CT9Desc=JCCT.Abbreviation 
from HQCO 
join JCCT WITH (NOLOCK) on HQCO.PhaseGroup=JCCT.PhaseGroup 
Where [email protected] and JCCT.CostType=9 
set nocount off; 


With Contracts (JCCo, Contract) 
as (select JCCo, 
Contract 
From JCCM 
Where JCCo = @JCCo 
and ( 
    ------------------------ 
    /*Open*/ 
    case @Status 
    when 'O' then ContractStatus 
    end = 1 
    or /*Open*/ 
    case @Status 
    when 'O' then ContractStatus 
    end = 2 
    and MonthClosed > @ThroughMth 
    or /*Open*/ 
    case @Status 
    when 'O' then ContractStatus 
    end = 3 
    and MonthClosed > @ThroughMth 
    ------------------------ 
    or /*Soft-Closed/Open*/ 
    case @Status 
    when 'S' then ContractStatus 
    end = 2 
    and MonthClosed <= @ThroughMth 
    or /*Soft-Closed/Open*/ 
    case @Status 
    when 'S' then ContractStatus 
    end = 1 
    or /*Soft-Closed/Open*/ 
    case @Status 
    when 'S' then ContractStatus 
    end = 2 
    and MonthClosed > @ThroughMth 
    or /*Soft-Closed/Open*/ 
    case @Status 
    when 'S' then ContractStatus 
    end = 3 
    and MonthClosed > @ThroughMth 
    ------------------------ 
    or /*Hard-Closed*/ 
    case @Status 
    when 'C' then ContractStatus 
    end = 3 
    and MonthClosed between @BegMthClosed and @EndMthClosed 
    ------------------------ 
    or 
    case @Status 
    when 'A' then ContractStatus 
    end <> 0) 
) --End CTE 


Select 'CT1Desc'[email protected], 'CT2Desc'[email protected], 'CT3Desc'[email protected],'CT4Desc'[email protected], 
'CT5Desc'[email protected],'CT6Desc'= @CT6Desc, 'CT7Desc'[email protected], 'CT8Desc'[email protected],'CT9Desc'[email protected], 

ProjectMngrID=JCJMPM.ProjectMgr, --3/11/16 CJO 
PrjDept=JCCM.Department, ---3/11/16 CJO 
PrjVP=JCCM.udProjectManager, --3/25/16 CJO 

JCCM.JCCo, JCCM.Contract, ContDesc=JCCM.Description, JCCM.ContractStatus, 
JCCI.Department, DeptDesc = JCDM.Description, 
JCIP.BilledAmt, 
JCIP.ReceivedAmt,ActualCost,ACost1,ACost2,ACost3,ACost4,ACost5,ACost6,ACost7,ACost8,ACost9, 
ProjCloseDate=JCCM.ProjCloseDate, 
StartMonth=JCCM.StartMonth,---7/5/02 AA 
MonthClosed=JCCM.MonthClosed,---7/5/02 AA 
ContractDays=JCCM.CurrentDays, 
JobItem=JCJMPM.Job, 
CoName=HQCO.Name, 
[email protected], 
[email protected], 
[email protected], 
[email protected]/*, JCCM.Notes*/ 

FROM JCCI WITH (NOLOCK) 
JOIN JCCM WITH (NOLOCK) on JCCM.JCCo=JCCI.JCCo AND JCCM.Contract=JCCI.Contract 
JOIN Contracts on JCCM.JCCo=Contracts.JCCo AND JCCM.Contract = Contracts.Contract /**CTE with Contracts filtered by Status**/ 
JOIN JCDM WITH (NoLock) on JCDM.JCCo=JCCI.JCCo AND JCDM.Department = JCCI.Department 
JOIN HQCO WITH (NOLOCK) on HQCO.HQCo=JCCI.JCCo 

--- Project Management ID Info 
LEFT JOIN JCJMPM WITH (NOLOCK) ON JCCI.BillGroup =JCJMPM.Job AND JCCI.JCCo = JCJMPM.JCCo --Pull project ID info into report CJO. 

--- Revenue  
left join (select JCCo, Contract, Item, BilledAmt=sum(JCIP.BilledAmt),ReceivedAmt=sum(JCIP.ReceivedAmt) 
from JCIP 
where JCIP.Mth>[email protected] and JCIP.Mth<[email protected] 
and (JCIP.ContractAmt<>0 or JCIP.BilledAmt<>0 or JCIP.ReceivedAmt<>0) 
group by JCCo, Contract, Item) 
as JCIP on JCIP.JCCo=JCCI.JCCo and JCIP.Contract=JCCI.Contract and JCIP.Item=JCCI.Item 

-- Cost 
left join (select JCJP.JCCo, JCJP.Contract, JCJP.Item, 
ACost1=sum(case when CostType=1 and JCCP.Mth>[email protected] then JCCP.ActualCost else 0 end), 
ACost2=sum(case when CostType=2 and JCCP.Mth>[email protected] then JCCP.ActualCost else 0 end), 
ACost3=sum(case when CostType=3 and JCCP.Mth>[email protected] then JCCP.ActualCost else 0 end), 
ACost4=sum(case when CostType=4 and JCCP.Mth>[email protected] then JCCP.ActualCost else 0 end), 
ACost5=sum(case when CostType=5 and JCCP.Mth>[email protected] then JCCP.ActualCost else 0 end), 
ACost6=sum(case when CostType=6 and JCCP.Mth>[email protected] then JCCP.ActualCost else 0 end), 
ACost7=sum(case when CostType=7 and JCCP.Mth>[email protected] then JCCP.ActualCost else 0 end), 
ACost8=sum(case when CostType=99 and JCCP.Mth>[email protected] then JCCP.ActualCost else 0 end), 
ACost9=sum(case when (CostType>7 and CostType <99) and JCCP.Mth>[email protected] then JCCP.ActualCost else 0 end), 
ActualCost=sum(case when JCCP.Mth>[email protected] then ActualCost else 0 end) - sum(case when CostType=99 and JCCP.Mth>[email protected] then JCCP.ActualCost else 0 end) 
from JCCP 
join JCJP WITH (NOLOCK) on JCJP.JCCo=JCCP.JCCo and JCJP.Job=JCCP.Job and JCJP.PhaseGroup=JCCP.PhaseGroup 
and JCJP.Phase=JCCP.Phase 
where JCCP.Mth>[email protected] and JCCP.Mth<[email protected] 
and [email protected] and JCJP.Contract>[email protected] and JCJP.Contract<[email protected] 
group by JCJP.JCCo, JCJP.Contract, JCJP.Item) 
as JCCP on JCCP.JCCo=JCCI.JCCo and JCCP.Contract=JCCI.Contract and JCCP.Item=JCCI.Item 

where [email protected] and JCCI.Contract>[email protected] and JCCI.Contract<[email protected] 
and [email protected] 

case where @OHDept="15" then "16 else 0 

order by JCCI.JCCo, JCCI.Department, JCCI.Contract 
+1

私は、これは、コメントの日付に基づいていくつかの古代の手続きであると仮定していますが、それでも、この検討する必要があります[悪い習慣:どこにでも置くNOLOCK - アーロン・ベルトラン](https://blogs.sentryone.com/ aaronbertrand/bad-habits-nolock-everywhere /) – SqlZim

+0

あなたは何をしようとしていますか? '@ OHDept'を使って' JCCI.Department'をフィルタリングしますか?または 'JCCI.Department'に基づいて' @ OHDept'を設定しようとしていますか? – fqhv

+0

ええ、これは私が今作業しようとしているかなり古い手順です - NOLOCKのチップのおかげで、私はそれを感謝します! – omchantichanti

答えて

1

投稿コードのどこには表示されません@OHDeptには任意の値が割り当てられます。

それは値が割り当てられている場合はいつでも、あなたはこのようなもので、そのステートメントに従うことができます:

set @OHDept = case when @OHDept = '15' then '16' else @OHDept end; 
+0

とても助かりました!ありがとうございました!!!!!! – omchantichanti

+0

@omchantichanti幸いです! – SqlZim

0

あなたはAにifをどうしたいのと同様の方法でcaseを使用しようとしているように見えます手続き型言語。 SQL caseでは、の値がではなく、フロー内のブランチに評価される式です。

そのまま1516に残し、他の値を変換式は次のとおりです。

case where @OHDept = '15' then '16' else @OhDept end 

あなたはそれを示す言及したので、その式がselect句に属するようですね。

また、ブール論理があなたを引き上げていると思います。このようにして、クエリの先頭に多くのものを簡単に集約することができます。 andの優先順位がorより高いため、厳密にかっこは必要ありません。

(
    @Status in ('O', 'S') and ContractStatus in (1, 2, 3)) 
    and MonthClosed > @ThroughMth 
) 
or 
(
    @Status = 'C' and ContractStatus = 3 
    and MonthClosed between @BegMthClosed and @EndMthClosed 
) 
or 
(
    @Status = 'A' and ContractStatus <> 0 
) 
+0

ありがとう、これはとても役に立ちます!これは私が更新しようとしている古いコードなので、私が追加していたものに焦点を当てていましたが、あなたは真実です。私は完全に私の顔に落ちています。私はそれを感謝します! – omchantichanti

関連する問題