sql - Declare variable MySQL trigger -


my question might simple you, if you're used mysql. i'm used postgresql sgbd , i'm trying translate pl/pgsql script mysql.

here have :

delimiter //  create trigger pgl_new_user  after insert on users each row begin     declare m_user_team_id integer;     select id m_user_team_id user_teams name = "pgl_reporters";      declare m_projects_id integer;     declare cur cursor select project_id user_team_project_relationships user_team_id = m_user_team_id;      open cur;         ins_loop: loop             fetch cur m_projects_id;             if done                 leave ins_loop;             end if;             insert 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// 

but mysql workbench gives me error on declare m_projects_id. don't understand because i've same instruction 2 lines above...

any hints ?

edit: neubert solved error. thanks.

but yet, when try insert users :

error code: 1329. no data - 0 rows fetched, selected, or processed 

do have idea ? or better, know how can better error message ?

all declares need @ top. ie.

delimiter //  create trigger pgl_new_user  after insert on users each row begin     declare m_user_team_id integer;     declare m_projects_id integer;     declare cur cursor select project_id user_team_project_relationships user_team_id = m_user_team_id;      set @m_user_team_id := (select id user_teams name = "pgl_reporters");      open cur;         ins_loop: loop             fetch cur m_projects_id;             if done                 leave ins_loop;             end if;             insert 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// 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -