oracle 我总是得到错误与CTE,不是什么问题

shstlldc  于 2023-11-17  发布在  Oracle
关注(0)|答案(1)|浏览(151)

我无法编译下面的存储过程,我总是得到CTE附近的错误(与DAYSINMONTH)
我正在使用join DAYSINMONTH

PROCEDURE getCalendar 
(        
    V_Year                 VARCHAR2,
    V_Month                VARCHAR2,
    V_LoginId varchar2,
    curAttendanceDate OUT T_CURSOR,
     res OUT SYS_REFCURSOR,
     sud OUT SYS_REFCURSOR,
     cur2 OUT SYS_REFCURSOR,
     cur OUT SYS_REFCURSOR
     
)
AS
 V_date Date:= TO_DATE (V_Year+V_Month+'01', 'yyyy-mm-dd');
--V_Date Date := TO_DATE (V_Year || '-' || V_Month || '-01', 'yyyy-mm-dd');

BEGIN
*****--I am always getting errror on with CTE DaysinMonth kindly help me to resolve.------------*****

WITH DaysInMonth (dates)
AS 
(
SELECT V_Date AS dates FROM   DUAL
UNION ALL
 SELECT dates + INTERVAL '1' DAY
 FROM   DaysInMonth
WHERE  dates + INTERVAL '1' DAY < ADD_MONTHS(TRUNC(V_Date, 'MONTH'), 1)
) 

                               
INSERT INTO T_ATTENDANCE_ATTENDANCE (LoginId, AttendanceDate,ShiftName, ShiftStartTime,  ShiftEndTime,  Active,AttendanceStatus)
             SELECT u.LoginID,d.dates,'',NULL,NULL,1,'Approved'
              FROM DaysInMonth d CROSS JOIN T_ATTENDANCE_USER_MASTER u
             WHERE     EXTRACT (MONTH FROM dates) = EXTRACT (MONTH FROM V_Date) AND LoginId = V_LoginId
             AND NOT EXISTS
                          (SELECT LoginId    FROM T_ATTENDANCE_ATTENDANCE WHERE     LoginId = V_LoginId
                           AND EXTRACT (YEAR FROM ATTENDANCEDATE) =EXTRACT (YEAR FROM V_Date)
                           AND EXTRACT (MONTH FROM AttendanceDate) =   EXTRACT (MONTH FROM V_Date)  AND ACTIVE = 1);
                                                    
INSERT INTO T_ATTENDANCE_ATTENDANCE 
(LoginId,AttendanceDate,ShiftName,ShiftStartTime,ShiftEndTime, Active,AttendanceStatus)
   SELECT u.LoginID,d.dates,'',NULL,NULL,1,'Approved' 
   FROM DaysInMonth d CROSS JOIN T_ATTENDANCE_USER_MASTER u
   WHERE     EXTRACT (MONTH FROM dates) = EXTRACT (MONTH FROM V_Date)
                   AND LoginId = V_LoginId
                   AND NOT EXISTS
                          (SELECT LoginId  FROM T_ATTENDANCE_ATTENDANCE   WHERE     LoginId = V_LoginId
                              AND EXTRACT (YEAR FROM ATTENDANCEDATE) = EXTRACT (YEAR FROM V_date)
                              AND EXTRACT (MONTH FROM AttendanceDate) =  EXTRACT (MONTH FROM V_date) AND ACTIVE = 1);
                              
                              
V_Date := INTERVAL '1' MONTH + V_Date;

************--I am always getting errror on with CTE DaysinMonth kindly help me to resolve.--

WITH DaysInMonth (dates) AS 
(
  SELECT V_Date AS dates FROM   DUAL
  UNION ALL
  SELECT dates + INTERVAL '1' DAY
  FROM   DaysInMonth
  WHERE  dates + INTERVAL '1' DAY < ADD_MONTHS(TRUNC(V_Date, 'MONTH'), 1)
)
                           
 select dates from  DaysInMonth;                              

OPEN curAttendanceDate FOR
SELECT AttendanceDate,ShiftName,  TO_CHAR (AttendanceDate, 'ddd') cDAY,  V_Year,V_Month,   TO_CHAR (SignIn, 'yyyy-MM-dd hh:mm:ss tt') SignIn, TO_CHAR (SignOut, 'yyyy-MM-dd hh:mm:ss tt') SignOut
   FROM T_ATTENDANCE_ATTENDANCE   WHERE     LoginId = V_LoginId   
  AND EXTRACT (YEAR FROM ATTENDANCEDATE) = V_Year  
  AND EXTRACT (MONTH FROM AttendanceDate) = V_Month    AND ACTIVE = 1          
  ORDER BY AttendanceDate ASC;                
            
  update a set a.shiftname = 
  (select case when to_char(to_date(a.attendancedate) ,'DAY') in ('SATURDAY') and a.week in (2,4) then 'WEEKLYOFF'
               when to_char(to_date(a.attendancedate),'WW')   in ('SUNDAY') then 'WEEKLYOFF'
               else 'GENERAL1' 
          end
   from (select attendancedate,
                shiftname, 
                row_number() over (partition by to_char(to_date(attendancedate,'DAY')) order by attendancedate) as week 
         from t_attendance_attendance a1
           left join t_attendance_user_master u     on u.loginid = a1.loginid
           left join t_attendance_employee_master e on e.empid   = u.empid 
         where a1.loginid = v_loginid 
           and e.company is null
           and extract (year from attendancedate)  = extract (year from sysdate) 
           and extract (month from attendancedate) = extract (month from sysdate) 
           and nvl(shiftname,'x') = 'x'
        ) a
  );

END getCalendar;

字符串

fnvucqvd

fnvucqvd1#

子查询分解子句(也称为CTE)不会***创建可以在多个语句中重用的临时表。
您需要在使用子查询分解子句的每个语句中定义该子句。
在PL/SQL中不能单独使用SELECT语句。您需要将其选择到游标中或使用SELECT ... [BULK COLLECT] INTO ...
类似的东西(未经测试,因为我们没有你的表):

PROCEDURE getCalendar 
(        
    V_Year                 VARCHAR2,
    V_Month                VARCHAR2,
    V_LoginId varchar2,
    curAttendanceDate OUT T_CURSOR,
     res OUT SYS_REFCURSOR,
     sud OUT SYS_REFCURSOR,
     cur2 OUT SYS_REFCURSOR,
     cur OUT SYS_REFCURSOR
     
)
AS
  V_Date Date := TO_DATE (V_Year || '-' || V_Month || '-01', 'yyyy-mm-dd');
BEGIN
  INSERT INTO T_ATTENDANCE_ATTENDANCE (
    LoginId,
    AttendanceDate,
    ShiftName,
    ShiftStartTime,
    ShiftEndTime,
    Active,
    AttendanceStatus
  )
  WITH DaysInMonth (dates) AS (
    SELECT V_Date AS dates FROM   DUAL
  UNION ALL
    SELECT dates + INTERVAL '1' DAY
    FROM   DaysInMonth
    WHERE  dates + INTERVAL '1' DAY < ADD_MONTHS(TRUNC(V_Date, 'MONTH'), 1)
  ) 
  SELECT u.LoginID,
         d.dates,
         NULL,    -- '' is identical to NULL
         NULL,
         NULL,
         1,
         'Approved'
  FROM   DaysInMonth d
         CROSS JOIN T_ATTENDANCE_USER_MASTER u
  WHERE  LoginId = V_LoginId
  AND    NOT EXISTS (
           SELECT LoginId
           FROM   T_ATTENDANCE_ATTENDANCE
           WHERE  LoginId = V_LoginId
           AND    attendancedate >= d.dates
           AND    attendancedate <  d.dates + 1
           AND    ACTIVE = 1
        );
   
  -- Remove duplicate code that appears to be duplicating the previous INSERT.
                                               
  V_Date := ADD_MONTHS(V_Date, 1);
  -- Not sure why you do this as V_Date is never needed after this.

  -- You never use DaysInMonth after this and can just delete the (invalid)
  -- query from here.

  OPEN curAttendanceDate FOR
    SELECT AttendanceDate,
           ShiftName,
           TO_CHAR (AttendanceDate, 'ddd') cDAY,
           V_Year,
           V_Month,
           TO_CHAR(SignIn, 'yyyy-MM-dd hh:mm:ss tt') SignIn,
           TO_CHAR(SignOut, 'yyyy-MM-dd hh:mm:ss tt') SignOut
    FROM   T_ATTENDANCE_ATTENDANCE
    WHERE  LoginId = V_LoginId   
    AND    EXTRACT (YEAR FROM ATTENDANCEDATE) = V_Year  
    AND    EXTRACT (MONTH FROM AttendanceDate) = V_Month
    AND    ACTIVE = 1          
    ORDER BY AttendanceDate ASC;                
            
  update a
  set a.shiftname = 
  (select case when to_char(to_date(a.attendancedate) ,'DAY') in ('SATURDAY') and a.week in (2,4) then 'WEEKLYOFF'
               when to_char(to_date(a.attendancedate),'WW')   in ('SUNDAY') then 'WEEKLYOFF'
               else 'GENERAL1' 
          end
   from (select attendancedate,
                shiftname, 
                row_number() over (partition by to_char(to_date(attendancedate,'DAY')) order by attendancedate) as week 
         from t_attendance_attendance a1
           left join t_attendance_user_master u     on u.loginid = a1.loginid
           left join t_attendance_employee_master e on e.empid   = u.empid 
         where a1.loginid = v_loginid 
           and e.company is null
           and extract (year from attendancedate)  = extract (year from sysdate) 
           and extract (month from attendancedate) = extract (month from sysdate) 
           and nvl(shiftname,'x') = 'x'
        ) a
  );

END getCalendar;
/

字符串

相关问题