Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
columbo1977
Starting Member
4 Posts |
Posted - 2013-10-20 : 19:17:29
|
Hi Therefor a uni tutorial I am trying to add 10 marks to all module 1 marks but fail if the mark is over 100..... haveing trouble as it has been a long time since I did SQL and I am very very rusty.Can anyone give me any clues, I am supposed to look up IF, @@ROWCOUNT and RAISERROR but I have been searching for examples etc for ages and I just dont know what to do and this is stopping me moving forward now.What I have so far is :-- ------------------------------------------------------------------ Tutorial 4 - q1 Use AssessmentGobegin tran errorbegin try If moduleCode = 1 and mark < 100 Then Select 'BEFORE', moduleCode,mark From AssessSchema.Result where moduleCode = 1 begin tran MarksUpdateBy10 Update AssessSchema.Result set mark = mark+10 where moduleCode = 1 commit tranend trybegin catch rollback tranend catchElse Goto End:EndEnd Ifselect 'AFTER' , *from AssessSchema.ResultGo CheersGraham |
|
columbo1977
Starting Member
4 Posts |
Posted - 2013-10-21 : 02:16:41
|
Oh, forgot to say. I have to update the mark of module 1 by 10 but it needs to fail if the mark goes above 100.G |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-21 : 06:41:22
|
something like this Update AssessSchema.Resultset mark = mark+10where moduleCode = 1AND mark+10 <= 100 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
columbo1977
Starting Member
4 Posts |
Posted - 2013-10-21 : 16:14:31
|
I already got something like that, and it worked. They want us to use the @@rowcount, IF and RAISERRORG |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-22 : 08:09:57
|
quote: Originally posted by columbo1977 I already got something like that, and it worked. They want us to use the @@rowcount, IF and RAISERRORG
do you mean this?DECLARE @ErrNo int,@Cnt intUpdate AssessSchema.Resultset mark = mark+10where moduleCode = 1AND mark+10 <= 100SELECT @Cnt=@@ROWCOUNT,@ErrNo=@@ERROR IF @ErrNo > 0 BEGIN PRINT 'your error message' RETURN (@Err) END ELSE RETURN (@Cnt) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
columbo1977
Starting Member
4 Posts |
Posted - 2013-10-22 : 08:41:41
|
Thanks for the assisstance so far :)Is RAISERROR a seperate command as that needs to be in there too.I'm new (again) to this, why is @Cnt in there ?G |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-22 : 09:05:18
|
quote: Originally posted by columbo1977 Thanks for the assisstance so far :)Is RAISERROR a seperate command as that needs to be in there too.I'm new (again) to this, why is @Cnt in there ?G
RAISERROR is required in case you want to raise the error from sql code to calling application directlyIn method I showed above you just capture errorid as return value and do eroor notification at application end------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|