Solved

Add 1 to a numeric field in MS SQL Server

Posted on 2007-03-28
15
573 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:Daniel Reynolds
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:Daniel Reynolds
ID: 18810917
oops change wet to set ;-)
0
 
LVL 11

Expert Comment

by:flipz
ID: 18810921
What error are you getting?
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: 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:Daniel Reynolds
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 143

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
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

713 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