Solved

Add 1 to a numeric field in MS SQL Server

Posted on 2007-03-28
15
550 Views
Last Modified: 2012-08-13
I'm trying too add the number 1 to a numeric field in MS SQL Server with asp.

Like this:

<!--#include file="../sqlcon/db.asp" -->
<%
      newID = Request("newBlogID")

      strSQL="Select * From blog where blogID="+newID+""
      Set objRS = Server.CreateObject("ADODB.Recordset")
      objRS.Open strSQL, objCON, 3, 3
      
      IF NOT objRS.EOF THEN
      
      objRS("blogRead") = objRS("blogRead") + 1

      objRS.update
      objRS.Close

      ELSE
      
      objRS.Close
      objCON.close
      Set objCON = Nothing      

      END IF      
      
%>
0
Comment
Question by:oksum73
  • 5
  • 4
  • 3
  • +2
15 Comments
 
LVL 17

Expert Comment

by:xDJR1875
ID: 18810915
Why not just change your sql code to

"Update blog wet blogID = blogID + 1 Where blogID = "+newID+""

If it exists, it gets updated.
0
 
LVL 17

Expert Comment

by:xDJR1875
ID: 18810917
oops change wet to set ;-)
0
 
LVL 11

Expert Comment

by:flipz
ID: 18810921
What error are you getting?
0
 

Author Comment

by:oksum73
ID: 18811004
like this?           PS! : It doesen't work

<!--#include file="../sqlcon/db.asp" -->
<%
      newID = Request("newBlogID")

      strSQL="Update blog set blogRead = blogRead + 1 Where blogID = "+newID+""
      
      strSQL.execute
%>
0
 
LVL 17

Expert Comment

by:xDJR1875
ID: 18811039
What error are you receiving? And what is the structure of table blog.
0
 
LVL 11

Accepted Solution

by:
flipz earned 50 total points
ID: 18811051
I believe you need to open the recordset using adOpenKeyset instead of adOpenStatic - try this:

newID = Request("newBlogID")

      strSQL="Select * From blog where blogID="+newID+""
      Set objRS = Server.CreateObject("ADODB.Recordset")
      objRS.Open strSQL, objCON, 1, 3
     
      IF NOT objRS.EOF THEN
     
      objRS("blogRead") = CLng(objRS("blogRead")) + 1

      objRS.update
      end if
     
      objRS.Close
      objCON.close
      Set objCON = Nothing      

   
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18811073
<!--#include file="../sqlcon/db.asp" -->
<%
      newID = Request("newBlogID")

      strSQL="Update blog set blogRead = blogRead + 1 Where blogID = "+newID+""
     
      objCON.execute strSQL
%>
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

Author Comment

by:oksum73
ID: 18811165
Both don't work

blogID datatype  =  int

blogRead = numeric

0
 
LVL 11

Expert Comment

by:flipz
ID: 18811197
ASP syntax?

strSQL="Select * From blog where blogID=" & newID

OR

strSQL="Update blog set blogRead = blogRead + 1 Where blogID = " & newID

if using xDJR1875/angelIII suggestion.
0
 

Author Comment

by:oksum73
ID: 18811245
None of the solutions work
0
 
LVL 11

Expert Comment

by:flipz
ID: 18811271
Any error messages to help us along??
0
 

Author Comment

by:oksum73
ID: 18811323
I use flash as front end so you can't see any error messages.

This used to work some time ago:

objRS("blogRead") = objRS("blogRead")+1

but not any more

This works:

objRS("blogRead") = 2

But I need to add 1 each time someone clicks the blogg post
0
 
LVL 25

Expert Comment

by:kevp75
ID: 18811398
<!--#include file="../sqlcon/db.asp" -->
<%
      newID = Request("newBlogID")

      strSQL="UDATE blog SET blogRead = blogRead + 1 where blogID=" &  newID & ""
      objCON.Execute strSQL      
      objCON.close
      Set objCON = Nothing      
     
%>


this ass-u-me's that you are opening objCON in "../sqlcon/db.asp"
0
 
LVL 25

Expert Comment

by:kevp75
ID: 18811745
I really hate to be a stickler, but that really is a very inept way of doing it......but, to each his/her own I guess...
0
 

Author Comment

by:oksum73
ID: 18812003
Hei kevp 75, your solution work too, so I'm going with that one, is it not wise to use Recordset so much?
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now