473,418 Members | 2,043 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Is there a way to do an addition withing a storeed procedure?

Hi,

I am trying to write a stored procedure that does some simple math. All I want to do is
get a views number with a select and then increment +1, and then do an update with the new value.

Here is my stored procedure so far. looks like it's something simple I am missing
Expand|Select|Wrap|Line Numbers
  1. /****** Object:  StoredProcedure [dbo].[UpdateAquariumViews]    Script Date: 10/17/2010 23:51:05 ******/
  2. SET ANSI_NULLS ON
  3. GO
  4. SET QUOTED_IDENTIFIER ON
  5. GO
  6. ALTER Procedure [dbo].[UpdateAquariumViews]( @AquariumsID int )
  7. AS
  8.  
  9. Declare @AquariumViews int
  10.  
  11. /* problem seems to be here, not getting @AquariumViews value  */
  12. Select *,@AquariumViews from Aquariums WHERE (AquariumsID = @AquariumsID )
  13.  
  14.  
  15. SET @AquariumViews = @AquariumViews + 1
  16.  
  17.  
  18. Update Aquariums SET AquariumViews = @AquariumViews WHERE (AquariumsID = @AquariumsID )
  19.  
any help would be greatly appreciated
Oct 18 '10 #1
4 1166
OK,

figured this out. But now I can't figure out the syntax for the "if then" statement

here is the updated code

Expand|Select|Wrap|Line Numbers
  1. SET ANSI_NULLS ON
  2. GO
  3. SET QUOTED_IDENTIFIER ON
  4. GO
  5. ALTER Procedure [dbo].[UpdateAquariumViews]( @AquariumsID int )
  6. AS
  7.  
  8. Declare @AquariumViews int
  9.  
  10. /*Select *,@AquariumViews output from Aquariums WHERE (AquariumsID = @AquariumsID ) */
  11. SET @AquariumViews = (Select AquariumViews from Aquariums WHERE (AquariumsID = @AquariumsID))
  12.  
  13.  
  14. IF (@AquariumViews IS NULL)
  15.     SET @AquariumViews = 0
  16. end if
  17.  
  18. SET @AquariumViews = @AquariumViews + 1
  19.  
  20.  
  21. Update Aquariums SET AquariumViews = @AquariumViews WHERE (AquariumsID = @AquariumsID )
  22.  
  23.  
Oct 18 '10 #2
all figured out

here is the code

Expand|Select|Wrap|Line Numbers
  1. /****** Object:  StoredProcedure [dbo].[UpdateAquariumViews]    Script Date: 10/17/2010 23:51:05 ******/
  2. SET ANSI_NULLS ON
  3. GO
  4. SET QUOTED_IDENTIFIER ON
  5. GO
  6. ALTER Procedure [dbo].[UpdateAquariumViews]( @AquariumsID int )
  7. AS
  8.  
  9. Declare @AquariumViews int
  10.  
  11. SET @AquariumViews = (Select AquariumViews from Aquariums WHERE (AquariumsID = @AquariumsID))
  12.  
  13. IF (@AquariumViews IS NULL) 
  14.     Begin
  15.         SET @AquariumViews = 0
  16.     End
  17.  
  18. SET @AquariumViews = @AquariumViews + 1
  19.  
  20.  
  21. Update Aquariums SET AquariumViews = @AquariumViews WHERE (AquariumsID = @AquariumsID )
  22.  
Oct 18 '10 #3
gpl
152 100+
You could do something like this
Expand|Select|Wrap|Line Numbers
  1. SET @AquariumViews = ISNULL(Select AquariumViews from Aquariums WHERE (AquariumsID = @AquariumsID),0) 
  2.  
Which removes the need for the IF
Oct 18 '10 #4
ck9663
2,878 Expert 2GB
Can't you just make AquariumViews column an IDENTITY? You might have problems with concurrency.

Happy Coding!!!

~~ CK
Oct 18 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Bill Kellaway | last post by:
Hi there - this should be fairly simple for someone. Basically I can't figure out how to pass the parameters from ASP to a Stored Procedure on SQL. Here's my code: I just need to help in...
0
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
3
by: Oldhandandy | last post by:
I have a SQL stored procedure that I need to run by pressing a button on a .Net form. I've created a SQLConnection, the form and a button, what code do I need to run the procedure.
4
by: Chris Auer | last post by:
What I am looking to do is use a complicated stored procedure to get data for me while in another stored procedure. Its like a view, but a view you can't pass parameters to. In essence I would...
3
by: Mike | last post by:
I have a Form that is set to visible=false while a save confirm Form is displayed. When the confirmation is complete the confirm Form is closed and the hidden form is made visible=true. ...
3
by: Dj | last post by:
If you passed all of the fields as one group level when you rejester the SP you would only be able to use one CHAR field. Now here is the rub. If you are passing mix data, why not send it in the...
5
by: Nesa | last post by:
I have a stored procedure that wraps a moderately complex query over 5, 6 related tables. The performance of the procedure is unacceptably slow as it takes on average 5-10 min to complete. To...
2
by: sridhar21 | last post by:
hi to all i am sridhar i need a simple example for all type of function in stored procedure thanxs
3
by: mgsmario | last post by:
Hi Guys I have an ASP application that connects to an Oracle database, right now I'm trying to connect this same ASP application to a DB2 database (Express-C, version 9.5.0). I'm in the process...
0
by: mgsmario | last post by:
Hi Guys I have an ASP application that connects to an Oracle database, right now I'm trying to connect this same ASP application to a DB2 database (Express-C, version 9.5.0). I'm in the process...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.