I have a MySQL function as below, but it is generating an error:
BEGIN
DECLARE image1 VARCHAR(250);
select case when
(
select COUNT(*)
from profile_images
where building_id = bid
and contractor_id = cid
) > 0
then (
select distinct (image)
-- into image1
from profile_images
where building_id = bid
and contractor_id = cid limit 1
) else (
select distinct (image)
-- into image1
from profile_images
where contractor_id = cid limit 1
)
END into image1;
RETURN image1;
END
The actual error shown by MySQL is #2014 - Commands out of sync; you can't run this command now.
It was a mistake.wrong parametername passed to the function.Thank you very much Arvindh Mani.
Related
i need help for the below code. My problem is that i want only that code executes once per statement (after i search i checked that expression don't exists anymore only once per row).
So i tried to add:
IF NOT EXISTS
(Select count(*) FROM replay_replays_access WHERE id_game = new.id_game GROUP BY id_game HAVING count(*) <5)
THEN
But didn't work either what can i do, its duplicating sometime triplicating the information?
TRIGGER replay
AFTER UPDATE
ON table_replays FOR EACH ROW
begin
IF EXISTS
(SELECT
replay_games.room_name
FROM replay_games
WHERE replay_games.room_name = 'Tournament Room' and replay_games.id = new.id_game)
THEN
IF NOT EXISTS
(Select
count(*)
FROM replay_replays_access
WHERE id_game = new.id_game
GROUP BY id_game
HAVING count(*) <5)
THEN
INSERT INTO replay_replays_access(id_game, id_player, replay_name, do_not_hide)
SELECT
new.id_game,
replay_users.id ,
CONCAT(
(SELECT game_types
FROM replay_games
WHERE id=new.id_game),
': ',
(SELECT
descr
FROM replay_games
WHERE id=new.id_game)) ,
0
FROM replay_users
WHERE
(replay_users.admin > 0 OR
replay_users.privlevel = 'TOURNAMENT MEMBER')
AND NOT replay_users.name = (
SELECT
replay_games.creator_name
FROM replay_games
WHERE replay_games.id = new.id_game);
END IF;
END IF;
END
When I run the following select Query in SQl
SELECT Count(*)
FROM workordercurrent
WHERE office_id = 1
AND ( ( scheduleddate = '2018-11-01' )
OR ( schedulestopdate = '2018-11-01' )
OR ( scheduleddate = '0000-00-00'
AND orderdate = '2018-11-01' ) )
AND worktype <> 6
The query returns 694 as count which is right
When I write the same Query in the SQL Procedure with 2 input parameters
office_id(int) and order_date (DATE)
BEGIN
SELECT Count(*)
FROM workordercurrent
WHERE office_id = office_id
AND ( ( scheduleddate = order_date )
OR ( schedulestopdate = order_date )
OR ( scheduleddate = '0000-00-00'
AND orderdate = order_date ) )
AND worktype <> 6;
END
It returns the count as 3260
What is the problem here as both queries exactly same. Here is how I am running the Stored procedure
You should avoid using Stored procedure's parameter name same as the columns/aliases used in your SP. WHERE office_id = office_id is behaving weird due to ambiguous name. MySQL is probably not able to resolve it as either a column name or parameter.
I normally prefix in_ or out_ or inout_ to param names; which also shows the type of param (for readability).
So you can rename the parameters to in_office_id and in_order_date instead.
I have the following SQL update trigger that works properly:
BEGIN
DECLARE myID INT;
SELECT user_id INTO myID FROM writer WHERE writer_id = NEW.writer_id;
IF (NEW.status_id = 2) THEN
INSERT INTO activity (
user_id,
work_id,
activity,
date_created
) VALUES (
myID,
NEW.work_id,
'confirmed',
now()
);
ELSE
INSERT INTO activity (
user_id,
work_id,
activity,
date_created
) VALUES (
myID,
NEW.work_id,
'modified',
now()
);
END IF;
END
I need to add an additional trigger as the following:
CREATE TRIGGER updateWorkStatus AFTER UPDATE ON writer_split
FOR EACH ROW
BEGIN
UPDATE work a
JOIN writer_split b
ON a.work_id = b.work_id AND a.current_version = b.version
SET a.status_id = 2
WHERE a.work_id NOT IN (
SELECT ab.work_id
FROM (SELECT s.work_id
FROM work w INNER JOIN writer_split s
ON w.work_id = s.work_id AND s.status_id != 2) ab
);
END;
when I run this create script, I am getting a syntax error. Any ideas?
For me i will use UPDATE work as a
I have the following statement, which produces this error. Apparently the alias "R" in the first line of the select statement is not declared, but it's right there at the end of the statement. Anyone have any ideas?
#1327 - Undeclared variable: R
CREATE PROCEDURE `get_series_completion`(IN team1_id INT, IN team2_id INT, IN round_shortname VARCHAR(5))
BEGIN
DECLARE num_games_total, num_games_played INT;
SELECT COUNT(*) INTO num_games_played, R.games_per_series INTO num_games_total
FROM (
SELECT game_id
FROM team_games
WHERE team_id IN (team1_id, team2_id)
GROUP BY game_id
HAVING COUNT(*) = 2
AND SUM(standing = 0) = 0
) AS S, rounds AS R ON R.round_shortname = round_shortname;
SELECT num_games_played/num_games_total;
END$$
You should select all columns before into clause, Your query should be:
SELECT COUNT(*), R.games_per_series INTO num_games_played, num_games_total
FROM (
SELECT game_id
FROM team_games
WHERE team_id IN (team1_id, team2_id)
GROUP BY game_id
HAVING COUNT(*) = 2
AND SUM(standing = 0) = 0
) AS S
LEFT OUTER JOIN rounds AS R ON R.round_shortname = round_shortname;
or
SELECT COUNT(*), R.games_per_series INTO num_games_played, num_games_total
FROM (
SELECT game_id
FROM team_games
WHERE team_id IN (team1_id, team2_id)
GROUP BY game_id
HAVING COUNT(*) = 2
AND SUM(standing = 0) = 0
) AS S, rounds AS R
WHERE R.round_shortname(+) = round_shortname; -- implicit left outer join
I'm trying to use a while loop in a one-off query on a MySQL (5.1.41-3ubuntu12.10-log) database:
WHILE ((SELECT COUNT(*) FROM
(SELECT id, COUNT(*) AS cnt
FROM foo
GROUP BY id
ORDER BY COUNT(*) DESC) cnts
WHERE cnt > 1) != 0) DO
BEGIN
SET #curr_id = (SELECT id FROM
(SELECT id, COUNT(*) AS cnt
FROM foo
GROUP BY id
ORDER BY COUNT(*) DESC) cnts
WHERE cnt > 1
LIMIT 1);
SET #new_id = (SELECT MAX(id) + 1
FROM foo);
UPDATE foo
SET id = #new_id
WHERE id = #curr_id
LIMIT 1;
END WHILE;
What this does is while there are multiple records with the same id, update one of them with the next id.
The syntax in the body is correct and the predicate used in the while statement also executes without any trouble on it's own. MySQL returns a syntax error on the beginning of the query:
Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHILE ((SELECT count(*) FROM
(SELECT id, COUNT(*) AS cnt
FROM stock_produ' at line 1
I realize this might not be The Right Way of doing things, but this is a very badly (or rather not-at-all) thought out database, so it would be awesome if I could get it to work this way.
Thanks,
Robin
It looks as though you are trying to run this procedural code as an anonymous block. While this works in some databases (like Oracle) it can't be done in MySQL.
If you want to run this then put it in a stored procedure and then call the procedure. Hence:
Create procedure
DELIMITER $$
CREATE PROCEDURE `foo_update_routine`()
BEGIN
WHILE ((SELECT COUNT(*) FROM
(SELECT id, COUNT(*) AS cnt
FROM foo
GROUP BY id
ORDER BY COUNT(*) DESC
) cnts
WHERE cnt > 1) != 0)
DO
SET #curr_id = (SELECT id FROM
(SELECT id, COUNT(*) AS cnt
FROM foo
GROUP BY id
ORDER BY COUNT(*) DESC
) cnts
WHERE cnt > 1
LIMIT 1);
SET #new_id = (SELECT MAX(id) + 1 FROM foo);
UPDATE foo SET id = #new_id
WHERE id = #curr_id
LIMIT 1;
END WHILE;
END $$
Call Procedure
CALL `foo_update_routine`;
PS You might want to investigate the HAVING clause for your select statements...