Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hai all,

I want to generate the Product ID when i insert the product in the table.it will be start after 990. i want to maintain the ID in a specific column. Note: I am not mention the Row auto generate ID.

For Example,
ID    Product    ProductID
1     Choclate      990
2     Book           991

The Table is look like this any help...
Posted
Updated 25-May-12 19:32pm
v3

SQL
INSERT INTO myTable (Product, ProductID) VALUES ('Book', (SELECT MAX(ProductId) +1 FROM myTable))

It would be better to create a stored procedure that sets the start value if no records exist, and returns the Product ID.
 
Share this answer
 
Comments
Sam Path 26-May-12 2:18am    
Sir,
I used this query but I got this Error.

Subqueries are not allowed in this context. Only scalar expressions are allowed.
Why don't you change the Structure of your Table as below:
SQL
create TABLE ProductTable
(
ProductID int primary key identity (990,1), --This will automatically Increment your value of ProductID starting from 990. Also Promary Key.
ProductName varchar(100), -- Name of Product stored as Varchar
);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900