Du måste använda Dynamisk SQL här, som Default
sats i Alter Table
kommer inte att kunna lösa variabelvärdet:
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 ;