Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6788169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:27:11+00:00 2026-05-26T17:27:11+00:00

I had asked a question here on how to get a sql function(max) result

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:27:12+00:00Added an answer on May 26, 2026 at 5:27 pm

    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:

    insert into csmslist(controller_idx,subcontroller_idx,command_idx,csmslist_idx,phone)
    values(0,0,LAST_INSERT_ID(),0,'+60127929022');ERROR 1048 (23000):
    Column 'command_idx' cannot be null
    

    And in the insert to command just 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:

    insert into csmslist(controller_idx,subcontroller_idx,command_idx,csmslist_idx,phone)
    values(0,0,(select max(command_idx) from command),0,'+60127929022');ERROR 1048 (23000):
    Column 'command_idx' cannot be null
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had asked this question Adding more attributes to LINQ to SQL entity Now,
This question may have been asked before, but I had trouble finding an answer,
I recently asked a question (and had it answered) here: jQuery Load JSON I
I had asked a question about this earlier, but it didn't get answered right
I had asked this question on Javaranch , but couldn't get a response there.
I had a problem with the Django tutorial so I asked a question here.
i had asked help on this question here Static member reclaiming memory and recovering
I had the same question as asked here: New git repository in root directory
I had asked a question on how to implement real time updates in ASP.NET
This is a link to a question I had asked two days back, How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.