Below is my table book.
ID BookName StoreId Sequence Price
1 ABC 1 1 450
2 DEF 1 2 450
3 GHF 2 1 300
So above table is book table, Each store will have multiple books. I am writing a function which will insert book record in this table. While inserting the new record our requirement is new book sequence should Max(Sequence) + 1
of corresponding store. I have written below query for that.
INSERT INTO Book(BookName,StoreId,Sequence,Price) Values
(@name,@sid,(SELECT MAX(Sequence) + 1 from book where StoreId=@StoreId),@price)
This query is working fine when we already have inserted storeId
present in table but as soon as i give storeId
not present table Sequence will come as NULL
because we don't have any existing sequence for that store and Max(sequence)+1
will give Null
. What is best way to resolve this?