question

Sacky avatar image
Sacky asked

UPDATE table with difference

Hi all, I have a table that captures printer page counters,the system scans at a given time and every time it does this it feeds the database with the information. Now what i want is, as it does the update of the page count, it should first get the difference between the previous reading and the current reading and the difference is what it should show. How can i achieve this in a tsql statement? Thanks in advance Best regards Sacky
t-sqlupdate
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

WilliamD avatar image WilliamD commented ·
One further question, what version of SQL server are you using? 2000,2005,2008, Although with the syntax used I'm guessing not even SQL server.
1 Like 1 ·
WilliamD avatar image WilliamD commented ·
@Sacky, this is possible, buy maybe could supply us with little more detail: Create table script, test data, expected output.
0 Likes 0 ·
Sacky avatar image Sacky commented ·
THANKS FOR THE QUICK REPLY WILLIAM HERE IS THE CREATE TABLE SRCIPT CREATE TABLE "devices" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "serial_number" varchar(75), "updated_on" datetime, "page_count" integer) At update UPDATE devices SET page_count= "this value comes from the printer at scanning" where ip_address="the ip address of the printer" now what i want, before it updates it should first do the difference calculations and its the difference that should be shown on the page_count column
0 Likes 0 ·

1 Answer

·
WilliamD avatar image
WilliamD answered
You can assign and use the value stored in the same column. Something like: SET page_count=page_count+@valuefromprinter this would read the value, add the value in the variable, then update the column.
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sacky avatar image Sacky commented ·
@ William, its actually sqlite, but i play around with your suggestion and what i will get out of it..Thanx loads
0 Likes 0 ·
WilliamD avatar image WilliamD commented ·
@Sacky, even with SQLite I would think it will work.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.