简体   繁体   中英

How to fix error in procedure SQL?

there is a hranimka, when it was created, an error occurs. Maybe she who struck by the ...

The stored procedure:

   CREATE PROCEDURE insert_log(

  IN LogType INT, 
  IN LogIdNote INT,
  IN LogName VARCHAR, 
  IN LogTime TIMESTAMP, 
  IN logTypeCategory INT, 
  IN LogIdUser INT) 

begin

INSERT INTO log (LogType, 
                 LogIdNote, 
                 LogName, 
                 LogTime, 
                 logTypeCategory, 
                 LogIdUser, 
                 LogTypeUser, 
                 LogUrl)
SELECT LogType, LogIdNote, LogName, LogTime, logTypeCategory, LogIdUser, url.URLCategorysubscribetotype, u.UsersTypeAccount FROM users u LEFT JOIN categorysubscribetotype url ON url.CategoryTypeCategorysubscribetotype = LogType WHERE u.idUsers = LogIdUser;

end //

Error:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INT LogType, INT LogIdNote, VARCHAR LogName, TIMESTAMP LogTime, I' at line 3

I tried only change data types at params.

I think, the next code will give me a good result, but I need save result from SELECT query at variable and insert it at query Insert:

DELIMITER |

CREATE PROCEDURE insert_log(

  IN pLogType INT, 
  IN pLogIdNote INT,
  IN pLogName VARCHAR(150), 
  IN pLogTime TIMESTAMP, 
  IN plogTypeCategory INT, 
  IN pLogIdUser INT) 

BEGIN

DECLARE user_type INT DEFAULT 0;
DECLARE url VARCHAR(250) DEFAULT;

SET user_type = (SELECT UsersTypeAccount FROM users WHERE idUsers = pLogIdUser);
SET url = (SELECT URLCategorysubscribetotype FROM categorysubscribetotype WHERE CategoryTypeCategorysubscribetotype = pLogType);

INSERT INTO log (pLogType, 
                 pLogIdNote, 
                 pLogName, 
                 pLogTime, 
                 plogTypeCategory, 
                 pLogIdUser, 
                 pLogTypeUser, 
                 pLogUrl)
VALUES (
                 LogType, 
                 LogIdNote, 
                 LogName, 
                 LogTime, 
                 logTypeCategory, 
                 LogIdUser, 
                 user_type, 
                 url
);

END |

delimiter ;

Your syntax is wrong . The data types come after the parameter names, the IN / OUT specifiers come before. Something like this:

CREATE PROCEDURE insert_log(

  IN LogType INT, 
  IN LogIdNote INT,
  IN LogName VARCHAR(10), 
  IN LogTime TIMESTAMP, 
  IN logTypeCategory INT, 
  IN LogIdUser INT)

begin

...

Edit: Also note that I added a size specifier to the VARCHAR data type, since it requires one . I guessed at 10 , but you can replace that value with whatever yours is.

Your issue is here:

INSERT INTO log (pLogType, //wrong!
                 pLogIdNote, 
                 pLogName, 
                 pLogTime, 
                 plogTypeCategory, 
                 pLogIdUser, 
                 pLogTypeUser, 
                 pLogUrl)

You have used the parameter as column while they should be VALUES try this Query

DELIMITER //
 CREATE PROCEDURE insert_log(
    IN pLogType INT, 
    IN pLogIdNote INT,
    IN pLogName VARCHAR(150), 
    IN pLogTime TIMESTAMP, 
    IN plogTypeCategory INT, 
    IN pLogIdUser INT) 

   BEGIN

    DECLARE user_type INT DEFAULT 0;
    DECLARE url VARCHAR(250) DEFAULT;

    SET user_type = (
                        SELECT UsersTypeAccount 
                        FROM users 
                        WHERE idUsers = pLogIdUser
                    );
    SET url = (
                        SELECT URLCategorysubscribetotype 
                        FROM categorysubscribetotype 
                        WHERE CategoryTypeCategorysubscribetotype = pLogType
               );

    INSERT INTO log (
                     `LogType`, 
                     `LogIdNote`, 
                     `LogName`, 
                     `LogTime`, 
                     `logTypeCategory`, 
                     `LogIdUser`, 
                     `LogIdUserType`, /*I added this*/
                     `LogIdUrl`,     /*this one too */
    )VALUES (
                     pLogType, 
                     pLogIdNote, 
                     pLogName, 
                     pLogTime, 
                     plogTypeCategory,  
                     pLogIdUser,
                     user_type, 
                     url
    );

   END //
 DELIMITER ;

Please note You need to adjust this stored procedure, there was few mistakes. for example pLogTypeUser and pLogUrl are undefined and I added comments where you need to change the column name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM