sql >> Database teknologi >  >> RDS >> Mysql

Erklærer variabel MySQL-trigger

Alle DECLARE skal være øverst. dvs.

delimiter //

CREATE TRIGGER pgl_new_user 
AFTER INSERT ON users FOR EACH ROW
BEGIN
    DECLARE m_user_team_id integer;
    DECLARE m_projects_id integer;
    DECLARE cur CURSOR FOR SELECT project_id FROM user_team_project_relationships WHERE user_team_id = m_user_team_id;

    SET @m_user_team_id := (SELECT id FROM user_teams WHERE name = "pgl_reporters");

    OPEN cur;
        ins_loop: LOOP
            FETCH cur INTO m_projects_id;
            IF done THEN
                LEAVE ins_loop;
            END IF;
            INSERT INTO users_projects (user_id, project_id, created_at, updated_at, project_access) 
            VALUES (NEW.id, m_projects_id, now(), now(), 20);
        END LOOP;
    CLOSE cur;
END//


  1. SQL DELETE for begyndere

  2. SQLite-tabelbegrænsning unik og ON CONFLICT REPLACE-brug

  3. Sådan aktiverer du MySQL Slow Query Log på MySQL

  4. GROUP BY for at kombinere/sammensætte en kolonne