Capitalize first letter in all rows MySQL - mysql

I need a script to run that will update 'given_name' column and change all first letters to capital..
I currently have this code.. and nothing happens..
DROP PROCEDURE IF EXISTS CAPITALIZE;
DELIMITER $$
CREATE PROCEDURE CAPITALIZE()
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 1;
SELECT COUNT(*) INTO n FROM users;
-- SET n = (SELECT COUNT(*) FROM users);
SET i=0;
WHILE (i<=n) DO
UPDATE users SET given_name = CONCAT( UPPER(SUBSTR(given_name,1,1)), SUBSTR(given_name,2)) WHERE user_id = n;
UPDATE users SET last_name = CONCAT( UPPER(SUBSTR(last_name,1,1)), SUBSTR(last_name,2)) WHERE user_id = n;
SET i = (i + 1);
END WHILE;
END $$
DELIMITER ;

As #Drew mentioned, you might be better off just doing a blanket UPDATE statement for the entire users table:
UPDATE users
SET given_name = CONCAT( UPPER(SUBSTR(given_name,1,1)), LOWER(SUBSTR(given_name,2))),
last_name = CONCAT( UPPER(SUBSTR(last_name,1,1)), LOWER(SUBSTR(last_name,2)))
To turn off your safe update mode, which is preventing the above query from working, type this:
SET SQL_SAFE_UPDATES = 0;
Here is a working fiddle which demonstrates the above query:
SQLFiddle

Related

MYSQL Stored Procedure only updates on record

MYSQL Stored Procedure only updates one record when it should update all the ids in the WHERE id IN clause. When I run the SELECT and UPDATE outside of the stored procedure it works fine. Any suggestions?
DELIMITER $$
CREATE PROCEDURE update_ids(IN source int(10),IN target int(10))
BEGIN
DECLARE idList varchar(5000) DEFAULT NULL;
SET idList = (SELECT GROUP_CONCAT(id SEPARATOR ', ') FROM myTable ii WHERE ii.generic_id = source);
UPDATE myTable i SET i.generic_id = target WHERE i.id IN (idList);
END$$
DELIMITER ;
Then I call it by -->
CALL update_generic_ids(63, 1258);
Update : 1 row effected.
Thanks in advance,
once try below chunk of code, hope it will solve your problems!
If any question reach me!
DELIMITER $$
CREATE PROCEDURE update_ids(IN source int(10),IN target int(10))
BEGIN
DECLARE idList varchar(5000) DEFAULT NULL;
--SET idList = (SELECT GROUP_CONCAT(id SEPARATOR ', ') FROM myTable ii WHERE ii.generic_id = source);
UPDATE myTable i SET i.generic_id = target WHERE i.id IN (
SELECT id FROM myTable ii WHERE ii.generic_id = source
);
END$$
DELIMITER ;
Actually, as per my comments, I think is may be the answer..
There's a world of difference between these two queries:
UPDATE a SET b = c WHERE d IN ('1,2,3,4')
UPDATE a SET b = c WHERE d IN (1,2,3,4)
I'd recommend you amend your procedure so you're not storing your list of ids in a string variable. Actually you shouldn't need to store anything, just put the list generated by the select into the update:
UPDATE myTable i SET i.generic_id = target
WHERE i.id IN
(SELECT id SEPARATOR FROM myTable ii WHERE ii.generic_id = source);

MySQL error "Truncated incorrect DOUBLE value" in procedure

I have problem with my procedure. I try to take values from sales table and make a query using them.
Procedure looks like this:
DROP PROCEDURE IF EXISTS turnover;
DELIMITER $$
CREATE PROCEDURE turnover()
BEGIN
DECLARE col INT;
DECLARE q TEXT;
DECLARE i INT DEFAULT 0;
DECLARE m TEXT;
SET col = (SELECT count(DISTINCT article) FROM sales);
SET q = "SELECT article, ";
WHILE i < co DO
SET m = (SELECT DISTINCT month FROM sales LIMIT 1 OFFSET i);
SET q = q + "SUM(IF(month=" + m + ",value,NULL)) AS " + m;
IF i < (col - 1) THEN
SET q = q + ", ";
END IF;
SET i = i + 1;
END WHILE;
SET q = q + " FROM sales GROUP BY article";
EXECUTE q;
END$$
DELIMITER ;
CALL turnover();
I receive error:
Error Code: 1292. Truncated incorrect DOUBLE value: ',value,NULL)) AS '
How i can make it works?
Thanks.
The col issue was fixed or assumed in the below.
CREATE SCHEMA safe_Tuesday_01; -- safe sandbox
USE safe_Tuesday_01; -- DO the work in this db to test it
-- a fake table, we need something
create table sales
( article varchar (100) not null,
month int not null
);
Step 1, find out what the string looks like:
DROP PROCEDURE IF EXISTS turnover;
DELIMITER $$
CREATE PROCEDURE turnover()
BEGIN
DECLARE col INT;
DECLARE q TEXT;
DECLARE i INT DEFAULT 0;
DECLARE m TEXT;
SET col = (SELECT count(DISTINCT article) FROM sales);
SET q = "SELECT article, ";
WHILE i < col DO
SET m = (SELECT DISTINCT month FROM sales LIMIT 1 OFFSET i);
SET q = CONCAT(q,"SUM(IF(month=" + m + ",value,NULL)) AS ", m);
IF i < (col - 1) THEN
SET q = q + ", ";
END IF;
SET i = i + 1;
END WHILE;
SET q = CONCAT(q," FROM sales GROUP BY article");
select q;
-- EXECUTE q; -- No no no this is wrong anyway
END$$
DELIMITER ;
CALL turnover();
SELECT article, FROM sales GROUP BY article
Well that above SELECT does not look so hot. Repeatedly fix your logic in Step 1 to fix that string.
Step 2, when you fix the code above, plop in the below. Note, at the moment, it is not fixed. So do that, again, above.
But in the below, use a proper PREPARED STATEMENT which you are not.
DROP PROCEDURE IF EXISTS turnover;
DELIMITER $$
CREATE PROCEDURE turnover()
BEGIN
DECLARE col INT;
DECLARE q TEXT;
DECLARE i INT DEFAULT 0;
DECLARE m TEXT;
SET col = (SELECT count(DISTINCT article) FROM sales);
SET q = "SELECT article, ";
WHILE i < col DO
SET m = (SELECT DISTINCT month FROM sales LIMIT 1 OFFSET i);
SET q = CONCAT(q,"SUM(IF(month=" + m + ",value,NULL)) AS ", m);
IF i < (col - 1) THEN
SET q = q + ", ";
END IF;
SET i = i + 1;
END WHILE;
SET q = CONCAT(q," FROM sales GROUP BY article");
-- select q;
SET #theSQL=q;
PREPARE stmt1 FROM #theSQL;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
END$$
DELIMITER ;
When done,
DROP SCHEMA safe_Tuesday_01; -- clean up, poof, sandbox is gone
CONCAT is your friend. You missed this step. It is important that the PREPARE works against a User Variable (with an # sign) and not a Local Var (from a DECLARE) else it will blow up. So I fixed that above with the #theSQL
Again, see the MySQL Manual Page PREPARE Syntax. It is important to get your string right. That is the point of Step 1. Only then do you move on to Step 2 and using it.
It happens when SELECT DISTINCT month FROM sales doesn't return anything. On the next line, the query fragment is generated as SUM(IF(month=,value,NULL)) AS and, of course, there is an error there (maybe MySQL doesn't produce the correct error message, but this is where the error is).
And the cause of the error is the WHILE line that compares i against the unknown variable co. It should probably read:
WHILE i < col DO
But it doesn't fix the problem because col is the number of distinct values of article and you iterate from 1 to col over the distinct values of month. Most probably they are in different amounts and if the number of articles is bigger then the error will happen again.

Update in mysql trigger not working

I'm creating a trigger to execute after an insert is done into my checkin table but my update statement is not working
DELIMITER $$
DROP TRIGGER IF EXISTS checkins_AINS$$
CREATE TRIGGER `checkins_AINS` AFTER INSERT ON `checkins` FOR EACH ROW
BEGIN
DECLARE client_id INT;
DECLARE duplicate_record INTEGER DEFAULT 0;
DECLARE bpoints INT;
DECLARE business_id INT;
DECLARE CONTINUE HANDLER FOR 1062 SET duplicate_record = 1;
SELECT checkinPoints, id INTO bpoints, business_id FROM businesses WHERE id = new.venue_id;
INSERT INTO clients_checkins_summary(client_id, venue_id, first_checkin, last_checkin,visits)
VALUES(new.client_id, new.venue_id, new.createAt, new.createAt,1);
INSERT INTO clients_points_summary(client_id, business_id,current_points)
VALUES(new.client_id, business_id,bpoints);
IF duplicate_record = 1
THEN
UPDATE clients_checkins_summary
SET last_checkin = new.createAt,
visits = visits + 1
WHERE client_id = new.client_id and venue_id = new.venue_id;
UPDATE clients_points_summary
SET current_points = current_points + bpoints
WHERE client_id = new.client_id and business_id = business_id;
END IF;
END$$
DELIMITER ;
Inserting:
insert into checkins(client_id,venue_id,points,createAt,updateAt)
values (52,19,1,now(),now());
for the first time works fine but when the case of update is trigger is entering into the if but is not update the value.
I trace the variables into a table and all the values are correct but update is not been updating anything.
I missing something?
am I missing something?
Possibly this
UPDATE clients_points_summary
SET current_points = current_points + bpoints
WHERE client_id = new.client_id and business_id = business_id;
The problem here is that your local variable business_id and the column name clients_points_summary.business_id are ambiguous. You could disambiguate as follows:
UPDATE clients_points_summary cps
SET cps.current_points = cps.current_points + bpoints
WHERE cps.client_id = new.client_id and cps.business_id = business_id;

Check MySQL database for unique value over many tables

I'm looking for a way to easily check each table of a MySQL database and make sure that a certain field contains one value only. I have tables named Authors, Titles, Places, etc.
Each table contains a field called xuser and it needs to ask "does the field xuser contain the value xy in all records of all tables".
Can someone push me in the right direction how to do this with a SQL query if this is possible?
Thanks for reading, regards
Nico
I've created stored procedure which checks all table for provided db:
DELIMITER $$
DROP PROCEDURE IF EXISTS `UTL_CHECK_BACKUP_FOR_USER` $$
CREATE PROCEDURE `UTL_CHECK_BACKUP_FOR_USER`(
IN i_database_name VARCHAR(255),
IN i_user_column_name VARCHAR(255),
IN i_user_column_value VARCHAR(255),
OUT o_result TINYINT(1)
)
BEGIN
DECLARE v_table_name VARCHAR(255);
DECLARE v_last_row_fetched TINYINT(3) DEFAULT 0;
DECLARE tables_cursor CURSOR FOR
SELECT table_name
FROM information_schema.tables
WHERE table_schema = i_database_name
;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_last_row_fetched = 1;
SET v_last_row_fetched = 0;
OPEN tables_cursor;
SET #query =
CONCAT(
'SELECT SUM(IF(user_column=''',
i_user_column_value,
''', 1, -1)) = 1 INTO #o_result FROM ( SELECT ''test'' AS user_column FROM information_schema.tables WHERE 1<>1 '
)
;
table_loop: LOOP
FETCH tables_cursor INTO v_table_name;
IF (v_last_row_fetched = 1) THEN
LEAVE table_loop;
END IF;
SET #query =
CONCAT(
#query,
' UNION SELECT DISTINCT(',
i_user_column_name,
') AS user_column FROM ',
v_table_name
)
;
END LOOP table_loop;
CLOSE tables_cursor;
SET v_last_row_fetched=0;
SET #query =
CONCAT(
#query,
' ) all_xusers;'
)
;
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET o_result = COALESCE(#o_result, 0);
END $$
DELIMITER ;
Just deploy this stored procedure to database.
And then it could be executed in the following way:
-- db_name, user_column_name, user_column_value, result
call UTL_CHECK_BACKUP_FOR_USER('test', 'xuser', 'xxx', #result);
select #result;
To get the rows from all three tables where xuser has the same value in all three tables you could use:
SELECT *
FROM authors a
JOIN titles t
ON t.xuser = a.xuser
JOIN places p
ON p.xuser = t.xuser
If you want to look at a specific xuser value you could add the following WHERE clause:
WHERE a.xuser = 'xy'
The first thing comes to my mind:
select sum(if(xuser='xxx', 1, -1)) = 1
from (
select distinct(xuser) from authors
union
select distinct(xuser) from titles
union
select distinct(xuser) from places
) all_xusers;
This will return 1 (true) if all tables contains records belonging ONLY to 'xxx' user. Otherwise (if there is no 'xxx' records or there is some other user records) it will return 0 (false).

Procedure that does not UPDATE a table row

I have this procedure (don't bother too much to figure it out what it does, aim for comments named "Modify 1,2,3,4" )
/* PROCEDURE 1 : Post notification */
DROP PROCEDURE IF EXISTS AddNotificationOnPosts;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `AddNotificationOnPosts`(arg_from_user INT(11),arg_on_post_id INT(11),arg_in_group_id INT(11))
BEGIN
DECLARE num_rows INT DEFAULT NULL;
DECLARE insert_result INT DEFAULT NULL;
DECLARE user_id INT DEFAULT NULL;
DECLARE done INT DEFAULT 0;
DECLARE var_user_id INT DEFAULT NULL;
DECLARE c1 CURSOR FOR
SELECT user_id
FROM user_rights
WHERE user_rights.right = 101 AND user_rights.group_id = arg_in_group_id
ORDER BY user_id DESC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
IF(arg_from_user IS NULL OR arg_from_user = '')
THEN
SELECT "0" AS response;
ELSEIF(arg_on_post_id IS NULL OR arg_on_post_id = '')
THEN
SELECT "0" AS response;
ELSEIF(arg_in_group_id IS NULL OR arg_in_group_id = '')
THEN
SELECT "0" AS response;
ELSE
SELECT count(notification_id) FROM notifications_posts
WHERE
from_user = arg_from_user AND
on_post_id = arg_on_post_id AND
in_group_id = arg_in_group_id
INTO num_rows;
/* MODIFY 1*/
UPDATE user_info SET notifications = 1 WHERE user_id = 145;
END IF;
IF num_rows = 0
THEN
INSERT INTO notifications_posts(from_user,on_post_id,in_group_id) VALUES(arg_from_user,arg_on_post_id,arg_in_group_id);
SELECT ROW_COUNT() INTO insert_result;
/* MODIFY 2*/
UPDATE user_info SET notifications = 1 WHERE user_id = 1;
IF insert_result > 0
THEN
/* MODIFY 3*/
UPDATE user_info SET notifications = 1 WHERE user_id = 5;
/* Increment the notifications for every user*/
OPEN c1;
read_loop: LOOP
FETCH c1 INTO var_user_id;
IF done THEN
LEAVE read_loop;
ELSE
/* MODIFY 4*/
UPDATE user_info SET notifications = 1 WHERE user_id = 1;
END IF;
END LOOP;
CLOSE c1;
SELECT "1" AS response;
ELSE
SELECT "0" AS response;
END IF;
ELSE
SELECT "0" AS response;
END IF;
END $$
DELIMITER ;
This works just fine, except the lines
UPDATE user_info SET notifications = 1 WHERE user_id = 1;
won't work, but in simple plain SQL(phpmyadmin) this query is working fine. What is the problem?
What this script does? It helps me be able to post a notification to certain users, and when you post something in group1 all users that have right101 on that group must be notified like
UPDATE user_info SET notifications = notifications + 1 WHERE user_id = var_user_id;
using a cursor as a FOR LOOP like i used to have in PHP
What is wrong with this? Can't a procedure update data?!
Hope i made myself understandable.
Not to sound presumptuous but does the data allow you to get past the
IF num_rows = 0
As a tip though if you are running in SQL Management studio you can debug your sql with breakpoints like normal code. I suggest putting a breakpoint on that line and see if it actually gets hit at all.
How about something along these lines? I usually work in SQL Server, so I apologize if some of the syntax is off, but I threw in some comments, so I hope you get the gist.
/* PROCEDURE 1 : Post notification */
DROP PROCEDURE IF EXISTS AddNotificationOnPosts;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `AddNotificationOnPosts`(arg_from_user INT(11), arg_on_post_id INT(11), arg_group_id INT(11))
BEGIN
-- sanity checks
IF(arg_from_user IS NULL OR arg_from_user = '')
THEN
RETURN 0;
ELSEIF(arg_on_post_id IS NULL OR arg_on_post_id <= 0)
THEN
RETURN 0;
ELSEIF(arg_in_group_id IS NULL OR arg_in_group_id <= 0)
THEN
RETURN 0;
END IF;
BEGIN TRAN;
-- insert if notification post does not exist
IF NOT EXISTS
(
SELECT *
FROM notification_posts
WHERE
from_user = arg_from_user AND
on_post_id = arg_on_post_id AND
in_group_id = arg_in_group_id
)
THEN
INSERT INTO notifications_posts
(
from_user,
on_post_id,
in_group_id
)
VALUES
(
arg_from_user,
arg_on_post_id,
arg_in_group_id
);
END IF;
-- update all users with 101 right
UPDATE ui
SET notifications = notifications + 1
FROM user_info ui
JOIN user_rights ur on ur.user_id = ui.user_id
WHERE ur.right = 101 and ur.group_id = arg_in_group_id
COMMIT;
END $$
DELIMITER ;