2013-01-15 76 views
2

我正在通過ODBC爲OpenEdge數據庫創建if語句。查詢我有像魅力,但進展要我定義的if語句不同,我不知道。有沒有人可以爲我定義正確的查詢?IF THEN SQL中的語句與OpenEdge

IF    
    {Crediteur code|type=string} = 'HOEFASTE' 
BEGIN  
    (SELECT 
     inok."vest-kd-arvo"+RTRIM('-') + RIGHT(CAST((inok."inok-nr" + 1000000) AS varchar(7)),6) AS inokvalue 
     ,RTRIM("leso-kd")+RTRIM('-')+inok."vest-kd-arvo"+RTRIM('-')+CAST("inok-nr"AS varchar(6)) AS inokdisplay 
     ,RIGHT(CAST((inok."inok-nr") AS varchar(7)),6) + '-' + inok."vest-kd-arvo" + '-' + RTRIM("leso-kd") AS inoknr 
     ,RTRIM("leso-kd") AS soortfact 
    FROM 
     PUB.inok 
    WHERE 
     inok."vcdc-nr" = {Administratie|type=int32} 
     AND inok."inss-kd" NOT IN ('H','G') 
    ) 
ELSE   
    (SELECT 
     inok."vest-kd-arvo"+RTRIM('-') + RIGHT(CAST((inok."inok-nr" + 1000000) AS varchar(7)),6) AS inokvalue 
     ,RTRIM("leso-kd")+RTRIM('-')+inok."vest-kd-arvo"+RTRIM('-')+CAST("inok-nr"AS varchar(6)) AS inokdisplay 
     ,RIGHT(CAST((inok."inok-nr") AS varchar(7)),6) + '-' + inok."vest-kd-arvo" + '-' + RTRIM("leso-kd") AS inoknr 
     ,RTRIM("leso-kd") AS soortfact 
    FROM 
     PUB.inok 
    WHERE 
     inok."rela-kd-kre" = {Crediteur code|type=string} 
     AND inok."vcdc-nr" = {Administratie|type=int32} 
     AND inok."inss-kd" NOT IN ('H','G') 
    ) 
END 

回答

1

如果我理解正確,只有你的WHERE條款是不同的,基於「Crediteur代碼」。

您可以移動WHERE條款中的IF。這會給你留下一個單一的SELECT -statement。這種替代方法適合你嗎?

SELECT 
    inok."vest-kd-arvo"+RTRIM('-') + RIGHT(CAST((inok."inok-nr" + 1000000) AS varchar(7)),6) AS inokvalue 
    ,RTRIM("leso-kd")+RTRIM('-')+inok."vest-kd-arvo"+RTRIM('-')+CAST("inok-nr"AS varchar(6)) AS inokdisplay 
    ,RIGHT(CAST((inok."inok-nr") AS varchar(7)),6) + '-' + inok."vest-kd-arvo" + '-' + RTRIM("leso-kd") AS inoknr 
    ,RTRIM("leso-kd") AS soortfact 
FROM 
    PUB.inok 
WHERE 
    inok."rela-kd-kre" = (CASE WHEN {Crediteur code|type=string} = 'HOEFASTE' THEN inok."rela-kd-kre" ELSE {Crediteur code|type=string} END) 
    AND inok."vcdc-nr" = {Administratie|type=int32} 
    AND inok."inss-kd" NOT IN ('H','G') 
+1

據我所知,這是我需要的解決方案!我現在要測試。 – Bouwplaats