I had asked a question here on how to get a sql function(max) result into a @variable and how to use it. The answer I got was very straightforward and useful(tanx to the responder: Tom Mac). No I am using the same mechanism in my program and I get an unacceptable result that I’ll explain here:
Please Have a look at my mysql console and everything is self explanatory. Even my question 🙂 :
> mysql> select max(command_idx) into @max_command_idx from command;
> Query OK, 1 row affected (0.00 sec)
Now use the variable for the first time:
mysql> insert into command(controller_idx,subcontroller_idx,command_idx,controller_id, subcontroller_id,code,status,type,plan_name,timetable_id,offset,ctime,user_name,result) values
(0,0,@max_command_idx+1,'937','SUB0','SMS',0,'CONFIG','NA','NA',0,NOW(),'admin',0);
Query OK, 1 row affected (0.00 sec)
Now use the variable for the second time for another table:
> mysql> insert into > csmslist(controller_idx,subcontroller_idx,command_idx,csmslist_idx,phone) > values(0,0,@max_command_idx+1,0,'+60127929022');ERROR 1048 (23000): > Column 'command_idx' cannot be null
……………………………………………………………..
My QUESTION:
would you kindly tell me why I get this error?
THANK YOU
……………………………………………………………..
more information on my tables:
mysql> desc csmslist;
+-------------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+-------------+------+-----+---------+-------+
| controller_idx | int(11) | NO | PRI | NULL | |
| subcontroller_idx | int(11) | NO | PRI | NULL | |
| command_idx | int(11) | NO | PRI | NULL | |
| csmslist_idx | int(11) | NO | PRI | NULL | |
| phone | varchar(24) | YES | | NULL | |
+-------------------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
mysql> desc command;
+-------------------+-------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+-------------+------+-----+-------------------+-----------------------------+
| controller_idx | int(11) | NO | PRI | NULL | |
| subcontroller_idx | int(11) | NO | PRI | NULL | |
| command_idx | int(11) | NO | PRI | NULL | auto_increment |
| controller_id | varchar(24) | YES | | NULL | |
| subcontroller_id | varchar(24) | YES | | NULL | |
| code | varchar(12) | YES | | NULL | |
| status | int(11) | YES | | NULL | |
| type | varchar(24) | YES | | NULL | |
| plan_name | varchar(24) | YES | | NULL | |
| timetable_id | varchar(24) | YES | | NULL | |
| offset | int(11) | YES | | NULL | |
| ctime | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| user_name | varchar(80) | YES | | NULL | |
| result | int(11) | YES | | NULL | |
+-------------------+-------------+------+-----+-------------------+-----------------------------+
14 rows in set (0.01 sec)
The command table has an auto_increment, when you set the column to null (which is what you are doing, despite what you think you are doing), it just does an auto increment on it.
The csmslist table has no auto increment so inserting null fails.
Do this instead:
And in the insert to
commandjust make the column NULL and let the database do its thing for you automatically.It could be the command table is empty, so it’s returning NULL for the max.
BTW instead of using a variable you can put that query right in there: