473,323 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Simply updating a count in a SQL DB

Hi guys,

I have a simple field in a table in my sql server DB. All i need to do
is update a count on it, from 5 to 6, from 6 to 7, so on. A simple counter.

Do I have to SELECT the count field once, get the value, do the adding
in my program, then do another command to update the count field? Or
does SQL syntax allow a simple increment function?

Thanks!

Buck

Jul 20 '05 #1
2 13547
I'm not sure what you mean by "update a count".

To increment a value in a column:

UPDATE Sometable
SET col1 = col1+1

If you want to assign unique a count to a row, you can do this on INSERT:

INSERT INTO Sometable (col1, col2, ...
SELECT COALESCE(MAX(col1),0)+1, ...
FROM Sometable

or on UPDATE, assuming you have a key to define the sequence:

UPDATE Sometable
SET col1 =
(SELECT COUNT(*)
FROM Sometable AS S
WHERE keycol <= Sometable.keycol)

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #2
This answered my question. Thanks! I didnt know a simple addition could
be formed in the sql statement.


UPDATE Sometable
SET col1 = col1+1


Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Jason | last post by:
Let's say I have an html form with 20 or 30 fields in it. The form submits the fields via POST to a php page which updates a table in a database with the $_POST vars. Which makes more sense? ...
2
by: Pete | last post by:
There is a Summary/Example further down... On page one of my site I have a form with some checkboxes and detailed descriptions. When the form is submitted (to page two), the values of the...
3
by: Daniel Pryde | last post by:
Hi there. I hope this isn't a stupid question to ask, but does anyone know how to print out a string without moving to a new line each time and simply updating the first line. An example would be,...
7
by: MgGuigg | last post by:
Hello all, This is my first time posting a question to this forum, so here is hoping I am following protocol. I am scraping the rust off my old Basic programming skills, and have just recently...
7
by: Ludwig | last post by:
Hi, Current situation: a number of C++ applications installed on multiple computers, some of them are dependent of each other. Updates are installed manually. Question: automate these...
0
by: Michael Kellogg | last post by:
I have a problem wherein a query that updates a GridView doesn't seem to really stay in sync with a label I have above the GridView. I have a GridView object that I'm updating with information...
5
by: jslssuze | last post by:
I created a database with registration information and my query works well on simply the count of people registered. Instead of count, however, my clients wants number of spaces available (i.e....
26
by: Ping | last post by:
Hi, I'm wondering if it is useful to extend the count() method of a list to accept a callable object? What it does should be quite intuitive: count the number of items that the callable returns...
5
sueb
by: sueb | last post by:
I'm trying to write a report that counts the number of times a certain field is set to True (or Yes), but all I can get is simply the count of how many records fall in that particular group. Here is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.