2015-05-08 40 views
-1

如果這裏的條件不起作用,我的嵌套。我不知道爲什麼。我是Postgresql中的新成員。你能幫我解決這個問題嗎?我不知道該怎麼辦。 :。'(Postgresql中的嵌套IF完全不起作用?

CREATE OR REPLACE FUNCTION pl_uploadhousehoold(mchholdnumber character varying, mcbrgycode character varying, mcstio character varying, mcstreet character varying, mnhousenumber character varying, mclat character varying, mclong character varying, mcrespondentname character varying, mddateinterview date, mdstattime character varying, mdendtime character varying, mcpurok character varying, mnuserid integer, mczone character varying, mccategory character varying, mfile_path_household character varying, mfile_path character varying, mfile_respondent character varying, mdyear date, mnuser integer, mdaccomplished character varying, mdregistered character varying, mdvalidated character varying, mcsubcategory character varying, mcfacilityname character varying, mcposition character varying) 
RETURNS SETOF tbl_household AS 
$BODY$ 
DECLARE 

t_10 time without time zone; 
t_11 time without time zone; 
t_21 timestamp without time zone; 
t_22 timestamp without time zone; 
t_23 timestamp without time zone; 
hh_id character varying; 

BEGIN 
    IF hh_id is NULL THEN   
     IF mdstattime !='' THEN 
     t_10:=(select CAST (mdstattime as time without time zone) as dstattime); 
INSERT INTO tbl_household(chholdnumber,cbrgycode,csitio,cstreet,nhousenumber,clat,clong,crespondentname,ddateinterview,dstattime,cpurok,nuserid,czone,ccategory,file_path_household, file_path, file_respondent,dyear,nuser, csubcategory, cfacilityname, cposition) 
         values($1,$2,$3,$4,$5,$6,$7,$8,$9,t_10,$12,$13,$14,$15,$16,$17,$18,$19,$20,$24,$25,$26); 
     END IF; 

     IF mdendtime !='' THEN   
      t_11:=(select CAST (mdendtime as time without time zone) as dendtime); 
        INSERT INTO tbl_household(chholdnumber,cbrgycode,csitio,cstreet,nhousenumber,clat,clong,crespondentname,ddateinterview,dendtime,cpurok,nuserid,czone,ccategory,file_path_household, file_path, file_respondent,dyear,nuser, csubcategory, cfacilityname, cposition) 
         values($1,$2,$3,$4,$5,$6,$7,$8,$9,t_11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$24,$25,$26); 
     END IF; 
     IF mdaccomplished !='' THEN   
      t_21:=(select CAST (mdaccomplished as timestamp without time zone) as daccomplished); 


INSERT INTO tbl_household(chholdnumber,cbrgycode,csitio,cstreet,nhousenumber,clat,clong,crespondentname,ddateinterview,cpurok,nuserid,czone,ccategory,file_path_household, file_path, file_respondent,dyear,nuser,daccomplished, csubcategory, cfacilityname, cposition) 
         values($1,$2,$3,$4,$5,$6,$7,$8,$9,$12,$13,$14,$15,$16,$17,$18,$19,$20,t_21,$24,$25,$26); 
     END IF; 
     IF mdregistered !='' THEN   
      t_22:=(select CAST (mdregistered as timestamp without time zone) as dregistered); 
        INSERT INTO tbl_household(chholdnumber,cbrgycode,csitio,cstreet,nhousenumber,clat,clong,crespondentname,ddateinterview,cpurok,nuserid,czone,ccategory,file_path_household, file_path, file_respondent,dyear,nuser,dregistered, csubcategory, cfacilityname, cposition) 
         values($1,$2,$3,$4,$5,$6,$7,$8,$9,$12,$13,$14,$15,$16,$17,$18,$19,$20,t_22,$24,$25,$26); 
     END IF; 
     IF mdvalidated !='' THEN   
       t_23:=(select CAST (mdvalidated as timestamp without time zone) as dvalidated); 
        INSERT INTO tbl_household(chholdnumber,cbrgycode,csitio,cstreet,nhousenumber,clat,clong,crespondentname,ddateinterview,cpurok,nuserid,czone,ccategory,file_path_household, file_path, file_respondent,dyear,nuser,dvalidated, csubcategory, cfacilityname, cposition) 
         values($1,$2,$3,$4,$5,$6,$7,$8,$9,$12,$13,$14,$15,$16,$17,$18,$19,$20,t_23,$24,$25,$26); 
     END IF; 
     IF mdaccomplished ='' and mdstattime='' and mdendtime='' and mdregistered ='' and mdvalidated ='' THEN   
        INSERT INTO tbl_household(chholdnumber,cbrgycode,csitio,cstreet,nhousenumber,clat,clong,crespondentname,ddateinterview,cpurok,nuserid,czone,ccategory,file_path_household, file_path, file_respondent,dyear,nuser, csubcategory, cfacilityname, cposition) 
         values($1,$2,$3,$4,$5,$6,$7,$8,$9,$12,$13,$14,$15,$16,$17,$18,$19,$20,$24,$25,$26); 
     END IF; 

     IF mdaccomplished !='' and mdstattime!='' and mdendtime!='' and mdregistered !='' and mdvalidated !='' THEN 
       t_22:=(select CAST (mdregistered as timestamp without time zone) as dregistered); 
       t_21:=(select CAST (mdaccomplished as timestamp without time zone) as daccomplished); 
       t_10:=(select CAST (mdstattime as time without time zone) as dstattime);  
       t_11:=(select CAST (mdendtime as time without time zone) as dendtime); 
       t_23:=(select CAST (mdvalidated as timestamp without time zone) as dvalidated); 
        INSERT INTO tbl_household(chholdnumber,cbrgycode,csitio,cstreet,nhousenumber,clat,clong,crespondentname,ddateinterview,dstattime,dendtime,cpurok,nuserid,czone,ccategory,file_path_household, file_path, file_respondent,dyear,nuser,daccomplished,dregistered,dvalidated, csubcategory, cfacilityname, cposition) 
values($1,$2,$3,$4,$5,$6,$7,$8,$9,t_10,t_11,$12,$13,$14,$15,$16,$17,$18,$19,$20,t_21,t_22,t_23,$24,$25,$26); 
     END IF; 

     ELSE      
    END IF; 

我測試過我mdendtime空值,但它的條件不工作

+0

什麼是錯誤? –

+1

「*不起作用*」不是有效的Postgres錯誤消息。你測試的任何變量('mdendtime','mdstattime')是否都是'null'?爲什麼將名爲「time」的變量與字符串文字進行比較?這些變量的數據類型是什麼? –

+0

我已經添加了功能部分。它不顯示錯誤,但條件部分根本不起作用:) –

回答

0

您應該刪除你的函數底部的最後ELSE聲明

此外,在功能使用的是一個字符串轉換爲timetimestamp值一個非常複雜的方式,但沒有說是必要的而不是做:

IF mdendtime != '' THEN   
    t_11 := (select CAST (mdendtime as time without time zone) as dendtime); 
    INSERT INTO tbl_household(chholdnumber, dendtime, ...) 
    VALUES ($1, t_11, ...); 
END IF; 

,你可以簡單地做:

INSERT INTO tbl_household(chholdnumber, dendtime) 
    VALUES ($1, NULLIF(mdendtime, '')::time); 

但像弗蘭克Heikens說,你的功能太複雜。您最終可能會執行6次基本相同的數據插入。顯然你在菲律賓進行住戶調查,你想存儲住戶調查數據的位置信息。住戶和調查是兩個獨立的實體,並且在適當的數據庫設計中,所有實體都有自己的表。

因此,從將所有家庭數據放在表households開始。當你有一個新的調查,檢查家庭數據是否已經在表中。如果是,請檢索hholdid,否則將數據插入householdsRETURNING hholdid

現在製作一張表survey,它使用households表中的hholdid作爲外鍵。在survey表中,您可以爲所有相關信息(包括hholdid,如調查日期,開始時間,結束時間等)添加列。然後,您可以簡單地將調查信息插入到一行中,而不需要一個函數。

根據所有調查數據的含義,您可能想要重複將所有數據分解爲實體的過程。答辯人的信息看起來是一個很好的候選人:一個答覆者可能會進行多項調查,因此請使用respid主鍵創建一個答辯人表,並將該值存儲在您的survey表中。

對你的「話語宇宙」的分析是良好數據庫建模的核心。

+0

Sir @Patrick?你怎麼知道我在菲律賓做家庭調查? –

+0

菲律賓是我知道的世界上唯一一個使用術語barangay(簡稱brgy)爲鄰域的國家。 – Patrick

+0

呀! :D哈哈!你很聰明:) –