Question:
I have doubt in SQL SERVER 2008,?
prabhu p
2011-11-16 21:42:43 UTC
I have table like this,

Studentid studentname mark1 mark2 mark3 total

I need a stored procedure, that when i am trying to insert a value to studentname, mark1, mark2, mark3 then total mark should be updated automatically based on all three marks.

Kindly help me to short out this problem.....
Four answers:
Lonely Rogue
2011-11-17 09:25:14 UTC
This can be easily acheived at the Table Design phase itself, with no additional programming.



All you have to do is to make the column Total as a Computed Column.

so the table creation syntax goes like this -



CREATE TABLE StudentInfo

(

StudentId INT,

StudentName VARCHAR(10),

Mark1 INT, Mark2 INT,

Mark3 INT, Mark4 INT,

TotalMarks AS (Mark1 +Mark2 +Mark3 +Mark4 )

)



And if you still wish to update this TotalMarks column programatically while inserting the individual marks, then you have to ignore this and have to consider Jason VanOrd's code piece.



-- In 'thoughts'...

Lonely Rogue.
?
2011-11-16 21:55:37 UTC
CREATE PROCEDURE InsertStudent @name AS varchar(50), @m1 AS int, @m2 AS int, @m3 AS int

AS

INSERT INTO Student (StudentName, Mark1, Mark2, Mark3, Total)

VALUES (@name, @m1, @m2, @m3, @m1+@m2+@m3)

GO
hice
2016-11-08 12:22:21 UTC
to respond to in trouble-free; this completely relies upon on what restore variety you've set. as long as you've set the to finish - then in anytime of disaster, only restore the energetic transaction log to recuperate the records till eventually to the point. as well: 10AM and 11AM backup is sweet on condition that your database is corrupted is this style of way that recuperating will grow to be not attainable then you could pass back to the before ideal attainable finished backup with the end results of the records loss (lets say, on your case minimum)
TheMadProfessor
2011-11-17 07:04:46 UTC
Rather than a stored procedure, why not a trigger?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...