0
我正在做一個postgresSql函數來返回臨時表中的所有行。我想在我的函數開始時從表中刪除所有行但它給我在TRUNCATE上或附近的語法錯誤。 這是我的查詢 -這個查詢有什麼問題。它給我在TRUNCATE上或附近的語法錯誤
CREATE OR REPLACE FUNCTION public.monthly_data_of_antenatal_care(hospitalId integer)
RETURNS SETOF antenatal_care_temp AS $$
SELECT DISTINCT * FROM antenatal_care_temp group by hospital_id,antenatal_temp_id;
BEGIN
TRUNCATE TABLE antenatal_care_temp RESTART IDENTITY;
FOR
select hospital_id from mas_hospital where parent_institute_id = $1 or hospital_id = $1
LOOP
RETURN QUERY EXECUTE
'insert into table antenatal_care_temp(total,bpl,sc,st,below_19,jsy_reg,early_reg,high_risk_an,prophylaxis_i,prophylaxis_c1,prophylaxis_c2,treatment_i,
treatment_c1,treatment_c2,an_checkups_1,an_checkups_2,an_checkups_3,an_checkups_4,an_checkups_5,tt_a_1,tt_i_1,tt_a_2,tt_i_2,data_type) values(
(select count(*) from ph_anc_survey where Extract(month from reg_date) = Extract(month from now()) and Extract(year from reg_date) = Extract(year from now()) and hospital_id = $1),
(select count(*) from ph_anc_survey where bpl_status = 'Y' and Extract(month from reg_date) = Extract(month from now()) and Extract(year from reg_date) = Extract(year from now()) and hospital_id = $1),
0,0,
(select count(*) from ph_anc_survey pas join ph_anc_followup paf on pas.anc_reg_id = paf.anc_reg_id where paf.age < 19 and Extract(year from paf.followup_date) = Extract(year from now()) and Extract(month from paf.followup_date) = Extract(month from now()) and pas.hospital_id = $1),
(select count(*) from ph_anc_survey where jsy_flag = 'Yes' and Extract(month from reg_date) = Extract(month from now()) and Extract(year from reg_date) = Extract(year from now()) and hospital_id = $1),
0,
(select count(*) from ph_anc_survey where complication != '' and Extract(month from reg_date) = Extract(month from now()) and Extract(year from reg_date) = Extract(year from now()) and hospital_id = $1),
0,0,0
(select count(*) from ph_anc_survey pas join ph_anc_followup paf on pas.anc_reg_id = paf.anc_reg_id where paf.age < 19 and Extract(year from paf.followup_date) = Extract(year from now()) and Extract(month from paf.followup_date) = Extract(month from now()) and pas.hospital_id = $1),
(select count(*) from ph_anc_survey pas join ph_anc_followup paf on pas.anc_reg_id = paf.anc_reg_id where paf.age < 19 and Extract(year from paf.followup_date) = Extract(year from now()) and Extract(month from paf.followup_date) = Extract(month from now()) and pas.hospital_id = $1),
(select count(*) from ph_anc_survey pas join ph_anc_followup paf on pas.anc_reg_id = paf.anc_reg_id where paf.age < 19 and Extract(year from paf.followup_date) = Extract(year from now()) and Extract(month from paf.followup_date) = Extract(month from now()) and pas.hospital_id = $1),
(select count(*) from ph_anc_survey pas join ph_anc_followup paf on pas.anc_reg_id = paf.anc_reg_id where paf.age < 19 and Extract(year from paf.followup_date) = Extract(year from now()) and Extract(month from paf.followup_date) = Extract(month from now()) and pas.hospital_id = $1),
(select count(*) from ph_anc_survey pas join ph_anc_followup paf on pas.anc_reg_id = paf.anc_reg_id where paf.age < 19 and Extract(year from paf.followup_date) = Extract(year from now()) and Extract(month from paf.followup_date) = Extract(month from now()) and pas.hospital_id = $1),
0,
(select count(*) from ph_anc_survey pas join ph_anc_followup paf on pas.anc_reg_id = paf.anc_reg_id where paf.age < 19 and Extract(year from paf.followup_date) = Extract(year from now()) and Extract(month from paf.followup_date) = Extract(month from now()) and pas.hospital_id = $1),
0,
(select count(*) from ph_anc_survey pas join ph_anc_followup paf on pas.anc_reg_id = paf.anc_reg_id where paf.age < 19 and Extract(year from paf.followup_date) = Extract(year from now()) and Extract(month from paf.followup_date) = Extract(month from now()) and pas.hospital_id = $1)
'current',
$1
);
insert into table antenatal_care_temp(total,bpl,sc,st,below_19,jsy_reg,early_reg,high_risk_an,prophylaxis_i,prophylaxis_c1,prophylaxis_c2,treatment_i,
treatment_c1,treatment_c2,an_checkups_1,an_checkups_2,an_checkups_3,an_checkups_4,an_checkups_5,tt_a_1,tt_i_1,tt_a_2,tt_i_2,data_type) values(
(select count(*) from ph_anc_survey where Extract(month from reg_date) between 4 and Extract(month from now()) and hospital_id = $1),
(select count(*) from ph_anc_survey where bpl_status = 'Y' and Extract(month from reg_date) between 4 and Extract(month from now()) and Extract(year from reg_date) = Extract(year from now()) and hospital_id = $1),
0,0,
(select count(*) from ph_anc_survey pas join ph_anc_followup paf on pas.anc_reg_id = paf.anc_reg_id where paf.age < 19 and Extract(year from paf.followup_date) = Extract(year from now()) and Extract(month from paf.followup_date) between 4 and Extract(month from now()) and pas.hospital_id = $1),
(select count(*) from ph_anc_survey where jsy_flag = 'Yes' and Extract(month from reg_date) between 4 and Extract(month from now()) and Extract(year from reg_date) = Extract(year from now()) and hospital_id = $1),
0,
(select count(*) from ph_anc_survey where complication != '' and Extract(month from reg_date) between 4 and Extract(month from now()) and Extract(year from reg_date) = Extract(year from now()) and hospital_id = $1),
0,0,0
(select count(*) from ph_anc_survey pas join ph_anc_followup paf on pas.anc_reg_id = paf.anc_reg_id where paf.age < 19 and Extract(year from paf.followup_date) = Extract(year from now()) and Extract(month from paf.followup_date) between 4 and Extract(month from now()) and pas.hospital_id = $1),
(select count(*) from ph_anc_survey pas join ph_anc_followup paf on pas.anc_reg_id = paf.anc_reg_id where paf.age < 19 and Extract(year from paf.followup_date) = Extract(year from now()) and Extract(month from paf.followup_date) between 4 and Extract(month from now()) and pas.hospital_id = $1),
(select count(*) from ph_anc_survey pas join ph_anc_followup paf on pas.anc_reg_id = paf.anc_reg_id where paf.age < 19 and Extract(year from paf.followup_date) = Extract(year from now()) and Extract(month from paf.followup_date) between 4 and Extract(month from now()) and pas.hospital_id = $1),
(select count(*) from ph_anc_survey pas join ph_anc_followup paf on pas.anc_reg_id = paf.anc_reg_id where paf.age < 19 and Extract(year from paf.followup_date) = Extract(year from now()) and Extract(month from paf.followup_date) between 4 and Extract(month from now()) and pas.hospital_id = $1),
(select count(*) from ph_anc_survey pas join ph_anc_followup paf on pas.anc_reg_id = paf.anc_reg_id where paf.age < 19 and Extract(year from paf.followup_date) = Extract(year from now()) and Extract(month from paf.followup_date) between 4 and Extract(month from now()) and pas.hospital_id = $1),
0,
(select count(*) from ph_anc_survey pas join ph_anc_followup paf on pas.anc_reg_id = paf.anc_reg_id where paf.age < 19 and Extract(year from paf.followup_date) = Extract(year from now()) and Extract(month from paf.followup_date) between 4 and Extract(month from now()) and pas.hospital_id = $1),
0,
(select count(*) from ph_anc_survey pas join ph_anc_followup paf on pas.anc_reg_id = paf.anc_reg_id where paf.age < 19 and Extract(year from paf.followup_date) = Extract(year from now()) and Extract(month from paf.followup_date) between 4 and Extract(month from now()) and pas.hospital_id = $1)
'cumulative',
$1
)';
END LOOP;
END
$$ LANGUAGE sql;
我想TRUNCATE後無表的關鍵字應該解決這個問題? –
'語言SQL'不支持'execute'(或'begin ... end')您需要'language plpgsql'。在第一個'begin'之前,你也不能有'select'。選擇還需要一個目標來存儲數據的存儲位置。我也不認爲你可以用一個'execute'運行兩條語句。這兩個'inserts'都沒有'returns'子句,所以它們不會返回任何數據。 –
整個函數看起來過於複雜,並且在循環中執行插入操作會降低性能。爲什麼不直接使用'insert ... select'?也許如果你向我們解釋了你真正**的問題,我們可以提出一個更好的解決方案。 –