I have some trouble with MySql Procedure.
I have:
DROP TABLE IF EXISTS `employees2`;
CREATE TABLE `employees2` (
`LastName` varchar(20) character set utf8 collate utf8_unicode_ci NOT NULL default '',
`FirstName` varchar(10) character set utf8 collate utf8_unicode_ci NOT NULL default ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
It work's and is OK.
And MySQL Procedure:
DROP PROCEDURE IF EXISTS gen;
DELIMITER $$
CREATE PROCEDURE gen()
BEGIN
DECLARE e1 TEXT;
DECLARE e2 TEXT;
DECLARE e3 TEXT;
SET e1 = "Davolio";
SET e2 = "Nancy";
SET e3 = "Ron , Deplin";
insert into `employees2`(`LastName`,`FirstName`) values ('Nonew','adams');
insert into `employees2`(`LastName`,`FirstName`) values (e1,e2);
insert into `employees2`(`LastName`,`FirstName`) values (e3);
END $$
DELIMITER ;
call gen();
I would like to instert into table values from variable e3. "Ron" is for column LastName and "Deplin" is for column FirstName. But i got error: "Error Code: 1136. Column count doesn't match value count at row 1" First and second inserts works fine. How to force the third insert to work ?
You're only specifying one value with two columns named in your last insert, hence the unequal row count.
See this post on how to use MySQL to split a string like that got two value entries:
https://stackoverflow.com/a/9953163/2812842
Related
I have created a stored procedure with one argument. I have made a trigger to call stored procedure. Trigger calls stored procedure on change of a column value a table(device_session1).If is_active is updated to 'No' then trigger calls stored procedure. I pass that column value in stored procedure and procedure prints it but its giving error when i update column value.
Table-
CREATE TABLE `device_session1` (
`id` varchar(75) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
`is_active` varchar(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci DEFAULT NULL,
PRIMARY KEY (`id`));
insert into device_session1(id,is_active) values ('11','YES');
Trigger-
Delimiter $$
create trigger after_device_session_update
after update on device_session1
For Each Row
BEGIN
IF (NEW.is_active="NO") THEN
SET #session_id = new.id;
call new_procedure1(#session_id);
END IF;
END;
Delimiter;
Stored Procedure-
CREATE PROCEDURE `new_procedure1`(IN id VARCHAR(50))
BEGIN
select concat('id : ',id);
END
I need to automatically update the value of AdoptionDate every time AdopterID receives a value different from NULL. I tried to do this using the code below, but it gives me Error Code: 1362.
DROP SCHEMA IF EXISTS `Assignment`;
CREATE SCHEMA `Assignment` DEFAULT CHARACTER SET utf8;
USE `Assignment`;
DROP TABLE IF EXISTS `Dog`;
CREATE TABLE `Dog` (
`DogID` INT NOT NULL AUTO_INCREMENT,
`Race` VARCHAR(32) NOT NULL,
`RescueDate` DATE NOT NULL,
`AdoptionDate` DATE DEFAULT NULL,
`AdopterID` INT(11) DEFAULT NULL,
PRIMARY KEY (`DogID`)
);
DELIMITER //
CREATE TRIGGER CustomTrigger AFTER UPDATE ON `Dog`
FOR EACH ROW
BEGIN
IF NEW.`AdopterID` <=> NULL THEN
SET NEW.`AdoptionDate` = NOW();
END IF;
END; //
DELIMITER ;
Getting a Operand should contain 1 column(s) mysql error whenever I try to insert into the table sets.
I googled and found a hoard of similar questions but they are always pin point specific to solving their immediate problem. I have mysql 5.6 by the way. I am allowed multiple TIMESTAMPS.
Here is my code:
INSERT INTO `sets` (`tabler_name`) VALUES ("leads_auto");
Here is my table:
CREATE TABLE IF NOT EXISTS `lms`.`sets` (
`set_id` BIGINT NOT NULL AUTO_INCREMENT,
`on_off` SMALLINT NOT NULL DEFAULT 0,
`tabler_name` VARCHAR(45) NULL,
`origin_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_modified_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`original_count` INT NULL,
`current_count` INT NULL,
`source_type` VARCHAR(45) NULL,
`source` VARCHAR(45) NULL,
`method` VARCHAR(45) NULL,
`agent` VARCHAR(45) NULL,
`dupes` INT NULL,
`bads` INT NULL,
`aged` INT NULL COMMENT 'This table keeps track of the record sets that enter the system. Example: a set of leads imported into the database.',
PRIMARY KEY (`set_id`)
) ENGINE = InnoDB;
Stored Procedure:
DELIMITER //
CREATE PROCEDURE `lms`.`leads_to_bak` ()
BEGIN
SET #table1 = (SELECT `tabler_name` FROM sets WHERE on_off=0 LIMIT 1);
SET #table2 = CONCAT(#table1, '_bak');
SET #SQL1 = CONCAT('INSERT INTO ',#table2, '(', (SELECT
REPLACE(GROUP_CONCAT(COLUMN_NAME), 'lead_id,', '') FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = #table2), ')', ' SELECT ', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), 'lead_id,', '') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = #table1), ' FROM ', #table1);
PREPARE stmt FROM #sql1;
EXECUTE stmt;
END//
DELIMITER ;
USE `lms`;
Trigger
DELIMITER $$
USE `lms`$$
CREATE TRIGGER `lms`.`after_insert_into_leads`
AFTER INSERT ON `sets` FOR EACH ROW
BEGIN
IF (SELECT * FROM sets WHERE on_off=0 LIMIT 1) THEN
CALL lms.leads_to_bak();
END IF;
END$$
DELIMITER ;
USE `lms`;
I don't see anything wrong with my routines. Removing the routines and trigger seems to make the problem go away.
In your trigger, did you mean to put EXISTS after IF? Like this:
CREATE TRIGGER `lms`.`after_insert_into_leads`
AFTER INSERT ON `sets` FOR EACH ROW
BEGIN
IF EXISTS (SELECT * FROM sets WHERE on_off=0 LIMIT 1) THEN
CALL lms.leads_to_bak();
END IF;
END$$
Escape your table name, it seems to be a reserved function. I'm not sure if you've defined one locally.
INSERT INTO `sets` (tabler_name) VALUES ("leads_auto");
Also, you can't have two timestamp fields in a single database afaik. Change one of the two timestamps to a DATETIME field if it caused you issues as well
Besides escaping the field name in your INSERT-statement, it cannot be improved very much. But it doesn't generate any error in my test enviroment. Is this really the exact statement throwing you an error?
However, there's a slight problem in your table definition, it will throw you an
Incorrect table definition; there can be only one TIMESTAMP column
with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause.
As the error message indicates, you can only use one timestamp column with CURRENT_TIMESTAMP, if you need more than one, you can do this using a trigger.
delimeter //
DROP function IF EXISTS get_seq_next//
create function get_seq_next(
IN sequence_ref_ varchar(30)
) returns int(11) unsigned
BEGIN
DECLARE seq_val_ int(11) unsigned;
LOCK TABLE ga_seq_tab WRITE;
select sequence_no into seq_val_ from ga_seq_tab where sequence_ref=sequence_ref_;
if not seq_val_ is null then
update ga_seq_tab set sequence_no=sequence_no+1 where sequence_ref=sequence_ref_;
end if
UNLOCK TABLES;
return seq_val;
END //
DELIMETER ;
I'm trying to create a function but it keeps saying I have syntax errors and I am not sure what is wrong with it
Try removing the IN reserved word in the parameters list.
I'm tumbled with a problem!
I've set up my first check constraint using MySQL, but unfortunately I'm having a problem. When inserting a row that should fail the test, the row is inserted anyway.
The structure:
CREATE TABLE user (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
uname VARCHAR(10) NOT NULL,
fname VARCHAR(50) NOT NULL,
lname VARCHAR(50) NOT NULL,
mail VARCHAR(50) NOT NULL,
PRIMARY KEY (id),
CHECK (LENGTH(fname) > 30)
);
The insert statement:
INSERT INTO user VALUES (null, 'user', 'Fname', 'Lname', 'mail#me.now');
The length of the string in the fname column should be too short, but it's inserted anyway.
I'm pretty sure I'm missing something basic here.
MySQL doesn't enforce CHECK constraints, on any engine.
Which leads me to ask, why would you declare the fname column as VARCHAR(50), but want to enforce that it can only be 30 characters long?
That said, the only alternative is to use a trigger:
CREATE TRIGGER t1 BEFORE INSERT ON user
FOR EACH ROW BEGIN
DECLARE numLength INT;
SET numLength = (SELECT LENGTH(NEW.fname));
IF (numLength > 30) THEN
SET NEW.col = 1/0;
END IF;
END;
As mentioned above you have to use a trigger, MySQL doesn't support check, also when you have multiple statements inside your trigger block, like declaring variables or control flows, you need to start it with begin and end and enclose your trigger inside two delimiters:
Note: If you use MariaDB use // after the first delimiter and before the second delimiter, otherwise if you use MySQL use $$ instead.
delimiter //
create trigger `user_insert_trigger` before insert on `user` for each row
begin
declare maximumFnameLength int unsigned;
declare fNameLength int unsigned;
set maximumFnameLength = 30;
set fNameLength = (select length(new.fNameLength));
if (fNameLength > maximumFnameLength) then
signal sqlstate '45000'
set message_text = 'First name is more than 30 characters long.';
end if;
end
//
delimiter ;