Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Add 1 to a numeric field in MS SQL Server

Posted on 2007-03-28
15
Medium Priority
?
588 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 

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 200 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

715 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