Newbie SQL question - SET value=value+1?

Status
Not open for further replies.
I am fairly new at SQL and I'm writing a simple poll style webapp. We have MS-SQL Server 7. Basically I have a table with three columns - ID (int), name (varchar 255), votecount (int). When a user votes for a specific name, I want to increment the votecount column for that name by one. Can I do something like:

UPDATE tablename
SET votecount = votecount + 1 (this is the line I'm unsure of being able to do)
WHERE ID = form.ID

??

And while I'm at it, Any recommendations on a good SQL reference book for beginners?

Thanks tons.
 

E3

Ars Centurion
387
Yes, it works fine.

If u are hard on money, I strongly suggest inhouse sql program help feature. Its very informative. I learnt my basics in school from lecture notes so I can't good any recommendations on books, sorry.

some links
http://www8.silversand.net/techdoc/teachsql/index.htm
http://www.sqlcourse.com/
http://sqlcourse2.com/
http://w3.one.net/~jhoffman/sqltut.htm
http://www.arsdigita.com/books/sql/
 

indigo

Well-known member
5,606
If you're going to do it strictly within SQL though, instead of passing a value from a script, then you'll want to do it in a StoredProcedure, and just call it. This way you lock the table too, so there's no chance of two different "votes" getting the same number.

As far as an SQL book... I started out with the Sam's 21days book myself, but if I had to do it over again, I'd just buy a Wrox reference book or something, learn the Northwind db inside and out, (it's installed by default as one of your databases), and google/visit the forums.

T-SQL is pretty straight-forward... in fact, a reference book isn't really necessary. SQL Books Online should handle all your syntax needs...
 

E3

Ars Centurion
387
indigo: Hi, I m using stored procedures and I was thinking the size of the codes are getting real big esp with
dbCmd.Parameters.Append dbCmd.CreateParameter _

for example when I have several tables,some different no related sql statements to execute, I have to declare a whole list of parameters to be pass in. I could be rather off then using dbconn.execute (sql4).
Its alot faster.

Besides less over overheads techically etc etc and being able to raise errors by returning status, I don't really see anything that makes it more convienient when I prog. Maybe its cuz I don't really understand it.



Just my 2 cents, :confused:
 

indigo

Well-known member
5,606
For the lazy factor, yeah, I build the complicated strings in script and just use .Execute.

For the smaller functions, that I use often, I like SP's though, since I can use them across several pages, and only modify one source. ;)

It all depends on the application though I suppose. With evaluations within SQL itself, you can weed out alot of the arguments you might otherwise use. I can't really think of anything off the top of my head where more than 1 or 2 arguments would be necessary really.

For things like Updates or Inserts, I use my connection object. It makes more sense to pass the SQL server a built statement than to pass a Stored Procedure 15 arguments IMO. It's probably a self-serving philosophy, but it hasn't bitten me yet WRT performance.

I do Deletes with SP's for logging purposes. I try to avoid Deletes whenever possible, as most of the stuff I'm working on use History tables.

Anyways, I'm far from the final word... but it works for me. I'd rather spend as much time as possible in the VS.NET IDE than Enterprise Manager... ;)

BTW, if you aren't already, it's really helpful to build your Stored Procedures in something like TextPad with the SQL definitions, test them in Query Analyzer, and then create them. That's just how I do it though.
 
Thanks for the help and suggestions. It did work out fine. I'm going to look into stored procedures some, but this poll is internal to the company and only going to be up for 3 days. It's a webpage written in ColdFusion. SO, my SQL call is actually coming from the wbpage through CFServer to MS-SQL Server. I know ColdFusion will allow me to use a stored procedure, but I think by the time I figure it out, it won't matter for this program. But when the next one comes around, I'll be ready.
 
Status
Not open for further replies.