简体   繁体   中英

How do to do an if statement in a stored procedure

Basically, i'm going to be passing my parameter SystemFeedbackidParam if SystemFeedbackidParam is not null, do one thing, if it is, then do the seperate thing. I can do this in SQL but the way to do this is mysql is eluding me.

Thanks!

CREATE PROCEDURE `_Insert_FeedBack`(
   FeedbackTypeParam       varchar(20),
   FeedbackSubjectParam    varchar(200),
   FeedbackTextParam       text,
   FeedbackHTMLParam       longtext,
   SubmittedByParam        varchar(20),
   SubmittedDateParam      datetime,
   SystemFeedbackidParam int)
   BEGIN
      IF (SystemFeedbackidParam IS NOT NULL)
      THEN
--/*Insert Child Record into tblfeedbackitems */
         INSERT INTO tblfeedbackitems(SystemFeedbackId,
                                      FeedbackType,
                                      FeedbackText,
                                      FeedbackHTML,
                                      SubmittedBy,
                                      SubmittedDate)
         VALUES (SystemFeedbackidParm,
                 FeedbackTypeParam            
                                  ,
                 FeedbackTextParam          
                                  ,
                 FeedbackHTMLParam          
                                  ,
                 SubmittedByParam           
                                 ,
                 SubmittedDateParam         
                                   );
      ELSE
 --/*Insert Parent Record into tblFeedback */

         INSERT INTO tblfeedback(
                                 FeedbackType,
                                 FeedbackSubject,
                                 FeedbackStatus)
         VALUES (
                 FeedbackTypeParam,
                 FeedbackSubjectParam,
                 'Open');

--/*Insert Child Record into tblFeedback */
         INSERT INTO tblfeedbackitems(SystemFeedbackId,
                                      FeedbackType,
                                      FeedbackText,
                                      FeedbackHTML,
                                      SubmittedBy,
                                      SubmittedDate)
         VALUES (LAST_INSERT_ID(),
                 FeedbackTypeParam            
                                  ,
                 FeedbackTextParam          
                                  ,
                 FeedbackHTMLParam          
                                  ,
                 SubmittedByParam           
                                 ,
                 SubmittedDateParam         
                                   );
      END

You have several problems here:

  • Comments start with --(space) - yours are missing the space
  • Delimiters. Change the delimiter at the top so that the semi-colons don't terminate your procedure. Change it back at the bottom
  • You've opened and IF statement but not closed it
  • You've opened a BEGIN but not terminated the closing END

Try this:

Delimiter $$
CREATE PROCEDURE `_Insert_FeedBack`(
   FeedbackTypeParam       varchar(20),
   FeedbackSubjectParam    varchar(200),
   FeedbackTextParam       text,
   FeedbackHTMLParam       longtext,
   SubmittedByParam        varchar(20),
   SubmittedDateParam      datetime,
   SystemFeedbackidParam int)
   BEGIN
      IF (SystemFeedbackidParam IS NOT NULL)
      THEN
-- /*Insert Child Record into tblfeedbackitems */
         INSERT INTO tblfeedbackitems(SystemFeedbackId,
                                      FeedbackType,
                                      FeedbackText,
                                      FeedbackHTML,
                                      SubmittedBy,
                                      SubmittedDate)
         VALUES (SystemFeedbackidParm,
                 FeedbackTypeParam            
                                  ,
                 FeedbackTextParam          
                                  ,
                 FeedbackHTMLParam          
                                  ,
                 SubmittedByParam           
                                 ,
                 SubmittedDateParam         
                                   );
      ELSE
 -- /*Insert Parent Record into tblFeedback */

         INSERT INTO tblfeedback(
                                 FeedbackType,
                                 FeedbackSubject,
                                 FeedbackStatus)
         VALUES (
                 FeedbackTypeParam,
                 FeedbackSubjectParam,
                 'Open');

-- /*Insert Child Record into tblFeedback */
         INSERT INTO tblfeedbackitems(SystemFeedbackId,
                                      FeedbackType,
                                      FeedbackText,
                                      FeedbackHTML,
                                      SubmittedBy,
                                      SubmittedDate)
         VALUES (LAST_INSERT_ID(),
                 FeedbackTypeParam            
                                  ,
                 FeedbackTextParam          
                                  ,
                 FeedbackHTMLParam          
                                  ,
                 SubmittedByParam           
                                 ,
                 SubmittedDateParam         
                                   );
      END IF; 
END $$
Delimiter ;

Note: I've fixed your syntax errors. I have no idea whether your code will actually work.

check the documentation of mysql on this site http://dev.mysql.com/doc/refman/5.0/en/if.html there you can find all the info, but basically is like this

IF NOT SystemFeedbackidParam THEN
......
 END IF;

also you can use the else statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM