0

I need to insert new record in table. The first column name ob_no is int, not null. So I need generate number which is maximum ob_no at the moment +1. How can I do it? Something Like (max(ob_no) + 1) but it doesn't work in SQL 2005. Thanks for any ideas.

INSERT INTO et_thanks_2014 (ob_no, c_name)
VALUES (???, 'Some Text')
2
  • Use an Identity column. CREATE TABLE et_thanks_2014 ( ob_no int IDENTITY(1,1), c_name varchar (20) ); Mar 26, 2015 at 13:06
  • if you want to use ob_no as a key, you should make it the primary key in your table schema and set it up with auto incrementing.
    – mituw16
    Mar 26, 2015 at 13:06

1 Answer 1

0

You should use identities if you don't need values without lag:

INSERT INTO et_thanks_2014 (ob_no, c_name)
SELECT MAX(ob_no) + 1, 'Some Text'
FROM et_thanks_2014

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.