Mysql procedure does not recognize WHERE and displays error

Asked

Viewed 143 times

-2

Running procudure below Mysql Workbench shows error:

"Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column. To disable safe mode, toggle the option in Preferences -> SQL Editor and Reconnect."

However, the only UPDATE of the past uses the primary key of the table.

    DELIMITER $$ 
    CREATE PROCEDURE Editar_Produtos(
     IN Id_Pro INT,
     IN Cod_Int VARCHAR (21),
     IN Nom_Pro VARCHAR (55),
     IN Id_Cat INT,
     IN Id_Cla INT,
     IN Id_Sub INT,
     IN Id_Mil INT,
     IN Id_Med INT,
     IN Id_Mar INT,
     IN Vlr_Cust DECIMAL (9,2),
     IN Vlr_Vend DECIMAL (9,2),
     IN Id_Un INT,
     IN Id_Tpd INT
    )
    BEGIN
     UPDATE ktsproduto 
     SET COD_INT = Cod_Int, 
         NOM_PRO = Nom_Pro, 
         ID_CAT = Id_Cat, 
         ID_CLA = Id_Cla, 
         ID_SUB = Id_Sub, 
         ID_MIL = Id_Mil, 
         ID_MED = Id_Med, 
         ID_MAR = Id_Mar, 
         VLR_CUST = Vlr_Cust, 
         VLR_VEND = Vlr_Vend, 
         ID_UN = Id_Un,      
         ID_TPD = Id_Tpd 
     WHERE ID_PRO = Id_Pro;    
    END $$
    DELIMITER ;

    CALL Editar_Produtos(
    '417',"KTSKTSKTS","KENNEDY TEIXIERA",'1','1','1','1','1','1','250.99','500.99','1','1');

I do not understand why the mistake, since I am using the clause WHERE with the primary key of the table.

If possible, I don’t want to rotate SET SQL_SAFE_UPDATES=0;, for safety reasons.

  • Restart mysql server and test.

  • ID_PRO is a key ?

  • Yes, it’s a key.

  • 1

    I was able to solve the problem, after doing many tests, realize that when removing the underline " _ " of all variables worked perfectly, ie Id_pro, Id_cat were like this Idpro, Idcat configured from this outside and gave right. I thank the other people who cared about my question. Thank you.

  • I could not understand why the question was closed. It seems to me a good question and with the problem well exposed.

  • @Kennedyteixeira, although I have "turned the problem around," I think I can explain his reason in my answer. Came to check if it works properly?

  • @tvdias thank you very much, your solution proposal worked, now I can better organize my code, this topic is very useful, should not be closed.

  • As for the closing, let’s see if it’s reviewed. :)

Show 3 more comments

1 answer

0


Mysql is considering both ID_PRO and Id_Pro in your Where as Id_Pro which is passed as input from your database. In this case, if the Safe Update, all records would be updated if found any column with valid name. The same can happen when your application is used outside the MySQL Workbench. As there are "case sensitive" differences between Windows and Linux, this behavior may vary depending on your operating system.

One way to solve this problem is by identifying the tables next to the columns:

DELIMITER $$ 
CREATE PROCEDURE Editar_Produtos(
 IN Id_Pro INT,
 IN Cod_Int VARCHAR (21),
 IN Nom_Pro VARCHAR (55),
 IN Id_Cat INT,
 IN Id_Cla INT,
 IN Id_Sub INT,
 IN Id_Mil INT,
 IN Id_Med INT,
 IN Id_Mar INT,
 IN Vlr_Cust DECIMAL (9,2),
 IN Vlr_Vend DECIMAL (9,2),
 IN Id_Un INT,
 IN Id_Tpd INT
)
BEGIN
 UPDATE ktsproduto 
 SET ktsproduto.COD_INT = Cod_Int, 
     ktsproduto.NOM_PRO = Nom_Pro, 
     ktsproduto.ID_CAT = Id_Cat, 
     ktsproduto.ID_CLA = Id_Cla, 
     ktsproduto.ID_SUB = Id_Sub, 
     ktsproduto.ID_MIL = Id_Mil, 
     ktsproduto.ID_MED = Id_Med, 
     ktsproduto.ID_MAR = Id_Mar, 
     ktsproduto.VLR_CUST = Vlr_Cust, 
     ktsproduto.VLR_VEND = Vlr_Vend, 
     ktsproduto.ID_UN = Id_Un,      
     ktsproduto.ID_TPD = Id_Tpd 
 WHERE ktsproduto.ID_PRO = Id_Pro;    
END $$
DELIMITER ;

CALL Editar_Produtos(
'417',"KTSKTSKTS","KENNEDY TEIXIERA",'1','1','1','1','1','1','250.99','500.99','1','1');
  • 1

    Would you care to at least comment on the reason for the negative vote?

  • I wasn’t the one who said no, I found the question in a line of analysis, But perhaps the answer was negative because it only corrected the code and superficially addressed the AP problem, which is the lack of understanding about sensitivity and the differentiation between upper and lower case in SQL. Of course I may be mistaken, people are negative for the most absurd reasons, but from a logical perspective I believe that is the most plausible reason.

  • I can almost take it for granted that this is the reason, reading in the question the last comment of the author is visible that he has reached a solution by way of wrong conclusion and he needs clarification and does not yet know.

  • I also could not understand why the question was closed. It seems to me a good question and with the problem well exposed.

  • 1

    The question was closed because it was successive syntax errors. At no time did I say it was a good question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.