Chirag Fisher 0 Newbie Poster

Its urgent please help!!!!!

animaldetails(Table 1 )
Animal_Id int (primary key)
IsMilking boolean
Milking_Capacity varchar

milking (Table 2)
Animal_Id int (primary key)
Month_Year varcahr (eg. '6 2009' -- Means June 2009)
Day1 varchar
Day2 varchar
and so on (till 31)


milkingevent(Table 3)
milking int (primary key)


Requirements:

1. I need a trigger for table3 Before update.
2. Loop 1
3. In table 1 it will search for every row for attribute IsMilking if it is true it will store value of Milking_Capacity in an variable1 as well as well as Animal_Id in another variable2.
4. Now it should go to table 2 search for Animal_Id from variable2. and Match with current month and year
5. a. If not found INSERT statement "Insert into table2..." values (variable2, current(month and Year), if todays date is 1 then value of variable1 in Day1 or whatever, reset all should be default value)
6. b. if found Update statement "Update into table2 ..." (where if current date is 28 then set value in Day 28 reset should be as it is.....)
7. After it gets completed
8. Go to loop 1 and search for Second animal ID and the process continues.

================================================================================================

CREATE TRIGGER TBU_Table3 After Update ON milkingevent
FOR EACH ROW BEGIN 
DECLARE @_lAnimal_Idint INTEGER DEFAULT 0;
DECLARE @_lIsMilking INTEGER DEFAULT 0;
DECLARE @_lMilking_Capacity VARCHAR(50) DEFAULT "";
DECLARE @_lMonth_Year VARCHAR(50) DEFAULT "";
DECLARE @_lCount INTEGER DEFAULT 0;
DECLARE @_lAnimal_Id INTEGER DEFAULT 0;

DECLARE Table1_CUR CURSOR FOR SELECT Milk_Capacity,Animal_Id FROM animaldetails WHERE IsMilking= 'Yes';
OPEN Table1_CUR;
FETCH Table1_CUR INTO @_lMilking_Capacity,@_lAnimal_Idint;

	BEGIN
            SET @_lMonth_Year= SELECT  CONCAT(MONTH(current_date), ' ',YEAR(current_date));
            SET @_lCount = Select COUNT(*) FROM milking WHERE Animal_Id=@_lAnimal_Idint AND MonthYear=@_lMonth_Year;
            IF @_lCount>0
		BEGIN
			IF DAY(current_date)=26
			BEGIN
				Update milking
				SET Day26=DAY(current_date)
			END
		END
            ELSE
		BEGIN
                        IF DAY(current_date)=26
                        BEGIN
                            INSERT INTO milking (Animal_Id,MonthYear, Day26) VALUES (@_lAnimal_Idint, @_lMonth_Year,@_lMilking_Capacity)
                        END
                        ELSE
                        BEGIN
                             INSERT INTO milking (Animal_Id,MonthYear) VALUES (@_lAnimal_Idint, @_lMonth_Year)
                        END
		END
	END
            FETCH Table1_CUR INTO @_lMilking_Capacity,@_lAnimal_Idint
END
CLOSE Table1_CUR;

================================================
Error displayed are ::::::::::::::::::::::
Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@_lAnimal_Idint INTEGER DEFAULT 0' at line 3
(0 ms taken)

Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE @_lIsMilking INTEGER DEFAULT 0' at line 1
(0 ms taken)

Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE @_lMilking_Capacity VARCHAR(50) DEFAULT ""' at line 1
(0 ms taken)

Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE @_lMonth_Year VARCHAR(50) DEFAULT ""' at line 1
(0 ms taken)

Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE @_lCount INTEGER DEFAULT 0' at line 1
(0 ms taken)

Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE @_lAnimal_Id INTEGER DEFAULT 0' at line 1
(0 ms taken)

Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE Table1_CUR CURSOR FOR SELECT Milk_Capacity,Animal_Id FROM animaldetails ' at line 1
(0 ms taken)

Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OPEN Table1_CUR' at line 1
(0 ms taken)

Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FETCH Table1_CUR INTO @_lMilking_Capacity,@_lAnimal_Idint' at line 1
(0 ms taken)

Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET @_lMonth_Year= SELECT CONCAT(MONTH(current_date), ' ',YEAR(current_date))' at line 2
(0 ms taken)

Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Select COUNT(*) FROM milking WHERE Animal_Id=@_lAnimal_Idint AND MonthYear=@_lMo' at line 1
(0 ms taken)

Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF @_lCount>0
BEGIN
IF DAY(current_date)=26
BEGIN
Update milking
' at line 1
(0 ms taken)