Author |
Topic |
daipayan
Posting Yak Master
181 Posts |
Posted - 2009-02-15 : 11:10:24
|
I want to create a table name: assign_marks and one of the column in the table will be marks.Now I want that whatever value user insert in marks column, the column total should not exceed 100.For e.g.MARKS40302010value insert will be success as total is 100.MARKS40303010value insert will not be success as total is 110 not 100. I want to store this code in the database, I don't want to run the code. I just want user should insert the value in marks column.Hope I can make you understand, what exactly is my problem??Daipayan |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-02-15 : 13:25:38
|
[code]CREATE TABLE Marks ( UserID INT NOT NULL, Marks TINYINT NOT NULL )GOCREATE TRIGGER dbo.trgMarks_UpdateInsertON db.MarksAFTER UPDATE, INSERTASIF EXISTS ( SELECT TOP 1 m.UserID FROM dbo.Marks AS m INNER JOIN ( SELECT UserID FROM inserted GROUP BY UserID ) AS i ON i.UserID = m.UserID GROUP BY m.UserID HAVING SUM(CAST(m.Marks AS SMALLINT)) > 100 ) BEGIN RAISERROR('Sum is greater than 100.', 16, 1) ROLLBACK TRAN ENDGOINSERT dbo.Marks ( UserID, Marks )SELECT 1, 40 UNION ALLSELECT 1, 30 UNION ALLSELECT 2, 80 UNION ALLSELECT 1, 30 UNION ALLSELECT 1, 10GOINSERT dbo.Marks ( UserID, Marks )SELECT 2, 30GO[/code] E 12°55'05.63"N 56°04'39.26" |
|
|
daipayan
Posting Yak Master
181 Posts |
Posted - 2009-02-15 : 15:44:15
|
@PesoSir, I exceuted the code, first I created the table and when am trying to create the trigger, am getting the following error:Server: Msg 156, Level 15, State 1, Procedure trgMarks_UpdateInsert, Line 14Incorrect syntax near the keyword 'WHERE'. Daipayan |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-15 : 23:49:53
|
make a udf like thiscreate function TotalMarks()returns int asbegindeclare @total intselect @total=sum(Marks)from yourtablereturn @totalendthen link it to table via check constraint asalter table yourtable add constraint chk_totalmarks check (dbo.TotalMarks()< 100)now try inserting marks |
|
|
daipayan
Posting Yak Master
181 Posts |
Posted - 2009-02-16 : 04:52:09
|
@visakh16I had created following table:TABLE: marks_assigncourse_code(varchar,50)marks_type(varchar,50)marks_assign(bigint,8) I had little bit modified your function, it as follows:CREATE function TotalMarks()returns int asbegindeclare @total intselect @total=sum(marks_assign)from marks_assigngroup by course_codereturn @totalend Hope this is ok, as I want the total 100 should be calculated according to a particular course.Daipayan |
|
|
daipayan
Posting Yak Master
181 Posts |
Posted - 2009-02-16 : 05:44:39
|
@PesoSir, I used your edited code, but this time, it's giving following error:Server: Msg 208, Level 16, State 4, Procedure trgMarks_UpdateInsert, Line 1Invalid object name 'db.Marks'. Daipayan |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-16 : 06:16:38
|
quote: Originally posted by daipayan @visakh16I had created following table:TABLE: marks_assigncourse_code(varchar,50)marks_type(varchar,50)marks_assign(bigint,8) I had little bit modified your function, it as follows:CREATE function TotalMarks()returns int asbegindeclare @total intselect @total=sum(marks_assign)from marks_assigngroup by course_codereturn @totalend Hope this is ok, as I want the total 100 should be calculated according to a particular course.Daipayan
yup...thats fine. if you want total based on course, you need to group by it |
|
|
daipayan
Posting Yak Master
181 Posts |
Posted - 2009-02-16 : 08:37:28
|
@visakh16This code showing server error, can't I generate self-created error message??and Sir, I had created a school database in the following link, please go through it once and give any suggestions, this is the following link:[url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=119218[/url]Daipayan |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-16 : 09:09:54
|
quote: Originally posted by daipayan @visakh16This code showing server error, can't I generate self-created error message??and Sir, I had created a school database in the following link, please go through it once and give any suggestions, this is the following link:[url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=119218[/url]Daipayan
you need to use RAISERROR for genrating self created error.see belowhttp://www.sommarskog.se/error-handling-I.html |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-02-16 : 09:39:48
|
[code]CREATE TABLE dbo.Marks ( UserID INT NOT NULL, Marks TINYINT NOT NULL )GOCREATE TRIGGER dbo.trgMarks_UpdateInsertON dbo.MarksAFTER UPDATE, INSERTASIF EXISTS ( SELECT TOP 1 m.UserID FROM dbo.Marks AS m INNER JOIN ( SELECT UserID FROM inserted GROUP BY UserID ) AS i ON i.UserID = m.UserID GROUP BY m.UserID HAVING SUM(CAST(m.Marks AS SMALLINT)) > 100 ) BEGIN RAISERROR('Sum is greater than 100.', 16, 1) ROLLBACK TRAN ENDGOINSERT dbo.Marks ( UserID, Marks )SELECT 1, 40 UNION ALLSELECT 1, 30 UNION ALLSELECT 2, 80 UNION ALLSELECT 1, 30 UNION ALLSELECT 1, 10GOINSERT dbo.Marks ( UserID, Marks )SELECT 2, 30GODROP TABLE dbo.MarksGO[/code] E 12°55'05.63"N 56°04'39.26" |
|
|
daipayan
Posting Yak Master
181 Posts |
Posted - 2009-02-16 : 13:32:43
|
THANKS A LOT AGAINDaipayan |
|
|
|