Tuesday, March 20, 2012

Question about incrementing a field

Hi everyone. I'm a new user to ASP.NET using VB.NET and SQL Server.
I have one question.
I am currently creating a website for my dissertation whereby whenever acustomer purchases a product, a number is incremented within the'Product' table. The column that needs to be incremented is called'ProductSold'.
I have got an Insert query to store the orders in the 'Orders' tablealready within my webpage. Is there a SQL Query that I could use toincrement the number in the 'Product' Table straight after my InsertQuery?
I really appreciate if anyone could help me in this matter!
you could write a stored proc in which you query in for theMAX(productnumber) and increment it and then do the insert. Why not usean Identity column ?
|||Hi Ndinakar,
Thanks for your reply!
You may think I'm really dumb for saying this. But the Identity Columnwill only increment when a new record is made within the productstable? How can I program the Identity Seed to increment when thatproduct is purchased?
And now going on to the Stored Procedure, I will write code toincrement the number and then write the Insert Statement? What do youmean by the MAX(productnumber)?
Could I write the Insert Statment first and then do an Update Statementto increment the number? Can you do that in an Update statement?
Sorry! I'm just really confused.
|||


You may think I'm really dumb for saying this. But the Identity Columnwill only increment when a new record is made within the productstable? How can I program the Identity Seed to increment when thatproduct is purchased?


I read your post in a hurry. Sorry about that.


Could I write the Insert Statment first and then do an Update Statementto increment the number? Can you do that in an Update statement?


Yes. Write your insert stmt first. then followed by an update. Dont worry about the MAX. You could do something like:
UPDATE
Product
SET
ProductSold = ProductSold + 1 ( or however if more than one order wa placed you could use a variable)
WHERE
Productid = ...

|||Oh I see now!
Thanks for all your help! Much appreciated!

No comments:

Post a Comment