Solved

Add 1 to a numeric field in MS SQL Server

Posted on 2007-03-28
15
534 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

705 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

20 Experts available now in Live!

Get 1:1 Help Now