Du skal bruge Dynamisk SQL her som Default
klausul i Alter Table
vil ikke være i stand til at løse variabelværdien:
DELIMITER $$
CREATE PROCEDURE updateDefaultUserRole(
IN rid_in INT
) BEGIN
-- generate the query string for Alter Table
SET @alter_query_str = CONCAT('ALTER TABLE _users
MODIFY rid INT(255) NOT NULL
DEFAULT ',
rid_in); -- Modify the columns default value
-- prepare the query
PREPARE stmt FROM @alter_query_str;
-- execute the query
EXECUTE stmt;
-- deallocate the query
DEALLOCATE PREPARE stmt;
UPDATE _users SET rid = rid_in
WHERE rid < rid_in; -- Update all entries lower than the role ID.
END $$
DELIMITER ;