MySQL Forums
Forum List  »  MySQL Query Browser

Strange Error while Adding dynamic columns in table with given date
Posted by: jamil Ahmad
Date: July 09, 2010 09:27AM

Hello MySql Gurus,

I have a stored procedure which creates a temp table and in that same temp table i am creating the no columns based on the month days.
it works correctly but i have one problem when i entered Date '20100708' it creates correct columns. but again if i entered the date '20100201'
it returns error "Unknown column livestat.D29 in filed list.
its driving me crazy. Any idea??

CREATE DEFINER=`root`@`localhost` PROCEDURE `spr_Test`(SelectedDate varchar(10))
BEGIN
DECLARE NoOfDaysInMonth INT DEFAULT 0;
DECLARE Counter INT DEFAULT 1;
DECLARE ColumnNmame VARCHAR(6) DEFAULT '';

SELECT DAYOFMONTH(LAST_DAY(SelectedDate)) INTO NoOfDaysInMonth;

DROP TABLE IF EXISTS livestat;
CREATE TABLE livestat AS SELECT * from tblcountry;

WHILE Counter <= NoOfDaysInMonth DO
SET ColumnNmame=CONCAT('D',Counter);
SET Counter = Counter + 1;
SET @sqlStr=CONCAT('ALTER TABLE livestat ADD COLUMN '"",ColumnNmame,""' INT DEFAULT 0;');
PREPARE stmt FROM @sqlStr;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END WHILE;


Select * from livestat;

END

Options: ReplyQuote


Subject
Written By
Posted
Strange Error while Adding dynamic columns in table with given date
July 09, 2010 09:27AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.