简体   繁体   中英

Flexible Update Stored Procedure Mysql

I am looking for a flexible update stored procedure. I made this one. When i call this CALL spUpdatePage(1,'a','b','c''d') it works fine.

Do i have to pass always in this example 5 params?

Can i also do this

CALL spUpdatePage(1,'a','c') 

and that the stored procedure knows that i only want to update the columns name and description?

CREATE DEFINER=`root`@`localhost` PROCEDURE `spUpdatePage`(
IN `pKey` INT(4), 
IN `name` VARCHAR(255), 
IN `title` VARCHAR(255), 
IN `description` VARCHAR(255), 
IN `keywords` VARCHAR(255))
UPDATE
    pages
SET
    name = COALESCE(name, name),
    title = COALESCE(title, title),
    description = COALESCE(description, description),
    keywords = COALESCE(keywords, keywords)
WHERE
    id = pKey

Do I have to pass always in this example 5 params?

Can I also do this?

 CALL spUpdatePage(1,'a','c') 

For your posted stored procedure, you must pass all 5 parameters. No, you can't just pass name and description (to your posted stored procedure). However, you could write a stored procedure that only updates name and description . Something like,

CREATE DEFINER=`root`@`localhost` PROCEDURE `spUpdatePageNameTitle`(
  IN `pKey` INT(4), 
  IN `name` VARCHAR(255), 
  IN `title` VARCHAR(255))
UPDATE
  pages
SET
  name = COALESCE(name, name),
  title = COALESCE(title, title)
WHERE
  id = pKey

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