I've been referencing and using a stored While Loop for no problems, daily for around 4 weeks. A few days ago it just stopped working.
Ive tried running this with different tables with no help.
DELIMITER $$
CREATE PROCEDURE sp_ukdatatable_route_loop (IN _date date)
BEGIN
SET #first = 1, #last = (
SELECT all_act
FROM ukdatatable_input_ops
WHERE date=_date
);
WHILE(#first <= #last)
DO
INSERT IGNORE INTO ukdatatable_input_daily (date)
VALUES (_date);
SET #first = #first + 1;
END WHILE;
END $$
The loop should lookup an integer from the table ukdatatable_input_ops and then run the loop, creating (new) the number of rows that is equal to the integer returned from the table in another table ukdatatable_input_daily
Is anyone able to help?
Tables involved;
CREATE TABLE `ukdatatable_input_daily` (
`entry_id` int(10) NOT NULL AUTO_INCREMENT,
`date` date DEFAULT NULL,
`aaa` varchar(25) DEFAULT NULL,
`bbb` varchar(50) DEFAULT NULL,
`ccc` varchar(25) DEFAULT NULL,
`ddd` varchar(25) DEFAULT NULL,
`eee` varchar(25) DEFAULT NULL,
`arrival_time` time DEFAULT NULL,
`departure_time` time DEFAULT NULL,
`comments` text,
`status` varchar(10) NOT NULL DEFAULT 'edit',
`day` varchar(10) NOT NULL,
PRIMARY KEY (`entry_id`)
) ENGINE=InnoDB AUTO_INCREMENT=156856 DEFAULT CHARSET=utf8mb4
CREATE TABLE `ukdatatable_input_ops` (
`entry_id` int(10) NOT NULL AUTO_INCREMENT,
`date` date DEFAULT NULL,
`fff` time DEFAULT NULL,
`ggg` int(10) DEFAULT NULL,
`hhh` int(10) DEFAULT NULL,
`iiil` int(10) DEFAULT NULL,
`jjj` int(10) NOT NULL,
`all_act` int(10) DEFAULT NULL,
PRIMARY KEY (`entry_id`),
UNIQUE KEY `date` (`date`)
) ENGINE=InnoDB AUTO_INCREMENT=52 DEFAULT CHARSET=utf8mb4
Related
Need Help with MYSQL Trigger with IF TEXT contains condition
Here's what I'm trying to do on MYSQL:
When there's an update on the table (i,e) when the "status" is changed from 'open' to "closed", i want to update date_closed to current date.
Trigger with if condition {could not get it to work}
create TRIGGER update_close_date BEFORE UPDATE ON issues
FOR EACH ROW
BEGIN
if LOCATE('closed', NEW.status) !=0 THEN
SET new.date_closed = CURRENT_DATE;
end if;
END;
Table Structure
CREATE TABLE `issues` (
`id` int(10) NOT NULL,
`project_type` varchar(100) NOT NULL,
`assessment_name` varchar(150) NOT NULL,
`issue_title` varchar(200) NOT NULL,
`description` varchar(1000) DEFAULT NULL,
`severity` varchar(10) DEFAULT NULL,
`status` varchar(25) DEFAULT NULL,
`found_on` date DEFAULT NULL,
`target_date` date DEFAULT NULL,
`app_support_contact` varchar(100) DEFAULT NULL,
`manager` varchar(100) DEFAULT NULL,
`date_created` datetime DEFAULT NULL,
`date_updated` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
`date_closed` date DEFAULT NULL,
`reminder_count` int(5) DEFAULT 0,
`escalation_count` int(5) DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Update
Thanks for the support, figured out whats wrong, had to add Delimiter word :)
DELIMITER //
create TRIGGER update_close_date BEFORE UPDATE ON issues
FOR EACH ROW
BEGIN
if LOCATE('closed', NEW.status) !=0 THEN
SET new.date_closed = CURRENT_DATE;
end if;
END;//
DELIMITER ;
I have two Table namely admin_user,admin_user_bak
the structure of the table admin_user is
CREATE TABLE IF NOT EXISTS `admin_user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(150) NOT NULL,
`name` varchar(150) NOT NULL,
`emailid` varchar(150) NOT NULL,
`password` varchar(150) NOT NULL,
`roll` varchar(50) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`last_login` datetime NOT NULL,
`status` enum('active','inactive') NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
and structure of table admin_user_bak have all the fields of admin_user and additionally a field bak_user_id . its auto increment id..
CREATE TABLE IF NOT EXISTS `admin_user_bak` (
`bak_user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`user_name` varchar(150) NOT NULL,
`name` varchar(150) NOT NULL,
`emailid` varchar(150) NOT NULL,
`password` varchar(150) NOT NULL,
`roll` varchar(50) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`last_login` datetime NOT NULL,
`status` enum('active','inactive') NOT NULL,
PRIMARY KEY (`bak_user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
here my trigger is
CREATE TRIGGER ins_admin_user BEFORE UPDATE ON admin_user
FOR EACH ROW
BEGIN
INSERT INTO admin_user_bak (user_id,user_name,name,emailid,password,roll,created,modified,last_login,status) VALUES ( NEW.user_id, NEW.user_name, NEW.emailid, new.password, NEW.roll, NEW.created, NEW.modified, NEW.last_login, NEW.status);
END
my purpose is i want to back up all events. insert update delete of a particular record. not all record. i write for insertion. its not working
any idea.. thanks
i want a insertion trigger but you had written update on in your trigger so it's not work.
You use INSERT ON
CREATE TRIGGER ins_admin_user BEFORE INSERT ON admin_user
FOR EACH ROW
BEGIN
INSERT INTO admin_user_bak (user_id,user_name,name,emailid,password,roll,created,modified,last_login,status) VALUES ( old.user_id, old.user_name, old.emailid, old.password, old.roll, old.created, old.modified, old.last_login, old.status);
END
I have 2 tables, students and Math. I want to UPDATE existing data on Math.Last_name when I update the students.Last_Name using the ID I am updating in students.
Students table
CREATE TABLE `STUDENTS` (
`Date_Modified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`LRN` BIGINT(12) NOT NULL AUTO_INCREMENT,
`Last_Name` VARCHAR(50) NOT NULL,
`First_Name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`LRN`)
)COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=123456789112;
MATH TABLE
CREATE TABLE `Math` (
`Date_Modified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`LRN` BIGINT(12) NOT NULL,
`Last_Name` VARCHAR(50) NOT NULL,
`First_Name` VARCHAR(50) NOT NULL,
`Level` VARCHAR(3) NOT NULL,
`UT1` VARCHAR(3) NOT NULL,
`Q1` VARCHAR(50) NULL DEFAULT NULL,
`UT2` VARCHAR(50) NULL DEFAULT NULL,
`Q2` VARCHAR(50) NULL DEFAULT NULL,
`UT3` VARCHAR(50) NULL DEFAULT NULL,
`Q3` VARCHAR(50) NULL DEFAULT NULL,
`UT4` VARCHAR(50) NULL DEFAULT NULL,
`Q4` VARCHAR(50) NULL DEFAULT NULL,
`FINAL GRADE` VARCHAR(50) NULL DEFAULT NULL,
PRIMARY KEY (`LRN`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
ROW_FORMAT=COMPACT;
MY TRIGGER
CREATE TRIGGER `STUDENTS_after_update` AFTER UPDATE ON `STUDENTS` FOR EACH ROW BEGIN
UPDATE Math
SET Last_Name = NEW.Last_Name
WHERE LRN IN (SELECT LRN FROM Math where LRN = NEW.LRN);
END
You do not need a sub select in the update command, since for each row it will pick up the new LRN value and will update all of them in the MATH table where the new.LRN matches in the MATH table. Here how the trigger would look like
delimiter //
create trigger `STUDENTS_after_update` after update on `STUDENTS`
for each row
begin
update Math
set Last_Name = NEW.Last_Name
where LRN = NEW.LRN ;
end;//
delimiter ;
Note that I have added delimiter which is needed when you run them in the Mysql CLI, some other user interface like PHPMyadmin you need to select the delimiter from the user interface.
I have 3 tables:
pp088_project_tasks
CREATE TABLE IF NOT EXISTS `pp088_project_tasks` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`task_list_id` int(10) unsigned DEFAULT NULL,
`text` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
`start_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`due_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`assigned_to_company_id` int(10) DEFAULT NULL,
`assigned_to_user_id` int(10) unsigned DEFAULT NULL,
`completed_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`completed_by_id` int(10) unsigned DEFAULT NULL,
`created_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by_id` int(10) unsigned DEFAULT NULL,
`updated_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_by_id` int(10) unsigned DEFAULT NULL,
`order` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `task_list_id` (`task_list_id`),
KEY `completed_on` (`completed_on`),
KEY `created_on` (`created_on`),
KEY `order` (`order`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=76 ;
pp088_project_task_lists
CREATE TABLE IF NOT EXISTS `pp088_project_task_lists` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`milestone_id` int(10) unsigned NOT NULL DEFAULT '0',
`project_id` int(10) unsigned DEFAULT NULL,
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`priority` int(3) unsigned NOT NULL DEFAULT '0',
`description` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
`start_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`due_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`score` int(3) unsigned NOT NULL DEFAULT '0',
`is_private` tinyint(1) unsigned NOT NULL DEFAULT '0',
`completed_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`completed_by_id` int(10) unsigned DEFAULT NULL,
`created_on` datetime DEFAULT NULL,
`created_by_id` int(10) unsigned NOT NULL DEFAULT '0',
`updated_on` datetime DEFAULT NULL,
`updated_by_id` int(10) unsigned NOT NULL DEFAULT '0',
`order` tinyint(3) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `milestone_id` (`milestone_id`),
KEY `project_id` (`project_id`),
KEY `completed_on` (`completed_on`),
KEY `created_on` (`created_on`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
gantt_tasks
CREATE TABLE IF NOT EXISTS `gantt_tasks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`text` varchar(255) NOT NULL,
`start_date` date NOT NULL,
`duration` int(11) NOT NULL,
`progress` float NOT NULL DEFAULT '1',
`sortorder` int(11) NOT NULL,
`parent` int(11) NOT NULL,
`projectID` int(10) unsigned DEFAULT NULL,
`tipo_evento` varchar(100) NOT NULL,
`idPier` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=48 ;
I created an AFTER INSERT trigger on the 'pp088_project_tasks' table to insert into 'gantt_tasks'
CREATE TRIGGER `insert_gantt_task` AFTER INSERT ON `pp088_project_tasks`
FOR EACH ROW BEGIN
declare project_id int;
declare duracion int;
set project_id=(select project_id from pp088_project_task_lists where id=NEW.task_list_id);
set duracion=(select DATEDIFF(NEW.due_date,NEW.start_date));
insert into gantt_tasks (text,start_date,duration,projectID,idPier) values (NEW.text,NEW.start_date,duracion,project_id,NEW.id);
END
but the problem is with the setting of the project_id variable which sets the gantt_tasks field projectID to NULL because that query cannot retrieve the value using NEW.task_list_id, even if I use that value directly on the insert just to check the value it sets the gantt_tasks field projectID to 0......The strange thing is that if I use other values of the NEW they all work, it's just that field, the insert is successful for all of the other fields on gantt_tasks. I checked and the types are the same, and so the collation as well.
The first two tables are from a project manager called projectPier and the gantt_tasks table is one I created for a gantt chart(DHTMLX gantt chart).
... but the problem is with the setting of the project_id variable which sets the gantt_tasks field projectID to NULL because that query cannot retrieve the value using NEW.task_list_id, even if I use that value directly on the insert just to check the value it sets the gantt_tasks field projectID to 0
Reason is very much visible.
You have a variable project_id whose name matches that of a column in the table.
And when you use column name without a table qualifier, there arises a conflict on the names. And priority will be given to the local variable over column.
In your case, due to the statement
declare project_id int;
the default value of project_id would be zero. And hence, the output of the statement
set project_id=(select project_id from pp088_project_task_lists
where id=NEW.task_list_id);
would be a zero. And hence the same is used in the insert statement.
Options:
Change local variable name to something relevant.
Use table name to qualify the column name.
Sample Example: (using option 1)
Make following changes to your code and it should be working.
delimiter //
CREATE TRIGGER `insert_gantt_task` AFTER INSERT ON `pp088_project_tasks`
FOR EACH ROW BEGIN
declare _project_id int default 0;
declare _duracion int default 0;
select project_id into _project_id
from pp088_project_task_lists
where id = NEW.task_list_id;
select DATEDIFF( NEW.due_date, NEW.start_date ) into _duracion;
insert into gantt_tasks ( text, start_date, duration, projectID, idPier )
values( NEW.text, NEW.start_date, _duracion, _project_id, NEW.id );
END;
//
delimiter ;
Sample Example: (using option 2)
Make following changes to your code and it should be working.
delimiter //
CREATE TRIGGER `insert_gantt_task` AFTER INSERT ON `pp088_project_tasks`
FOR EACH ROW BEGIN
declare project_id int default 0;
declare duracion int default 0;
set project_id := ( select tl.project_id
from pp088_project_task_lists tl
where tl.id = NEW.task_list_id );
set duracion := select DATEDIFF( NEW.due_date, NEW.start_date );
insert into gantt_tasks ( text, start_date, duration, projectID, idPier )
values( NEW.text, NEW.start_date, duracion, project_id, NEW.id );
END;
//
delimiter ;
I am working on magento platform.I face a problem regarding values insertion to specific field: My query run perfect but one specific column not working for any query.I try my best but didn't find why .When i change the column type from int to varchar type it works.This is my table structure.
CREATE TABLE `followupemails_emaillogs` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`schedule_time` datetime DEFAULT NULL,
`sent_time` datetime DEFAULT NULL,
`email_status` varchar(100) DEFAULT NULL,
`client_name` varchar(250) DEFAULT NULL,
`client_email` varchar(250) DEFAULT NULL,
`followupemails_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1.
the "followupemails_id" column not working in insert and update query.This is one update query where record exist that id(29). UPDATE followupemails_emaillogs SET followupemails_id=5 WHERE id =29.
This is insertion query INSERT INTO followupemails_emaillogs SET followupemails_id=4, schedule_time='2013-10-23 08:10:00', email_status='pending', client_name='ayaz ali'.this works fine on fiddle but not on my sqlyog ? what could be the issue.At last i find query that work perfect
.INSERT INTO followupemails_emaillogs (followupemails_id,schedule_time,email_status,client_name,client_email) VALUES (26,'2013-10-23 08:10:00','pending','ayaz ali','mamhmood#yahoo.com');
Can anyone tell me why set query not working but second query works perfect.so that i can accept his answer.Thanks for all your help
Try like this
To Create,
CREATE TABLE followupemails_emaillogs (
id int(8) NOT NULL AUTO_INCREMENT PRIMARY KEY,
schedule_time datetime DEFAULT NULL,
sent_time datetime DEFAULT NULL,
email_status varchar(100) DEFAULT NULL,
client_name varchar(250) DEFAULT NULL,
client_email varchar(250) DEFAULT NULL,
followupemails_i int(11) DEFAULT NULL,
UNIQUE (id)
)
To Insert,
INSERT INTO followupemails_emaillogs (schedule_time,sent_time,email_status,client_name,client_email,followupemails_i)
VALUES
('2012-05-05','2012-05-06',"sent","sagar","sagar#xxxx.com",2)
the whole query is ok
CREATE TABLE `followupemails_emaillogs` (
`id` int NOT NULL AUTO_INCREMENT,
`schedule_time` datetime DEFAULT NULL,
`sent_time` datetime DEFAULT NULL,
`email_status` varchar(100) DEFAULT NULL,
`client_name` varchar(250) DEFAULT NULL,
`client_email` varchar(250) DEFAULT NULL,
`followupemails_id` int DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1.
but at the last there is dot which is actually error so remove the dot and create the table
latin1.
so remove the dot sign and not null in id filed use this line by default fields are null so don't use default null
id int (8) AUTO_INCREMENT
CREATE TABLE `followupemails_emaillogs` (
`id` int (8) AUTO_INCREMENT,
`schedule_time` datetime DEFAULT NULL,
`sent_time` datetime DEFAULT NULL,
`email_status` varchar(100),
`client_name` varchar(250),
`client_email` varchar(250),
`followupemails_id` int,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1
don't need (11) in the sql query for int operator , This get for length of the nvarchar,varchar datatype column only not a int datatype,So change and write int instead of int(11) and int(8)
Try this query instead of your query
CREATE TABLE `followupemails_emaillogs` (
`id` int NOT NULL AUTO_INCREMENT,
`schedule_time` datetime DEFAULT NULL,
`sent_time` datetime DEFAULT NULL,
`email_status` varchar(100) DEFAULT NULL,
`client_name` varchar(250) DEFAULT NULL,
`client_email` varchar(250) DEFAULT NULL,
`followupemails_id` int DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1.