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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Trigger not inserting any records !

Author  Topic 

aspardeshi
Starting Member

3 Posts

Posted - 2010-10-18 : 06:19:55
I have the following trigger for automated timetable generation. It is not inserting any records ! Can you tell me what is wrong with this trigger. The record is getting inserted in the table on which this insert trigger is defined but the trigger is not getting processed. Please help me.
ALTER trigger [dbo].[timetableautomatedins] on [dbo].[college_timetableautomated] after insert AS
DECLARE @SWV_NEW_CO VARCHAR(255)
DECLARE @SWV_NEW_COLLEGECD VARCHAR(255)
DECLARE @SWV_NEW_COURSECD VARCHAR(255)
DECLARE @SWV_NEW_CLASSCD VARCHAR(255)
DECLARE @SWV_NEW_DIVISION VARCHAR(255)
DECLARE @SWV_NEW_YEARCD VARCHAR(255)
DECLARE @SWV_NEW_STARTDATE VARCHAR(255)
DECLARE @SWV_NEW_ENDDATE VARCHAR(255)
DECLARE @SWV_NEW_DAY VARCHAR(255)
DECLARE @SWV_NEW_BUILDINGCD VARCHAR(255)
DECLARE @SWV_NEW_FLOORNO VARCHAR(255)
DECLARE @SWV_NEW_ROOMNO VARCHAR(255)
DECLARE @SWV_NEW_WING VARCHAR(255)
DECLARE @SWV_NEW_FROMTIME VARCHAR(255)
DECLARE @SWV_NEW_TOTIME VARCHAR(255)
DECLARE @SWV_NEW_SUBJECTID VARCHAR(255)
DECLARE @SWV_NEW_TEACHERCD VARCHAR(255)
DECLARE @SWV_Cursor_For_NEW CURSOR
SET @SWV_Cursor_For_NEW = CURSOR FOR SELECT co, collegecd, coursecd, classcd, division, yearcd, startdate, enddate, day, buildingcd, floorno, roomno, wing, fromtime, totime, subjectid, teachercd FROM inserted
OPEN @SWV_Cursor_For_NEW
FETCH NEXT FROM @SWV_Cursor_For_NEW INTO @SWV_NEW_CO,@SWV_NEW_COLLEGECD,@SWV_NEW_COURSECD,@SWV_NEW_CLASSCD,@SWV_NEW_DIVISION,
@SWV_NEW_YEARCD,@SWV_NEW_STARTDATE,@SWV_NEW_ENDDATE,@SWV_NEW_DAY,
@SWV_NEW_BUILDINGCD,@SWV_NEW_FLOORNO,@SWV_NEW_ROOMNO,@SWV_NEW_WING,
@SWV_NEW_FROMTIME,@SWV_NEW_TOTIME,@SWV_NEW_SUBJECTID,@SWV_NEW_TEACHERCD
WHILE @@FETCH_STATUS = 0
begin
DECLARE @vholidaydate DATETIME
DECLARE @vstartdate DATETIME
DECLARE @venddate DATETIME
DECLARE @vtimetablestartfrom DATETIME
DECLARE @vtimetableendfrom DATETIME
DECLARE @vtimetableday VARCHAR(50)
DECLARE @c1 CURSOR
DECLARE @c2 CURSOR
SET @c1 = CURSOR FOR select startdate,enddate from college_classmaster where co = @SWV_NEW_CO and
collegecd = @SWV_NEW_COLLEGECD and coursecd = @SWV_NEW_COURSECD and classcd = @SWV_NEW_CLASSCD
and division = @SWV_NEW_DIVISION
open @c1
fetch @c1 into @vstartdate,@venddate
SET @vtimetablestartfrom = @SWV_NEW_STARTDATE
SET @vtimetableendfrom = @SWV_NEW_ENDDATE
SET @vtimetableday = @SWV_NEW_DAY
while 1 = 1
begin
SET @vtimetablestartfrom = CONVERT(DATETIME,@vtimetablestartfrom)+1
while 1 = 1
begin
SET @c2 = CURSOR FOR select docdate from college_holidays where co = @SWV_NEW_CO and collegecd = @SWV_NEW_COLLEGECD
and yearcd = @SWV_NEW_YEARCD and docdate = @vtimetablestartfrom
open @c2
fetch @c2 into @vholidaydate
if @@FETCH_STATUS = 0
SET @vtimetablestartfrom = CONVERT(DATETIME,@vtimetablestartfrom)+1
else
BREAK

close @c2
end
if SUBSTRING(CONVERT(VARCHAR(30),CONVERT(DATETIME,@vtimetablestartfrom),110),1,3) = SUBSTRING(@SWV_NEW_DAY,1,3) and @vtimetablestartfrom between @vstartdate and @venddate
insert into college_timetable(co,collegecd,coursecd,classcd,division,buildingcd,floorno,
roomno,wing,dateofclass,fromtime,totime,yearcd,subjectid,teachercd)
values(@SWV_NEW_CO,@SWV_NEW_COLLEGECD,@SWV_NEW_COURSECD,@SWV_NEW_CLASSCD,@SWV_NEW_DIVISION,@SWV_NEW_BUILDINGCD,@SWV_NEW_FLOORNO,
@SWV_NEW_ROOMNO,@SWV_NEW_WING,@vtimetablestartfrom,@SWV_NEW_FROMTIME,@SWV_NEW_TOTIME,@SWV_NEW_YEARCD,@SWV_NEW_SUBJECTID,@SWV_NEW_TEACHERCD)

if @vtimetablestartfrom > @vtimetableendfrom
BREAK
end
close @c1
FETCH NEXT FROM @SWV_Cursor_For_NEW INTO @SWV_NEW_CO,@SWV_NEW_COLLEGECD,@SWV_NEW_COURSECD,@SWV_NEW_CLASSCD,@SWV_NEW_DIVISION,
@SWV_NEW_YEARCD,@SWV_NEW_STARTDATE,@SWV_NEW_ENDDATE,@SWV_NEW_DAY,
@SWV_NEW_BUILDINGCD,@SWV_NEW_FLOORNO,@SWV_NEW_ROOMNO,@SWV_NEW_WING,
@SWV_NEW_FROMTIME,@SWV_NEW_TOTIME,@SWV_NEW_SUBJECTID,@SWV_NEW_TEACHERCD
end
CLOSE @SWV_Cursor_For_NEW

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-10-18 : 07:34:01
Wow!
THREE cursors in a trigger - unbelievable!

I think nobody is willing to examine what the posted code is doing.

A better way will be to tell us what we need to know about the table structure and what the trigger should do.
Some sample data and wanted result would also be great.
I am sure there is a solution without all that cursor s??t!


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

kunal.mehta
Yak Posting Veteran

83 Posts

Posted - 2010-10-18 : 07:58:49
:)
Go to Top of Page

aspardeshi
Starting Member

3 Posts

Posted - 2010-10-18 : 23:22:20
Hello,

The logic is there are 4 tables columns referred in brackets

1. college_Classmaster (startdate,enddate)
2. college_holidays (date)
3. college_timetable (all columns)
4. college_timetableautomated (all columns) trigger is on this table for insert it is replica of all columns of college_timetable but a added column of day for trigger processing

Logic is when i insert into timetableautomated then i should create a automated timetable in college_timetable for the complete period on the day inserted in college_timetable which is specified for that class. so the timetable should be generated for the class startdate and enddate period only. again in between that class period i want to generate timetable for all the days inserted like for example all Wednesdays at a inserted time. After that i need to compare the generated date with holidays ! if it is not holiday and falling in the inserted day column then timetable is generated.

It is a automated time table generation programme. rest all the columns for insert are self explanatory.

I hope the logic is clear now. Please help me correct this trigger.
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-10-19 : 00:49:07
Can you just post some sample data & the expected output?

Cursors are bad and a cursor with in trigger is EVIL.If you post some sample data & the o/p someone might help you to write a efficient trigger.

PBUH

Go to Top of Page
   

- Advertisement -