MySQL返回NULL而不是实际值

MySQL返回NULL而不是实际值,mysql,stored-procedures,Mysql,Stored Procedures,我正在编写一个MySQL存储过程,它将选择一行,替换一些列值,然后更新该行。我的问题是SELECT语句没有获取所有值。也就是说,在CLI中具有值的列在我的存储过程中显示为NULL。以下是我的表定义: Field | Type | Null | Key | Default | Extra | id | int(11) | NO | PRI | NULL

我正在编写一个MySQL存储过程,它将选择一行,替换一些列值,然后更新该行。我的问题是SELECT语句没有获取所有值。也就是说,在CLI中具有值的列在我的存储过程中显示为NULL。以下是我的表定义:

Field           | Type        | Null | Key | Default           | Extra                       |
id              | int(11)     | NO   | PRI | NULL              | auto_increment              |
mac             | varchar(32) | YES  | UNI | NULL              
ip              | varchar(16) | YES  |     | NULL              
type            | varchar(16) | YES  |     | NULL              
location_id     | int(11)     | YES  |     | NULL              
name            | varchar(64) | YES  |     | NULL
manufacturer    | varchar(64) | YES  |     | NULL              
model           | varchar(64) | YES  |     | NULL              
serial_number   | varchar(64) | YES  |     | NULL              
owner           | int(11)     | YES  |     | NULL              
assigned_to     | int(11)     | YES  |     | NULL              
contact         | int(11)     | YES  |     | NULL              
admin           | int(11)     | YES  |     | NULL              
status          | int(11)     | YES  |     | NULL              
authorized      | tinyint(1)  | NO   |     | 0                 
authorized_by   | int(11)     | YES  |     | NULL              
date_authorized | datetime    | YES  |     | NULL              
date_first_seen | datetime    | YES  |     | NULL              
date_last_seen  | datetime    | YES  |     | NULL              
impact          | smallint(6) | YES  |     | NULL              
os_id           | int(11)     | YES  |     | NULL              
updated_at      | timestamp   | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP
org_unit_id     | int(11)     | NO   |     | NULL              
is_virtual      | tinyint(1)  | NO   |     | 0                 
notes           | text        | YES  |     | NULL
以下是我的部分代码:

CREATE PROCEDURE `updatehw`(mac varchar(32), ipaddr varchar(16), typ varchar(16),
                            loc int, name varchar(64), maker varchar(64),
                            model varchar(64), ser_no varchar(64), owner int,
                            assignee int, contact int, admin int, status int,
                            authorized tinyint, authorizer int, auth_date datetime,
                            impact smallint, os_id int, org int, virtual tinyint,
                            seen tinyint, notes text, 
                OUT error int, OUT error_text varchar(64))
   MODIFIES SQL DATA
proc_label: BEGIN
   Declare rc INT;
   Declare hw_mac varchar(32);
   Declare hw_ip varchar(16);
   Declare hw_type varchar(16);
   Declare hw_location_id INT;
   Declare hw_name varchar(64);
   Declare hw_manufacturer varchar(64);
   Declare hw_model varchar(64);
   Declare hw_serial_number varchar(64);
   Declare hw_owner INT;
   Declare hw_assigned_to INT;
   Declare hw_contact INT;
   Declare hw_admin INT;
   Declare hw_status INT;
   Declare hw_authorized tinyint;
   Declare hw_authorized_by INT;
   Declare hw_date_authorized, l_date_authorized DATETIME;
   Declare hw_date_first_seen DATETIME;
   Declare hw_date_last_seen DATETIME;
   Declare hw_impact smallint;
   Declare hw_os_id INT;
   Declare hw_updated_at timestamp;
   Declare hw_org_unit_id INT;
   Declare hw_is_virtual tinyint;
   Declare hw_notes text;

   Declare Exit Handler for SQLEXCEPTION
   BEGIN
      ROLLBACK;
      Set error = -99;
      Set error_text = 'SQL Exception detected';
   End;

   Set error = 0;
   Set error_text = NULL;
   insert into log (msg) values ("updateHW starting"); -- DEBUG

   Start Transaction;
   -- See if the row exists and lock it
   insert into log (msg) values (concat_ws('=','ipaddr',ipaddr)); -- DEBUG
   Select mac, ip, type, location_id, name, manufacturer,
          model, serial_number, owner, assigned_to, contact,
          admin, status, authorized, authorized_by,
          date_authorized, date_first_seen, date_last_seen,
          impact, os_id, org_unit_id, is_virtual, notes
     into hw_mac, hw_ip, hw_type, hw_location_id, hw_name, hw_manufacturer,
          hw_model, hw_serial_number, hw_owner, hw_assigned_to, hw_contact,
          hw_admin, hw_status, hw_authorized, hw_authorized_by,
          hw_date_authorized, hw_date_first_seen, hw_date_last_seen,
          hw_impact, hw_os_id, hw_org_unit_id, hw_is_virtual, hw_notes
     from hardware where ip=ipaddr
     lock in share mode;
   insert into log (msg) values (concat_ws(' ',"hw_owner after select is",hw_owner)); -- DEBUG
   insert into log (msg) values (concat_ws(' ',"hw_authorized_by after select is",hw_authorized_by)); -- DEBUG
   insert into log (msg) values (concat_ws(' ',"hw_authorized after select is",hex(hw_authorized))); -- DEBUG
   insert into log (msg) values (concat_ws(' ',"hw_is_virtual after select is",hex(hw_is_virtual))); -- DEBUG
   Select found_rows() into rc;
   If rc = 0 THEN
      set error = -11;
      set error_text = 'Entry not found';
      ROLLBACK;
      Leave proc_label;
   End if;
.
.
.
CLI显示这是我的行的内容(我使用的是占位符值):

我的日志显示hw_authorized和hw_owner列被读取为NULL,而不是实际值

| 2013-03-25 13:12:23 |    NULL | updateHW starting                  |
| 2013-03-25 13:12:23 |    NULL | ipaddr=ip                          |
| 2013-03-25 13:12:23 |    NULL | hw_owner after select is           |
| 2013-03-25 13:12:23 |    NULL | hw_authorized_by after select is 1 |
| 2013-03-25 13:12:23 |    NULL | hw_authorized after select is      |
| 2013-03-25 13:12:23 |    NULL | hw_is_virtual after select is 1    |
以下是如何创建我的表:

CREATE TABLE hardware (
id int(11) NOT NULL AUTO_INCREMENT,
mac varchar(32) DEFAULT NULL,
ip varchar(16) DEFAULT NULL,
type varchar(16) DEFAULT NULL,
location_id int(11) DEFAULT NULL,
name varchar(64) DEFAULT NULL,
manufacturer varchar(64) DEFAULT NULL,
model varchar(64) DEFAULT NULL,
serial_number varchar(64) DEFAULT NULL,
owner int(11) DEFAULT NULL,
assigned_to int(11) DEFAULT NULL,
contact int(11) DEFAULT NULL,
admin int(11) DEFAULT NULL,
status int(11) DEFAULT NULL,
authorized tinyint(1) NOT NULL DEFAULT 0,
authorized_by int(11) DEFAULT NULL,
date_authorized datetime DEFAULT NULL,
date_first_seen datetime DEFAULT NULL,
date_last_seen datetime DEFAULT NULL,
impact smallint(6) DEFAULT NULL,
os_id int(11) DEFAULT NULL,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
org_unit_id int(11) NOT NULL,
is_virtual tinyint(1) NOT NULL DEFAULT 0,
notes text,
PRIMARY KEY (id),
UNIQUE KEY hardware_idx (mac)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=latin1
以及一份样本插页:

Insert into hardware (mac, ip, type, location_id, name, manufacturer,
                  model, serial_number, owner, assigned_to, contact,
                  admin, status, authorized, authorized_by,
                  date_authorized, date_first_seen,
                  date_last_seen, impact, os_id, org_unit_id, 
                  is_virtual, notes)
          values ('mac','ip','type',1,'name','maker','model','serial',
                  1,1,1,1, 1, 0,
                  1, current_timestamp(), current_timestamp(), current_timestamp(),
                  0, NULL, 1, 1, "Test");
我做错了什么


我正在CentOS 6.4上使用MySql 5.1。

问题已解决。显然,有些秘密保留字不能在存储过程中使用。我将“authorized”列重命名为“is_authorized”,SELECT语句返回了预期值

FTR,我必须更改的列名是:名称、型号、状态、影响和操作系统id


顺便说一句,我尝试在这些列名周围使用记号,但没有任何帮助。

请提供查询以创建表,并提供查询以插入一些示例数据问题,这些问题经过编辑以添加请求的信息。您是否能够成功运行该过程?该过程失败,出现“column cannot null”(列不能为空)错误,因为它试图使用从SELECT错误接收到的空值更新授权列。
Insert into hardware (mac, ip, type, location_id, name, manufacturer,
                  model, serial_number, owner, assigned_to, contact,
                  admin, status, authorized, authorized_by,
                  date_authorized, date_first_seen,
                  date_last_seen, impact, os_id, org_unit_id, 
                  is_virtual, notes)
          values ('mac','ip','type',1,'name','maker','model','serial',
                  1,1,1,1, 1, 0,
                  1, current_timestamp(), current_timestamp(), current_timestamp(),
                  0, NULL, 1, 1, "Test");