簡體   English   中英

SQL Server 存儲過程到 MySQL

[英]SQL Server Stored Procedure to MySQL

我需要將 SQL Server 存儲過程轉換為 MySQL,找到了這個工具: http : //www.sqlines.com/online但轉換后 Workbench 給我一個錯誤,DELIMITER 說它在這個位置無效:創建預期的。

不幸的是,SQL Server 數據庫無法遷移,因此我需要轉換此過程的整個代碼 - 是否有一種簡單的方法可以轉換此代碼,或者有人知道為什么會出現 DELIMITER 錯誤?

        DELIMITER //

CREATE PROCEDURE CreateQuestionnairForCourse (

p_courseId bigint,
p_openDate bigint /* = 1573884000 */, -- 16 Nov 2019
p_closeDate bigint /* = 1575150900 */,  -- 30 Nov 2019
   
p_servey_etalon bigint /* =20 */,  -- Greek English together
p_questionnair_etalon bigint/* =20 */)

sp_lbl:

BEGIN
DECLARE NOT_FOUND INT DEFAULT 0;
   -- SET NOCOUNT ON added to prevent extra result sets from
   -- interfering with SELECT statements.


DECLARE v_surveyId bigint;
DECLARE v_questionnairId bigint;
DECLARE v_courseModuleId bigint;
DECLARE v_sequence longtext;
DECLARE v_section bigint;
DECLARE v_added bigint;
DECLARE v_questionId bigint;
DECLARE v_new_questionId bigint;
DECLARE v_IsVisible tinyint DEFAULT 1

declare curs cursor local;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET NOT_FOUND = 1;;


if p_courseId = 0 then
   LEAVE sp_lbl;
end if;
ELSE

      DECLARE v_UnixTS bigint DEFAULT [dbo].[DTtoUnixTS](NOW());

      BEGIN TRAN T1;

       INSERT INTO mdl_questionnaire_survey
       (
          `name`,
          `courseid`,
          `realm`,
          `status`,
          `title`,
          `email`,
          `subtitle`,
          `info`,
          `theme`
       )
        SELECT
         name,
         p_courseId,
         'private',
         `status`,
         title,
         email,
         subtitle,
         info,
         theme
      FROM mdl_questionnaire_survey
      WHERE id =  p_servey_etalon;
              
      SET v_surveyId = LAST_INSERT_ID();

      INSERT INTO mdl_questionnaire
            (`course`,
            `name`,
            intro,
            `qtype` ,
            `respondenttype`,
            `resp_eligible`,
            `resp_view`,
            `opendate`,
            `closedate`,
            `resume`,
            `navigate`,
            `grade`,
            `sid`
            )
         Select
            p_courseId,
            name,
            intro,  
            qtype,respondenttype,
            resp_eligible,
            resp_view,
            p_openDate,
            p_closeDate,
            resume,
            navigate,
            grade,
            v_surveyId
         FROM mdl_questionnaire
         WHERE id=p_questionnair_etalon;
                 
      SET v_questionnairId = LAST_INSERT_ID();

      fast_forward for
      select id  
      FROM `mdl_questionnaire_question`
      where deleted='n' and survey_id=p_servey_etalon   
      open curs;
      fetch next from; curs into v_questionId

         while Not_found = 0
         do         
      
            
            INSERT INTO mdl_questionnaire_question (survey_id,name ,type_id,result_id,`length`,`precise`,`position` ,`content` ,`required` ,`deleted`)
            SELECT v_surveyId,`name`,`type_id` ,`result_id`,`length`,`precise`,`position` ,`content` ,`required` ,`deleted`
            FROM mdl_questionnaire_question where id = v_questionId;
           
            SET v_new_questionId = LAST_INSERT_ID();
         

            INSERT INTO mdl_questionnaire_quest_choice
            SELECT v_new_questionId    
              ,`content`
              ,`value`
           FROM mdl_questionnaire_quest_choice where question_id = v_questionId;           

           fetch next from; curs into v_questionId
         end while;
      
      CLOSE curs;


      SELECT TOP 1 id INTO v_section from mdl_course_sections where course=p_courseId
      ORDER By id ASC;

      INSERT INTO mdl_course_modules  
      (
         course,
         module,
         instance,
         section,
         idnumber,
         added,
         score,
         indent,
         visible,
         visibleold,
         groupmode,
         groupingid,
         -- groupmembersonly,
         completion,
         completionview,
         completionexpected,
         `showdescription`,
         `availability`
      )
      VALUES (
         p_courseId,
         23,
         v_questionnairId,
         v_section,
         0,
         0,
         0,
         0,
         v_IsVisible,
         v_IsVisible,
         0,
         0,
         -- 0,
         0,
         0,
         0,
         0,
         ''
      );

      SET v_courseModuleId = LAST_INSERT_ID();
   
      SELECT  sequence INTO v_sequence from mdl_course_sections where id = v_section;
        
      if v_sequence = '' then
         SET v_sequence = CAST(v_courseModuleId as varchar(1));
      ELSE
         SET v_sequence = Concat(v_sequence , ','  , CAST(v_courseModuleId as varchar(1)));
      end if; -- COLLATE DATABASE_DEFAULT  
         
      UPDATE mdl_course_sections
      SET sequence = v_sequence
      WHERE id=v_section;

      

      UPDATE mdl_course
                   SET cacherev = (;CASE
                       WHEN cacherev IS NULL THEN v_UnixTS
                       WHEN cacherev < v_UnixTS THEN v_UnixTS
                       WHEN cacherev > v_UnixTS + 3600 THEN v_UnixTS
                       ELSE cacherev + 1 END) WHERE id = p_courseId


   COMMIT; T1;

   END;

END IF;
//

DELIMITER ;

在我看來,問題是代碼中的注釋。 可能是轉換工具無法處理 /* = whatewer.. */

同樣在轉換后按照建議重構代碼 - varchar(1) 總是一個壞主意......在你的情況下它可能也是錯誤的,因為我預計課程模塊將長於從 int 轉換的 1 個字符......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM