CREATE OR REPLACE FUNCTION GetContPlanCode(tgrpContNo in VARCHAR2)
  return VARCHAR2 is
  tsql             VARCHAR2(1000);
  t_lenth int  := 0;CURSOR t_ContPlanCodeSet IS
    select ContPlanCode from lccontplan where grpcontno =tgrpContNo and plantype = '0';
begin
    FOR t_lccontplan IN t_ContPlanCodeSet LOOP
       t_flag        := 0;
       t_ContPlanCode :=  trim(t_lccontplan.ContPlanCode);  
         select case
                  when exists
                   (select 1
                          from lccontplanrisk a
                         where grpcontno = tgrpContNo
                           and contplancode = t_ContPlanCode
                           and riskcode not in
                               (select riskcode
                                  from lmriskedoritem
                                 WHERE edorcode = 'IR')) then
                   0
                  else
                   1
                end
                 into t_flag 
           from dual
        if t_flag = 1 then
           tsql := tsql || t_ContPlanCode;
        end if;     END LOOP;  return(tsql);end GetContPlanCode;
这段代码中一共报了三个错,
Compilation errors for FUNCTION LISTEST.GETCONTPLANCODEError: PL/SQL: ORA-00933: SQL 命令未正确结束
Line: 27
Text: if t_flag = 1 thenError: PL/SQL: SQL Statement ignored
Line: 11
Text: select caseError: PLS-00103: 出现符号 "IF"在需要下列之一时:
        loop
Line: 29
Text: end if;
求大神解答!