Error code 1415 not allowed to return a result set from a trigger

CREATE TRIGGER `usersToChanges` AFTER INSERT ON tbl1 FOR EACH ROW BEGIN SELECT * FROM tbl2 WHERE tbl2.id=tbl1.id; INSERT INTO tbl3 (col1,col2,col3, col4, col5,col6) VALUES (456,NOW(),'test','
 CREATE TRIGGER `usersToChanges` AFTER INSERT ON tbl1 FOR EACH ROW
BEGIN
SELECT * FROM tbl2 WHERE tbl2.id=tbl1.id;
INSERT INTO tbl3 (col1,col2,col3, col4, col5,col6) 
        VALUES (456,NOW(),'test','test',46000,123);
END

i want to create a trigger like above. i need to select data from tbl2 because the values to be inserted in tbl3 come from tbl2. the values shown here are just dummy values . And i am getting error [Err] 1415 — Not allowed to return a result set from a trigger.
Any Help would be appreciated.

asked Nov 9, 2015 at 19:27

Atif's user avatar

Are you wanting something like

CREATE TRIGGER `usersToChanges` AFTER INSERT ON tbl1 FOR EACH ROW
BEGIN 
INSERT INTO tbl3 (col1,col2,col3, col4, col5,col6) 
SELECT 456,NOW(),'test','test',46000,123 
FROM tbl2 WHERE tbl2.id=NEW.id;
END

answered Nov 9, 2015 at 19:30

wogsland's user avatar

wogslandwogsland

8,85019 gold badges57 silver badges89 bronze badges

6

Error Code: 1415. Not allowed to return a result set from a trigger

Discussion in ‘MySQL’ started by ying7690, Jul 4, 2011.


  1. ying7690

    ying7690
    New Member

    Joined:
    Jul 4, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0

    I have the following code for trigger
    the syntax message was

    «Error Code: 1415. Not allowed to return a result set from a trigger»

    delimiter $$   CREATE TRIGGER  New_User AFTER INSERT ON  tblAdmin   FOR EACH ROW  BEGIN  DECLARE _AdminRECID char(36);  DECLARE _CreateUser varchar(50);  DECLARE _CreateDate datetime;  DECLARE _AuditUser varchar(50);  DECLARE _AuditDate datetime;  DECLARE _MenuRECID CHAR(36);   SELECT RECID = _AdminRECID, 	    CreateUser = _CreateUser, 	    CreateDate = _CreateDate, 	    AuditUser  = _AuditUser, 	    AuditDate  = _AuditDate  FROM tblAdmin;    SELECT RECID = _MenuRECID FROM tblMenu;    	  INSERT INTO tblAdmin_Access(RECID,AdminRECID,MenuRECID,Status,   CreateUser,CreateDate,AuditUser,AuditDate) 	 VALUES (UUID(),_AdminRECID,_MenuRECID,'A', _CreateUser,CURDATE(),_AuditUser,CURDATE());     END;

  2. ying7690

    ying7690
    New Member

    Joined:
    Jul 4, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0

    Not allowed to return a result set from a trigger

    I have the following code for trigger
    the syntax message was

    «Error Code: 1415. Not allowed to return a result set from a trigger»

    delimiter $$
    
    CREATE TRIGGER  New_User AFTER INSERT ON  tblAdmin
    
    FOR EACH ROW
    BEGIN
    DECLARE _AdminRECID char(36);
    DECLARE _CreateUser varchar(50);
    DECLARE _CreateDate datetime;
    DECLARE _AuditUser varchar(50);
    DECLARE _AuditDate datetime;
    DECLARE _MenuRECID CHAR(36);
    
    SELECT RECID = _AdminRECID,
           CreateUser = _CreateUser,
           CreateDate = _CreateDate,
           AuditUser  = _AuditUser,
           AuditDate  = _AuditDate
    FROM tblAdmin;
    
    SELECT RECID = _MenuRECID
    FROM tblMenu;
    
    
    
        INSERT INTO tblAdmin_Access(RECID,AdminRECID,MenuRECID,Status,CreateUser,CreateDate,AuditUser,AuditDate)
        VALUES (UUID(),_AdminRECID,_MenuRECID,'A',_CreateUser,CURDATE(),_AuditUser,CURDATE());
    
    
    END;
    
    

  3. shabbir

    shabbir
    Administrator
    Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,369
    Likes Received:
    387
    Trophy Points:
    83

    Please don’t create two separate threads and I have merged both of them for you.


  4. ying7690

    ying7690
    New Member

    Joined:
    Jul 4, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0

    thanks.because i cant remove the previous thread…

Share This Page


Go4Expert

franc_gs

5 / 5 / 4

Регистрация: 17.11.2012

Сообщений: 17

1

Триггер на запрет обновления

19.11.2012, 04:16. Показов 6058. Ответов 2

Метки нет (Все метки)


Есть таблица «Поставки», где имеется поле «Статус документа». Дело в том, что нужно сделать триггер , который не будет позволять редактировать записи, где статус документа >= ‘3’ Помогите пожалуйста сделать правильно.
Мой код: выдает ошибку #1415 — Not allowed to return a result set from a trigger

SQL
1
2
3
4
5
6
7
8
9
10
DELIMITER $$
CREATE TRIGGER `no_delete` BEFORE UPDATE ON `punkt_postavka`
FOR EACH ROW
 BEGIN
SELECT kod_status  INTO @kod FROM punkt_postavka  ;
IF (KOD >='3' )THEN
SELECT 'NET DOSTUPA';
END IF;
 
END $$

__________________
Помощь в написании контрольных, курсовых и дипломных работ, диссертаций здесь



0



Programming

Эксперт

94731 / 64177 / 26122

Регистрация: 12.04.2006

Сообщений: 116,782

19.11.2012, 04:16

2

lionn2233

19.11.2012, 17:01

2

SQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
DELIMITER $$
CREATE TRIGGER `no_update` BEFORE UPDATE ON `postavka`
FOR EACH ROW
 BEGIN
 
IF (OLD.kod_status >= 3 )THEN
SET NEW.kod_postavka = OLD.kod_postavka;
SET NEW.kod_personal= OLD.kod_personal;
SET NEW.kod_kontragent = OLD.kod_kontragent;
SET NEW.date_postavka = OLD.date_postavka;
SET NEW.kod_status = OLD.kod_status;
END IF;
 
END $$

1176 / 418 / 106

Регистрация: 31.03.2012

Сообщений: 1,136

20.11.2012, 08:10

3

Цитата
Сообщение от franc_gs
Посмотреть сообщение

выдает ошибку #1415 — Not allowed to return a result set from a trigger

а не пробовал перевести сообщение об ошибке? А именно, триггер не может возвращать какой-либо набор данных



0



For 5.5 and later it is possible to use signals:

delimiter @

create trigger checkcollision 
after update on players 
for each row 
begin 
    declare dummy int default 0; 
    select 1 into dummy from walls where x=NEW.x and y=NEW.y; 
    if (dummy = 1) then 
        SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Any Message'; 
    end if; 
end @

delimiter ;

For 5.1 and earlier version there is no support for signal. You could try to mimic it with an forced exception, like division by zero or by referencing something that does not exist. You don’t get a nice error message though:

delimiter @

create trigger checkcollision  
after update on players  
for each row  begin
    declare dummy int default 0;
    select 1 into dummy from walls where x=NEW.x and y=NEW.y;
    if (dummy = 1) then
        select 1/0 into dummy; 
    end if;
end @

delimiter ;

A slightly more elegant way is to use an exit handler which is supported in 5.1, still no error message though:

create trigger checkcollision  
after update on players  
for each row  
begin
    declare dummy int default 0; 
    DECLARE EXIT HANDLER FOR NOT FOUND begin end;
    select 1 into dummy from walls where x=NEW.x and y=NEW.y;
    select 1/0 into dummy;
end @

If no wall is found an empty exit handler is invoked, otherwise the trigger continues and an deliberate error is made.

By adding a dummy table like:

create table dummy (msg varchar(100) primary key);

We can force a primary key violation by inserting the same value twice from the trigger:

delimiter @
create trigger checkcollision  
after update on players  
for each row  begin
    declare dummy int default 0; 
    DECLARE EXIT HANDLER FOR NOT FOUND begin end;     
    select 1 into dummy from walls where x=NEW.x and y=NEW.y; 
    insert into dummy (msg) values ('ERROR: Collision')
                                 , ('ERROR: Collision');  
end @
delimiter ;

We will get an error message like (tested in 10.0.20-MariaDB):

ERROR 1062 (23000): Duplicate entry 'ERROR: Collision' for key 'PRIMARY'

You might want to encapsulate this into a stored procedure:

create procedure my_signal (msg varchar(100)) 
begin 
    insert into dummy (msg) 
    values ('ERROR: Collision')
         , ('ERROR: Collision'); 
end @

which can be called from the trigger:

create trigger checkcollision  
after update on players  
for each row  
begin
    declare dummy int default 0; 
    DECLARE EXIT HANDLER FOR NOT FOUND begin end;
    select 1 into dummy from walls where x=NEW.x and y=NEW.y; 
    call my_signal('ERROR: Collision');  
end @

M

What you are looking for is the inverse of a GROUP BY aggregate query using the GROUP_CONCAT. If you are willing to store the results in a temp table, I got just the thing.
First, here is the code to use you sample data in a table called prod and a temp table called prodcat to hold the results you are looking for.
use test
drop table if exists prod;
drop table if exists prodcat;
create table prod
(
product_id int not null,
categories varchar(255)
) engine=MyISAM;
create table prodcat
(
product_id int not null,
cat int not null
) engine=MyISAM;
insert into prod values
(10,’9,12′),(11,’8′),(12,’11,18,5′);
select * from prod;

Here it is loaded
mysql> use test
Database changed
mysql> drop table if exists prod;
Query OK, 0 rows affected (0.00 sec)

mysql> drop table if exists prodcat;
Query OK, 0 rows affected (0.00 sec)

mysql> create table prod
-> (
-> product_id int not null,
-> categories varchar(255)
-> ) engine=MyISAM;
Query OK, 0 rows affected (0.07 sec)

mysql> create table prodcat
-> (
-> product_id int not null,
-> cat int not null
-> ) engine=MyISAM;
Query OK, 0 rows affected (0.06 sec)

mysql> insert into prod values
-> (10,’9,12′),(11,’8′),(12,’11,18,5′);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from prod;
+————+————+
| product_id | categories |
+————+————+
| 10 | 9,12 |
| 11 | 8 |
| 12 | 11,18,5 |
+————+————+
3 rows in set (0.00 sec)

mysql>

OK, you need query to put together each product_id with each category. Here it is:
select concat(‘insert into prodcat select ‘,product_id,’,cat from (select NULL cat union select ‘,
replace(categories,’,’,’ union select ‘),’) A where cat IS NOT NULL;’) ProdCatQueries from prod;

Here it is executed
mysql> select concat(‘insert into prodcat select ‘,product_id,’,cat from (select NULL cat union select ‘,
-> replace(categories,’,’,’ union select ‘),’) A where cat IS NOT NULL;’) ProdCatQueries from prod;
+———————————————————————————————————————————-+
| ProdCatQueries |
+———————————————————————————————————————————-+
| insert into prodcat select 10,cat from (select NULL cat union select 9 union select 12) A where cat IS NOT NULL; |
| insert into prodcat select 11,cat from (select NULL cat union select 8) A where cat IS NOT NULL; |
| insert into prodcat select 12,cat from (select NULL cat union select 11 union select 18 union select 5) A where cat IS NOT NULL; |
+———————————————————————————————————————————-+
3 rows in set (0.00 sec)

mysql>

Let me run each line by hand
mysql> insert into prodcat select 10,cat from (select NULL cat union select 9 union select 12) A where cat IS NOT NULL;
Query OK, 2 rows affected (0.07 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> insert into prodcat select 11,cat from (select NULL cat union select 8) A where cat IS NOT NULL;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> insert into prodcat select 12,cat from (select NULL cat union select 11 union select 18 union select 5) A where cat IS NOT NULL;
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql>

OK, good. The queries work. Did the prodcat table populate properly?
mysql> select * from prodcat;
+————+——+
| product_id | cat |
+————+——+
| 10 | 9 |
| 10 | 12 |
| 11 | 8 |
| 12 | 11 |
| 12 | 18 |
| 12 | 5 |
+————+——+
6 rows in set (0.00 sec)

mysql>

OK Great. It has the data.
To be honest, I think SQL Server can perform all of this in a single pivot query without a handmade temp table.
I could have taken it to another level and concatenated all the queries into a single query, but the SQL would have been insanely long. If your actual query had 1000s of rows, a single MySQL would not have been practical.
Instead of running the 3 INSERT queries by hand, you could echo the 3 INSERT queries to a text file and execute it as a script. Then, you have a table with the products and categories combinations individually written.

Понравилась статья? Поделить с друзьями:
  • Error code 1415 not allowed to return a result set from a function
  • Error code 12007 no such host
  • Error code 120004
  • Error code 1200
  • Error code 12 не удалось обновить мастерлист loot