将下面的sqlcode转换为oracle

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

我需要将下面的代码从SQL转换为Oracle PLSQL,请帮助我

Update A
         Set 
          A.ShiftName=@ShiftName,
          A.ShiftStartTime=DATEADD(day,DATEDIFF(day,0,AttendanceDate),
          CAST(S.ShiftStartTime as varchar(8))),
          A.ShiftEndTime=DATEADD(day,DATEDIFF(day,case when @ShiftName='NIGHT' then -1 else 0 end ,
          AttendanceDate),CAST(S.ShiftEndTime as varchar(8))),
          A.ModifiedBy=@UpdatedBy, 
          A.ModifiedOn=GETDATE()
          from Attendance A left join ShiftMaster S on S.[Shift]=@ShiftName
          where AttendanceId=@AttendanceId
          
    =========================================================================
    I need to convert below code from sql to oracle PLSQL  please help me 
=========================================================================
    UPDATE A
            SET A.ShiftName = dt.ShiftName, 
            A.ShiftStartTime=DATEADD(day,DATEDIFF(day,0,A.AttendanceDate),CAST(S.ShiftStartTime as varchar(8))),
            A.ShiftEndTime=DATEADD(day,DATEDIFF(day,case when dt.ShiftName='NIGHT' then -1 else 0 end ,A.AttendanceDate),CAST(S.ShiftEndTime as varchar(8))),
            A.ModifiedBy = @UpdatedBy,
            A.ModifiedOn = getdate()
            from Attendance A join @dtAttendance dt on A.LoginId = dt.LoginID and A.AttendanceDate = dt.AttendanceDate 
            left join ShiftMaster S on S.[Shift]=dt.ShiftName 
            where a.AttendanceDate between getdate()-1 and dateadd(day,+30,getdate())
            and A.LoginId in (Select LoginId from UserAttendanceTeam where TowerPk = @TowerPk and isActive = 'Y')

字符串

我需要将上述代码从sql转换为oracle PLSQL,请帮助我

kyxcudwk

kyxcudwk1#

好吧,那是一段无效的代码;你为什么期望它能工作?

  • CTE * 在空间中丢失 * -您需要对它做些什么(在我的示例中,我打开了一个ref光标)
  • 你必须终止每个语句
  • 修复v_date;不依赖于隐式数据类型转换(从字符串到date
  • 递归CTE必须具有列列表

另外,请注意,包body(您发布的)需要包规范(没有它,body不能存在)。
看起来更好的代码是

CREATE OR REPLACE PACKAGE BODY PKG_ATTENDANCE_DASHBOARD
AS
   PROCEDURE Proc_Dashboard (V_CommandType                VARCHAR2,
                             V_LoginId                    VARCHAR2,
                             V_SignType                   VARCHAR2,
                             V_AttendanceDate             DATE,
                             V_Year                       VARCHAR2,
                             V_Month                      VARCHAR2,
                             V_IPAddress                  VARCHAR2,
                             V_LogonUserName              VARCHAR2,
                             V_WorkLocation               VARCHAR2,
                             V_WorkLocationType           VARCHAR2,
                             V_Company                    VARCHAR2,
                             V_UserType                   VARCHAR2,
                             V_ShiftName                  VARCHAR2,
                             V_Self_Covid_Status          VARCHAR2,
                             V_Self_Covid_Remark          VARCHAR2,
                             V_Family_Covid_Status        VARCHAR2,
                             V_Family_Covid_Remark        VARCHAR2,
                             V_UpdatedBy                  VARCHAR2,
                             curGetCalendar           OUT SYS_REFCURSOR,
                             curGetCovidStatus        OUT SYS_REFCURSOR,
                             curListCompanyName       OUT SYS_REFCURSOR,
                             curListTowerName         OUT SYS_REFCURSOR,
                             curAttendanceListByDate  OUT SYS_REFCURSOR)
   AS
      v_Date  DATE := TO_DATE (V_Year || '-' || V_Month || '-01', 'yyyy-mm-dd');
   BEGIN
      IF V_CommandType = 'getCalendar'
      THEN
         OPEN curGetCalendar FOR
            WITH
               DaysInMonth (dates)
               AS
                  (SELECT SYSDATE AS dates FROM DUAL
                   UNION ALL
                   SELECT INTERVAL '1' DAY (5) + dates
                     FROM DaysInMonth
                    WHERE EXTRACT (MONTH FROM dates) = EXTRACT (MONTH FROM v_date))
            SELECT dates
              FROM DaysInMonth;
      END IF;
   END Proc_Dashboard;
END PKG_ATTENDANCE_DASHBOARD;

字符串
现在,它取决于你传递给它的内容(所有参数都是强制性的),所以我不打算为你做这件事。CTE的工作原理是:

SQL> WITH
  2     DaysInMonth (dates)
  3     AS
  4        (SELECT SYSDATE AS dates FROM DUAL
  5         UNION ALL
  6         SELECT INTERVAL '1' DAY (5) + dates
  7           FROM DaysInMonth
  8          WHERE EXTRACT (MONTH FROM dates) = EXTRACT (MONTH FROM DATE '2023-01-01'))
  9  SELECT dates
 10    FROM DaysInMonth;

DATES
----------
2023-10-26

SQL>

[编辑],基于您的评论:

代码

WITH DaysInMonth (dates) AS ( SELECT V_Date AS dates FROM DUAL UNION ...


不会工作,因为在DUAL表中没有V_DATE列。如果你使用SYSDATE函数,它会执行,但仍然返回错误的结果(缺少当前月份的第一天),所以你必须修复它。此外,如果你想省略日期的时间部分,使用trunc(sysdate)
另一方面,这里有一些简单的代码,你可能会感兴趣:

SQL> select trunc(sysdate, 'mm') + level - 1 as dates
  2  from dual
  3  connect by level <= last_day(sysdate) - trunc(sysdate, 'mm') + 1;

DATES
----------
01.11.2023
02.11.2023
03.11.2023
<snip>
29.11.2023
30.11.2023

30 rows selected.

SQL>

相关问题