Inserting record in table with generating new id for primary key field using single query

November 12, 2007 at 10:21 am | In SQL Server | Leave a Comment

Query to Insert new record in table with generating new id in key column
If no records exist then new id is 1 else new id is greater then 1 of maximum id.

Example :

Inserting new product in product_master : -

Note : Assuming that productid is of type int.

Insert into product_master (productid,product_name,created_date)
select (select isnull(max(productid)+1,1) from product_master),’CPU’,getdate()

Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.