Mysql stored procedure gives unknown column error when executed [duplicate]

坚强是说给别人听的谎言 提交于 2021-01-29 03:20:46

问题


I have written a small stored procedure to store redmine issue and timelog entries in my private redmine. The procedure is stored, but when I call it, it gives SQL error (1054): Unknown column 'uid' in 'field list' error. The column that the error message mention is exists, the type matches.

Rewrite code, redeploy db

When the second insert removed from SP, it works great.

delimiter $$
DROP PROCEDURE if exists redmine.insert_fingerprint_workday_and_hour $$

CREATE DEFINER=`root`@`%` PROCEDURE `insert_fingerprint_workday_and_hour`(
    IN `userid` INT(11) )
LANGUAGE SQL
NOT DETERMINISTIC
MODIFIES SQL DATA
SQL SECURITY DEFINER
COMMENT ''

BEGIN

set @uid=userid;

INSERT INTO issues (
tracker_id,
project_id,
subject,
description,
due_date,
category_id,
status_id,
assigned_to_id,
priority_id,
fixed_version_id,
author_id,
lock_version,
created_on,
updated_on,
start_date,
done_ratio,
estimated_hours,
parent_id,
root_id,
lft,
rgt,
is_private,
closed_on ) 
 VALUES
(
  "4",                      -- tracker id
  "1",                          -- project id
  "teszt subject",      -- subject  
  "teszt description",  -- description 
  NULL,                         -- due_date
  NULL,                         -- category_id
  "7",                          -- status id
  uid,              -- assigned_to_id
  "2",                          -- priority_id
  NULL,                     -- fixed_version_id
  uid,                  --  author_id
  "0",                          -- lock_version
  NOW(),                    -- created_on
  NOW(),                        -- updated_on
  date(now()),              -- start_date
  "0",                      -- done_ratio
  NULL,                     -- estimated_hours
  NULL,                     -- parent_id
  "1",                      -- root_id
  "1",                      -- lft
  "2",                      -- rgt
  "0",                      -- is_private
  now()                     -- closed_on
  );

  set @ujissue=LAST_INSERT_ID();

  insert into time_entries 
  (project_id,user_id,issue_id,hours,activity_id,spent_on,tyear,tmonth,tweek,created_on,updated_on)
  values
  (
  "1",                      -- project_id
  uid,                  -- user_id
  ujissue,                  -- issue_id
  "8",                      -- hours
  "10",                     -- activity_id
  date(now()),              -- spent_on
  year(now()),              -- tyear
  month(now()),         -- tmonth
  week(now()),              -- tweek
  now(),                        -- created_on
  now()
  );

  END $$

  DELIMITER ;

SQL> CALL redmine.insert_fingerprint_workday_and_hour('7');
/* SQL error (1054): Unknown column 'uid' in 'field list' */

The only input is a number, a user_id. The code should add an issue to a project and log 8 workhours. The first insert adds the issue to the project and a second logs the hours.

After hours of debugging I cannot figure out what can be the problem.

All advice is welcome!

Thanks, Peter


回答1:


In MySQL, variables with the @ sigil are session variables, not local variables. Session variables are different from local variables, even if they have the same name other than the @ sigil.

See also a few of my past answers on this:

  • "@" symbol in stored procedure?
  • MySql syntax error on procedure parameter
  • Stored Procedure Null Error


来源:https://stackoverflow.com/questions/58070946/mysql-stored-procedure-gives-unknown-column-error-when-executed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!