Solved

Add 1 to a numeric field in MS SQL Server

Posted on 2007-03-28
15
565 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

809 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