Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
Search :       Advanced Search »
Home » Blogs Home » Blog Detail

How to Get sum of first record amount and second amount is in Total Amount using Sql server STored Procedure

 by Dhaval Patel on Jan 18, 2012

Stored procedure using first sum and second sum total is goes to second id total_sum value.
Comments: 0 Views: 490 Printable Version 
Get sum of first record amount and second amount is in  Total Amount
In this table i want to insert value fot sum of first and second temp_id is total_sum of second temp_id and than after repeat loop for it.
This is Table Script.

 USE [temp]
GO
/****** Object: Table [dbo].[TblTest] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[TblTest](
[temp_id] [int] IDENTITY(1,1) NOT NULL,
[temp_name] [varchar](50) NULL,
[sum] [int] NULL,
[Total_sum] [int] NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF

Stored Procedure :-

Alter procedure ProcTestGet
@temp_name varchar(50),
@sum int
AS
Declare @Tmp_id As int
Declare @total_sum As int
Declare @sum1 As int
select @Tmp_id = @@identity
If @Tmp_id = 0
Begin
set @total_sum =Null
insert into TblTest(temp_name,sum,total_sum) VALUES(@temp_name,@sum,@total_sum)
End
else
begin
select @total_sum = total_sum from TblTest where temp_id = @@identity
if @total_sum = NULL
begin
select @sum1= sum from TblTest where temp_id = @@identity
declare @totalsum int
set @totalsum = @sum1 + @sum
insert into TblTest(temp_name,sum,total_sum) VALUES(@temp_name,@sum,@totalsum)
end
else
begin
select @total_sum = total_sum from TblTest where temp_id = @@identity
declare @totalsum1 int
set @totalsum1 = @total_sum + @sum
insert into TblTest(temp_name,sum,total_sum) VALUES(@temp_name,@sum,@totalsum1)
end
select @@identity
End

 exec ProcTestGet  '1',600    -- Execeute Stored Procedure
 select * from TblTest           -- Run query to see result
Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
 
What do you say about this post? Post a comment here
*Title:
*Comment:
 
Comments not available.
Nevron Gauge for SharePoint
Become a Sponsor

 Blogger's Profile
Age: Not Available
Location:
Title: Developer
Joined: Mar 26, 2010
Education: Bachelors Degree
 More Blogs from this Blogger
No record available
 Latest Blogs
[Video] OnClose Handler
[Video] Storing and Loading the Window and Toolbar position
The Euclidean Algorithm
Swapping Exe Process
How Exe file is Generated by VS2005 C++ Project?
What is Exe
Header files: Multiple Inclusion problem - Solution B
Header files: Multiple Inclusion problem - Solution A
Header files: Multiple Inclusion problem - Reason
Header files: Multiple Inclusion problem
View all »
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.