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.
CSS
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 :-
SQL
Alterprocedure ProcTestGet
@temp_namevarchar(50),
@sumintASDeclare@Tmp_idAsintDeclare@total_sumAsintDeclare@sum1Asintselect@Tmp_id= @@identity
If @Tmp_id=0Beginset@total_sum=Nullinsert into TblTest(temp_name,sum,total_sum) VALUES(@temp_name,@sum,@total_sum)
Endelsebeginselect@total_sum= total_sum from TblTest where temp_id = @@identity
if @total_sum=NULLbeginselect@sum1= sum from TblTest where temp_id = @@identitydeclare@totalsumintset@totalsum=@sum1+@suminsert into TblTest(temp_name,sum,total_sum) VALUES(@temp_name,@sum,@totalsum)
endelsebeginselect@total_sum= total_sum from TblTest where temp_id = @@identitydeclare@totalsum1intset@totalsum1=@total_sum+@suminsert into TblTest(temp_name,sum,total_sum) VALUES(@temp_name,@sum,@totalsum1)
endselect @@identityEnd
exec ProcTestGet '1',600 -- Execeute Stored Procedure select * from TblTest -- Run query to see result
1 comment
1 Comment
Join the conversation! Your thoughts help the community grow.
Join to leave a comment
Members earn 10 SHARP for a comment, 10 SHARP for a reaction, and 250 SHARP for every blog they publish.
Not calculating/ inserting @total_sum value into the records while adding...I think @@Identity giving results always 0 only.. Can you please update me the same.
Thanks In Advance..
Lakshmi Reddy YeluriPosted Aug 27, 2012, 10:53 PM EDT
Not calculating/ inserting @total_sum value into the records while adding...I think @@Identity giving results always 0 only.. Can you please update me the same. Thanks In Advance..