the target table is not updatable (update view) - mysql

hello i want to update a view using this procedure but i got the error below:
Procedure:
CREATE DEFINER=`root`#`localhost` PROCEDURE `setijfchauffeur`()
begin
declare cnt int;
declare c int;
declare cn int;
declare nomvol varchar(11);
declare i int;
declare rest int;
/*create a table with ID and ARR_num*/
create table ijfnbrvols select distinct ARR_num from ijforder ;
alter table ijfnbrvols add column ID int primary key auto_increment after ARR_num;
select count(*) into cnt from ijfnbrvols ;
set c = 1;
while (c<= cnt) do
select ARR_num into nomvol from ijfnbrvols where ID = c;
select count(*) into cn from ijforder where ARR_num = nomvol;
if (cn <= 3) and (cnt !=0) THEN
update ijforder set Chauffeur ='berline' where ARR_num = nomvol;
END IF;
if (cnt <= 8) and (cnt >=4) THEN
update ijforder set Chauffeur ='minibus' where ARR_num = nomvol;
END IF;
set c= c+1;
End while;
END
i got this error: Error Code: 1288. The target table ijforder of the UPDATE is not updatable
Help please :) thanks

Related

MySQL Procedure variable receiving null on count(*)

Why when I do Select count(*) From table1 I receive 300 but if I do SELECT end = COUNT(*) FROM table1; returns null
Here is the fiddle example https://dbfiddle.uk/ZHzoaztV
code snippet:
CREATE TABLE table1(
start int NOT NULL,
id int PRIMARY KEY AUTO_INCREMENT,
counter int NOT NULL,
difference int NOT NULL,
end int NOT NULL
);
CREATE PROCEDURE doWhile()
BEGIN
DECLARE i INT DEFAULT 1;
DECLARE start INT DEFAULT 120;
DECLARE counter INT DEFAULT 1;
DECLARE end INT DEFAULT 300;
WHILE (i <= end) DO
INSERT INTO table1 VALUES (start,null,counter,start+counter,end);
SET i = i+1;
SET counter = counter+1;
END WHILE;
END;
CALL doWhile();
SELECT * FROM table1;
CREATE PROCEDURE insertMore()
BEGIN
DECLARE start INT;
DECLARE counter INT DEFAULT 1;
DECLARE end INT;
SELECT end = COUNT(*) FROM table1;
SELECT start = MAX(id)+1 FROM table1;
-- SELECT COUNT(*) FROM table1;
WHILE (counter <= end) DO
INSERT INTO table1 VALUES (start,null,counter,start+counter,end);
SET counter = counter+1;
END WHILE;
END;
CALL insertMore();
SELECT * FROM table1;
I expected to return 300, so hopefully my function should do it right
You have a problem with start and end Variable
Can you try this :
CREATE PROCEDURE insertMore()
BEGIN
DECLARE start INT;
DECLARE counter INT DEFAULT 1;
DECLARE end INT;
SELECT COUNT(*) into end FROM table1;
SELECT max(id)+1 into start FROM table1;
-- SELECT COUNT(*) FROM table1;
WHILE (counter <= end) DO
INSERT INTO table1 VALUES (start,null,counter,start+counter,end);
SET counter = counter+1;
END WHILE;
END;
Try it here : https://dbfiddle.uk/X6vP3wKW

Missing values percentage in MYSQL

I want to find the missing values percentage for each column in table name passed in missing_data_perc function parameter.
Delimiter $$
CREATE PROCEDURE missing_data_perc(IN tableName VARCHAR(30))
BEGIN
DECLARE i int default 0;
DECLARE n int default 0;
DECLARE columnName VARCHAR(30);
DECLARE perc int;
SET #tname = tableName;
SET #perc = 0;
DROP TEMPORARY TABLE IF EXISTS missing_data;
Create TEMPORARY TABLE missing_data (
data_name VARCHAR(50),
missing_data_percentage int
);
SELECT count(*) from information_schema.columns where table_name = tableName into n;
SET i=0;
while i < n Do
-- Select column_name from information_schema.columns where table_name = tableName limit i,1 into columnName;
-- Set #Q3 = CONCAT('SELECT 1-count(*)/count(columnName) from ? into ?');
-- PREPARE Stmt3 from #Q3;
-- EXECUTE Stmt3 using #tname, #perc;
-- SET perc = #perc;
SELECT 1-count(*)/count(columnName) from tableName into perc;
INSERT into missing_data VALUES (columnName, perc);
SET i = i+1;
End While;
SELECT * from missing_data;
END; $$
Delimiter ;
CALL missing_data_perc("deliveries");
Below line is giving me error:
SELECT 1-count(*)/count(columnName) from tableName into perc;
But if i change it to below statement then i works,
SELECT 1-count(*)/count(columnName) from deliveries into perc;

How to deal with missing rows in my stored procedure loop?

I have this procedure which loops through article values, fetch tag1 and insert it into article_tag table:
DELIMITER $$
CREATE PROCEDURE dt1()
BEGIN
DECLARE maxid INT;
DECLARE x INT;
DECLARE t VARCHAR(30);
DECLARE ntag1 int;
SET maxid = (SELECT MAX(id) FROM `article`);
SET x = (SELECT MIN(id) FROM `article`) ;
WHILE x<= maxid DO
SET t = (SELECT tag1 from `article` WHERE id=x);
SET ntag1 = (SELECT count(*) from `article_tag` WHERE tag=t);
IF ntag1 = 0
THEN
INSERT INTO `article_tag` (tag, slug, frequency) VALUES (t, t, 1);
ELSE
UPDATE `article_tag` SET frequency = frequency + 1 WHERE tag=t;
END IF;
SET x = x + 1;
END WHILE;
END$$
This works fine when there are rows with id in the while loop, but when when there are some missing ids in between (like here)
I get
Query Error: Error: ER_BAD_NULL_ERROR: Column 'tag' cannot be null
I'm wondering what is the idiomatic way to deal with such missing rows?
You can use loop with cursor, so you will only loop through existing records and do not need to check for NULL.
Something like this:
DELIMITER \\
CREATE PROCEDURE dt1()
BEGIN
DECLARE t VARCHAR(30);
DECLARE done INT DEFAULT FALSE;
DECLARE cur1 CURSOR FOR SELECT tag FROM `article` ORDER BY id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
loop1: LOOP
FETCH cur1 INTO t;
IF done THEN
LEAVE loop1;
END IF;
SELECT COUNT(*) INTO #cnt from `article_tag` WHERE tag = t;
IF #cnt = 0 THEN
INSERT INTO `article_tag` (tag, slug, frequency) VALUES (t, t, 1);
ELSE
UPDATE `article_tag` SET frequency = frequency + 1 WHERE tag = t;
END IF;
END LOOP;
CLOSE cur1;
END
\\
DELIMITER ;
See manual for details:
https://dev.mysql.com/doc/refman/5.7/en/cursors.html
Sure, if possible I'd use INSERT ON DUPLICATE UPDATE as already mentioned.

MySQL Stored Procedure inserts no data

Actually I'm trying to insert some data into a table using a cursor, and I'm getting no error. It just says:
Query executed OK, 0 rows affected.
I'm trying to get data from each field of the testdatabase.wp_posts p table and insert it into an existing table `testdatabase.wp_postmeta.
My syntax is:
DROP PROCEDURE IF EXISTS add_authors_to_prod;
DELIMITER $$
CREATE PROCEDURE add_authors_to_prod()
BEGIN
DECLARE v_post_id INT;
DECLARE v_author_id INT;
DECLARE v_last_author INT DEFAULT 0;
DECLARE str VARCHAR(500);
DECLARE v_first_id INT DEFAULT 10916;
DECLARE author_csr CURSOR FOR
SELECT n.nid, p.id FROM testdatabase.wp_posts p
JOIN drupaltest.srmaif_field_data_field_auther_list auth ON auth.entity_id = p.id
JOIN drupaltest.node_field_data n ON auth.field_auther_list_nid = n.nid
ORDER BY p.id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_last_author = 1;
START TRANSACTION;
OPEN author_csr;
author_loop:LOOP
FETCH author_csr INTO v_author_id, v_post_id;
IF v_last_author THEN
SET str = CONCAT(str, '}');
LEAVE author_loop;
END IF;
IF v_first_id <> v_post_id THEN
SET v_first_id = v_post_id;
SET str = CONCAT(str, '}');
INSERT INTO testdatabase.wp_postmeta (post_id, meta_key, meta_value)
VALUES(v_post_id, 'authors', CONCAT(str, v_author_id));
SET str = 'a:2:{';
ELSE
SET str = CONCAT(str,'i:', v_author_id, ';s:5:\"', v_author_id, '\";');
END IF;
END LOOP author_loop;
CLOSE author_csr;
SET v_last_author=0;
END$$
DELIMITER ;
CALL add_authors_to_prod()

Not able to run/execute MySQL Triggers

I am creating Trigger into Mysql Workbanch , but not able to create the trigger.
Can Anyone Help me For This.
DELIMITER $$
create TRIGGER Insert_Test
After Delete ON Test
FOR EACH ROW
BEGIN
START TRANSACTION;
DECLARE v_ID INT;
DECLARE v_Error TINYINT;
DECLARE v_AgentID INT;
SELECT v_Error = 0;
v_ID=Old.id;
BEGIN
DELETE Test2 WHERE id = SELECT id FROM Test where id=v_ID;
Rollback;
SET v_Error = 1;
END
IF v_Error = 0;
THEN
COMMIT;
ELSEIF
v_Error = 1;
THEN
ROLLBACK;
END IF;
END
DELIMITER ;
Sql server Trigger
ALTER TRIGGER [dbo].[tr_DelRecordTypeID] ON [dbo].[luRecordType] FOR DELETE
AS
SET NOCOUNT ON
BEGIN TRANSACTION
DECLARE #ID INT, #GroupTypeID INT, #Error BIT, #Msg VARCHAR(500)
SELECT #Error = 0
SELECT #ID = RecordTypeID FROM deleted
SELECT #GroupTypeID = 30
IF EXISTS ( SELECT g.GroupID
FROM luGroup g,
[luGroupDetail] gd
WHERE g.[GroupID] = gd.[GroupID]
AND g.[GroupTypeID] = #GroupTypeID
AND gd.[MemberID] = #ID )
BEGIN
DELETE [agAgent] WHERE [AgentID] = (SELECT TOP 1 AgentID FROM agAgentPayType)
Rollback transaction
SET #Error = 1
END
IF #Error = 0
BEGIN
COMMIT TRANSACTION
END
ELSE
IF #Error = 1
BEGIN
ROLLBACK TRANSACTION
END
This trigger which i am trying to achieve into mysql workbanch, please check
I shall Be thankful,
Thanks
Aman