Search code examples
sqlproceduremysql-error-1064executeheidisql

What's wrong with my EXECUTE command?


I'm trying to make a PROCEDURE that makes it easier to store changes users make in their settings (like a server log, you know right) into a table user_settings_changelog. I finally made the PROCEDURE acceptable so my sql program (HeidiSQL) would store it. Now the problem is this: how to properly EXECUTE it. I tried this multiple times with multiple lines of code, but it seemed nothing worked. Can anyone help me out here?

The PROCEDURE query:

DELIMITER $$
CREATE PROCEDURE setting_adjustment_log (name_setting VARCHAR(45), 
changed_from VARCHAR(45), changed_to VARCHAR(45), username 
VARCHAR(45))
BEGIN
    INSERT INTO user_settings_changelog
    VALUES (GETDATE(), name_setting, changed_from, changed_to, 
username);
END$$

The table user_settings_changelog has 5 columns: date DATETIME, name_setting VARCHAR(45), changed_from VARCHAR(45), changed_to VARCHAR(45) and username VARCHAR(45).

The EXECUTE query:

EXECUTE setting_adjustment_log ('background','black','white','TheCoderNoob');

The error HeidiSQL gives me:

SQL 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 '('background','black','white','TheCoderNoob')' at line 1

Maybe useful to mention: I am using a version of USBwebserver from a few years ago.

EDIT: I've been looking at the EXECUTE/CALL query in MySQL Workbench for a while, it seems like the database expects something between the name of the procedure and the given data or something. When I hover over it, it reads:

Syntax error: 'background' (single quoted text) is not a valid input at this position


Solution

  • First, you should always include the column names in an insert statement. (There might be a few exceptions, but if you are learning SQL, then sticks with best practices.)

    Second, distinguish the input parameters from possible column names.

    Third, your code looks like MySQL, so use NOW() or a similar function:

    DELIMITER $$
    CREATE PROCEDURE setting_adjustment_log (
        in_name_setting VARCHAR(45), 
        in_changed_from VARCHAR(45),
        in_changed_to VARCHAR(45),
        in_username VARCHAR(45)
    )
    BEGIN
        INSERT INTO user_settings_changelog (date, name_setting, changed_from, changed_to, username)
        VALUES (NOW(), in_name_setting, in_changed_from, in_changed_to, 
    in_username);
    END$$
    

    When you call the stored procedure, use call:

    CALL setting_adjustment_log('background', 'black', 'white', 'TheCoderNoob');