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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Checking Error

Author  Topic 

daipayan
Posting Yak Master

181 Posts

Posted - 2009-02-28 : 14:12:57
I have a Triiger, that checking that marks_assign in table dbo.marks_assign should br less than the marks of table student_marks:
CREATE TRIGGER	dbo.Check_Marks
ON dbo.student_marks
AFTER UPDATE,
INSERT
AS
IF EXISTS (
SELECT TOP 1 m.course_code, m.marks_type
FROM dbo.marks_assign AS m
INNER JOIN (
SELECT course_code, marks_type
FROM inserted
) AS i ON i.course_code = m.course_code
AND i.marks_type = m.marks_type
Group By m.course_code, m.marks_type
HAVING (CAST(i.marks AS SMALLINT)) > (CAST(m.marks_assign AS SMALLINT))
)
BEGIN
RAISERROR('Marks Entered is greater than Assigned Marks', 16, 1)
ROLLBACK TRAN
END

My two tables are:
-------------    ------------
student_marks marks_assign
------------- ------------
studentMarks_ID marksAssign_ID
student_ID
course_code----->course_code
marks_type------>marks_type
marks marks_assign
------------- ------------

Am getting following error, while excuting the trigger:
Server: Msg 207, Level 16, State 3, Procedure Check_Marks, Line 6
Invalid column name 'marks'.


Daipayan

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2009-02-28 : 16:13:52
here is your problem:

...(CAST(i.marks AS SMALLINT))...

you should specify or call this field in select list within inner join, otherwise it is not recognised.

INNER JOIN (
SELECT course_code
,marks_type
,marks -- THIS LINE IS ADDED
FROM inserted
) AS i ON i.course_code = m.course_code

Go to Top of Page

daipayan
Posting Yak Master

181 Posts

Posted - 2009-03-01 : 12:08:28
After updating the code, I got the following error...
Server: Msg 8121, Level 16, State 1, Procedure Check_Marks, Line 6
Column 'i.marks' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8121, Level 16, State 1, Procedure Check_Marks, Line 6
Column 'm.marks_assign' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.


Daipayan
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-03-01 : 17:20:21
quote:
Originally posted by daipayan

I have a Triiger, that checking that marks_assign in table dbo.marks_assign should br less than the marks of table student_marks:
CREATE TRIGGER	dbo.Check_Marks
ON dbo.student_marks
AFTER UPDATE,
INSERT
AS
IF EXISTS (
SELECT TOP 1 m.course_code, m.marks_type,i.marks,m.marks_assign
FROM dbo.marks_assign AS m
INNER JOIN (
SELECT course_code, marks_type,marks
FROM inserted
) AS i ON i.course_code = m.course_code
AND i.marks_type = m.marks_type
Group By m.course_code, m.marks_type,i.marks,m.marks_assign
HAVING (CAST(i.marks AS SMALLINT)) > (CAST(m.marks_assign AS SMALLINT))
)
BEGIN
RAISERROR('Marks Entered is greater than Assigned Marks', 16, 1)
ROLLBACK TRAN
END

My two tables are:
-------------    ------------
student_marks marks_assign
------------- ------------
studentMarks_ID marksAssign_ID
student_ID
course_code----->course_code
marks_type------>marks_type
marks marks_assign
------------- ------------

Am getting following error, while excuting the trigger:
Server: Msg 207, Level 16, State 3, Procedure Check_Marks, Line 6
Invalid column name 'marks'.


Daipayan

Go to Top of Page

daipayan
Posting Yak Master

181 Posts

Posted - 2009-03-02 : 09:00:48
Thank You Sodeep......it's now working...I understand the error, THANKS A LOT!

Daipayan
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2009-03-02 : 11:21:17
oh, yes..i've overlooked that :)
great.
Go to Top of Page

daipayan
Posting Yak Master

181 Posts

Posted - 2009-03-02 : 11:26:51
But Thanks BOTH of you to help me on this issue...thanks a lot!!

Daipayan
Go to Top of Page
   

- Advertisement -