将Oracle存储过程转换为SQL Server存储过程

时间:2017-05-16 11:24:31

标签: sql sql-server database oracle stored-procedures

亲爱的所有人都可以帮助我转换以下Oracle存储过程 到SQL Server版本

CREATE  PROCEDURE Schedule_Employees (
param_from          IN VARCHAR2,
param_to            IN VARCHAR2,
param_template_id   IN NUMBER)
AS
start_date      NUMBER;
end_date        NUMBER;
template_type   NUMBER;
rec_no          NUMBER;
days_no         NUMERIC;
day_date        VARCHAR2 (20);
shifts_value    VARCHAR2 (10);
current_shift   VARCHAR2 (10) := '-1';
current_index   NUMERIC := 0;
rec_column       VARCHAR (1);
BEGIN
start_date := TO_NUMBER (TO_CHAR (TO_DATE (param_from, 'dd/MM/yyyy'), 'j'));
end_date := TO_NUMBER (TO_CHAR (TO_DATE (param_to, 'dd/MM/yyyy'), 'j'));

FOR cur_r IN start_date .. end_date
LOOP
  day_date := TO_CHAR (TO_DATE (cur_r, 'j'), 'dd/MM/RRRR');

  FOR employees IN (SELECT EmpID FROM EmployeesTempList)
  LOOP
     SELECT TYPE
       INTO template_type
        FROM WORKSCHEDULES
       WHERE SCHEDULEID = param_template_id;

      SELECT RECNO
       INTO rec_no
       FROM WORKSCHEDULES
      WHERE SCHEDULEID = param_template_id;

     SELECT DAYSNO
       INTO days_no
       FROM WORKSCHEDULES
      WHERE SCHEDULEID = param_template_id;

     IF template_type = 2
     THEN
        SELECT RECURRCEVALUES
          INTO shifts_value
          FROM WORKSCHEDULESDYNAMICDETAILS
         WHERE WORKSCHEDULESID = param_template_id
               AND DAYID = MOD (current_index, days_no);

        SELECT ShiftID
          INTO current_shift
          FROM (WITH DATA AS (SELECT shifts_value str FROM DUAL)
                    SELECT ROWNUM AS ID,
                           TRIM (REGEXP_SUBSTR (str,
                                                '[^-]+',
                                                1,
                                                LEVEL))
                              ShiftID
                      FROM DATA
                CONNECT BY INSTR (str,
                                  '-',
                                  1,
                                  LEVEL - 1) > 0)
         WHERE ID = GetRecurrence (current_index, days_no, rec_no);
     ELSE
        rec_column := GetRecurrence (current_index, 7, 4);
        CASE
            WHEN rec_column = 1 THEN select RECUR1 into current_shift from WORKSCHEDULESDETAILS where WORKSCHEDULESID = param_template_id  and DAYID= ((select to_char(to_date(day_date,'dd/MM/yyyy'),'D') from dual) - 1);
            WHEN rec_column = 2 THEN select RECUR2 into current_shift from WORKSCHEDULESDETAILS where WORKSCHEDULESID = param_template_id  and DAYID= ((select to_char(to_date(day_date,'dd/MM/yyyy'),'D') from dual) - 1);
            WHEN rec_column = 3 THEN select RECUR3 into current_shift from WORKSCHEDULESDETAILS where WORKSCHEDULESID = param_template_id  and DAYID= ((select to_char(to_date(day_date,'dd/MM/yyyy'),'D') from dual) - 1);
            WHEN rec_column = 4 THEN select RECUR4 into current_shift from WORKSCHEDULESDETAILS where WORKSCHEDULESID = param_template_id  and DAYID= ((select to_char(to_date(day_date,'dd/MM/yyyy'),'D') from dual) - 1);
        END CASE;
     END IF;

     DELETE FROM ScheduleEmployees
           WHERE EMPLOYEEID = employees.EmpID
                 AND DAYDATE = TO_DATE (day_date, 'dd/MM/yyyy');

     INSERT INTO ScheduleEmployees (SCHEDULEID,
                                    EMPLOYEEID,
                                    FROMDATE,
                                    TODATE,
                                    DAYDATE,
                                    SHIFTS,
                                    CREATEDATE)
          VALUES (param_template_id,
                   employees.EmpID,
                  TO_DATE (param_from, 'dd/MM/yyyy'),
                  TO_DATE (param_to, 'dd/MM/yyyy'),
                  TO_DATE (day_date, 'dd/MM/yyyy'),
                  current_shift,
                  SYSDATE);
   END LOOP;

   current_index := current_index + 1;
END LOOP;
 DELETE FROM EmployeesTempList;
  COMMIT;
END;
/

我尝试过的    

       CREATE  PROCEDURE Schedule_Employees (
       @param_from          VARCHAR(4000),
       @param_to            VARCHAR(4000),
       @param_template_id   FLOAT)
       AS
       BEGIN
       DECLARE @start_date      FLOAT;
       DECLARE @end_date        FLOAT;
       DECLARE @template_type   FLOAT;
       DECLARE @rec_no          FLOAT;
       DECLARE @days_no         NUMERIC(38,0);
       DECLARE @day_date        VARCHAR (20);
       DECLARE @shifts_value    VARCHAR (10);
       DECLARE @current_shift   VARCHAR (10) = '-1';
       DECLARE @current_index   NUMERIC(38,0) = 0;
       DECLARE @rec_column       VARCHAR (1);
       SET NOCOUNT ON;
       SET @start_date = TO_NUMBER (TO_CHAR (CONVERT (DATETIME, @param_from, 
       'dd/MM/yyyy'), 'j'));
       SET @end_date = TO_NUMBER (TO_CHAR (CONVERT (DATETIME, @param_to, 
        'dd/MM/yyyy'), 'j'));

   DECLARE cur_r CURSOR FOR start_date .. end_date
   OPEN cur_r;
   FETCH cur_r INTO;
   WHILE @@FETCH_STATUS=0
   BEGIN
     SET @day_date = TO_CHAR (CONVERT (DATETIME, cur_r, 'j'), 'dd/MM/RRRR');

    DECLARE employees CURSOR FOR SELECT EmpID FROM EmployeesTempList;
    OPEN employees;
    FETCH employees INTO;
    WHILE @@FETCH_STATUS=0
    BEGIN
       SELECT @template_type = TYPE
       FROM WORKSCHEDULES
       WHERE SCHEDULEID = @param_template_id;

       SELECT @rec_no = RECNO
       FROM WORKSCHEDULES
       WHERE SCHEDULEID = @param_template_id;

       SELECT @days_no = DAYSNO
       FROM WORKSCHEDULES
       WHERE SCHEDULEID = @param_template_id;

       IF @template_type = 2
       BEGIN
        SELECT @shifts_value = RECURRCEVALUES
          FROM WORKSCHEDULESDYNAMICDETAILS
          WHERE WORKSCHEDULESID = @param_template_id
               AND DAYID = (@current_index % @days_no);

         SELECT @current_shift = ShiftID
          FROM (WITH DATA AS (SELECT @shifts_value str)
                    SELECT ROWNUM AS ID,
                           RTRIM(LTRIM (REGEXP_SUBSTR (str,
                                                '[^-]+',
                                                1,
                                                LEVEL))
                              ShiftID
                      FROM DATA
                CONNECT BY INSTR (str,
                                  '-',
                                  1,
                                  LEVEL - 1) > 0)
         WHERE ID = GetRecurrence (@current_index, @days_no, @rec_no);
     END
     ELSE BEGIN
        SET @rec_column = GetRecurrence (@current_index, 7, 4);
        CASE
            WHEN @rec_column = 1 THEN select @current_shift = RECUR1 from WORKSCHEDULESDETAILS where WORKSCHEDULESID = @param_template_id  and DAYID= ((select to_char(convert(DATETIME, @day_date,'dd/MM/yyyy'),'D')) - 1);
            WHEN @rec_column = 2 THEN select @current_shift = RECUR2 from WORKSCHEDULESDETAILS where WORKSCHEDULESID = @param_template_id  and DAYID= ((select to_char(convert(DATETIME, @day_date,'dd/MM/yyyy'),'D')) - 1);
            WHEN @rec_column = 3 THEN select @current_shift = RECUR3 from WORKSCHEDULESDETAILS where WORKSCHEDULESID = @param_template_id  and DAYID= ((select to_char(convert(DATETIME, @day_date,'dd/MM/yyyy'),'D')) - 1);
            WHEN @rec_column = 4 THEN select @current_shift = RECUR4 from WORKSCHEDULESDETAILS where WORKSCHEDULESID = @param_template_id  and DAYID= ((select to_char(convert(DATETIME, @day_date,'dd/MM/yyyy'),'D')) - 1);
        END CASE;
     END 

     DELETE FROM ScheduleEmployees
           WHERE EMPLOYEEID = employees.EmpID
                 AND DAYDATE = CONVERT (DATETIME, @day_date, 'dd/MM/yyyy');

     INSERT INTO ScheduleEmployees (SCHEDULEID,
                                    EMPLOYEEID,
                                    FROMDATE,
                                    TODATE,
                                    DAYDATE,
                                    SHIFTS,
                                    CREATEDATE)
          VALUES (@param_template_id,
                   employees.EmpID,
                  CONVERT (DATETIME, @param_from, 'dd/MM/yyyy'),
                  CONVERT (DATETIME, @param_to, 'dd/MM/yyyy'),
                  CONVERT (DATETIME, @day_date, 'dd/MM/yyyy'),
                  @current_shift,
                  GETDATE());
  FETCH employees INTO;
  END;
  CLOSE employees;
  DEALLOCATE employees;

  SET @current_index = @current_index + 1;
  FETCH cur_r INTO;
  END;
  CLOSE cur_r;
  DEALLOCATE cur_r;
  DELETE FROM EmployeesTempList;
  COMMIT;
  END;
  GO

0 个答案:

没有答案