Quote:
|
Originally Posted by alvincks
select case when LAST_INSERT_ID() = " " then '10001' else LAST_INSERT_ID()+1 end as running_num from tbl_invoicehdr limit 1
|
When you specify the table, you will get an empty result set since you select rows from an empty table. Even if you specify a function (in this case, LAST_INSERT_ID()) there are no rows, so you will never get any result set. You probably meant to do
select case when LAST_INSERT_ID() IS NULL then '10001' else LAST_INSERT_ID()+1
but since it will never be NULL (but 0) if you have not inserted any rows in the current transaction, you can skip the case statement. Are you sure you aren't looking for the information returned by
SELECT CASE WHEN MAX(id) IS NULL THEN '10001' ELSE MAX(id)+1 END AS running_num FROM tbl_invoicehdr
?
The latest query returns the highest id number plus one. However, if you delete the number with the highest id number, you will not get the next auto_increment value that will be used. On the other hand, you should probably not use the primary key in the way you think you want to!