I have following stored procedure which was executing perfectly. But since morning I am getting the error
Prepared statement needs to be re-prepared
I tried all the things but could not get any help.
CREATE PROCEDURE `SP_FEEDBACK_REPORT`(
IN p_eventID INT(10),
IN p_sessionID INT(10)
)
BEGIN
SET SESSION group_concat_max_len = 10240;
SET #sqlQry = NULL;
SELECT GROUP_CONCAT(DISTINCT CONCAT(' MAX( IF(attended_id = ', attended_id, ', IF(answer_id !=47, (SELECT key_value FROM tblrating_master WHERE key_id=answer_id), comments),NULL)) AS ', attendee_name)) INTO #sqlQry FROM vwfeedback_report WHERE event_id=p_eventID AND session_id=p_sessionID;
SET #sqlQry = CONCAT('SELECT question, ', #sqlQry, ' FROM vwfeedback_report WHERE event_id=', p_eventID,' AND session_id=', p_sessionID,' GROUP BY feedback_id;');
-- SELECT #sqlQry FROM DUAL;
PREPARE stmt FROM #sqlQry;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET SESSION group_concat_max_len = 1024;
END;
Please help
I am using a prepared statement to insert data from table 1 into table 2 (a copy of table 1)
Only difference is table 2 has a DateTime column. I am trying to insert the data from table 1 into table 2. But how can I add the datetime(current time and date)?
I am a little suck on how to add this into my prepared statement.
DECLARE sqlquery text;
SET sqlquery = CONCAT('insert into partial_log select inaid, `PartNo`,`LineCode`,`EPartsPartNo`,`Surcharge`,`Price`,`CustomerDescription`,`ShortCode`,`PlusCode`,`LiveDate`,`CustomerPartNo`,`Alt1LineCode`,`Alt1PartNo`,`Alt1EPartsPartNo`,`Alt2LineCode`,`Alt2PartNo`,`Alt2EPartsPartNo`,`Alt3LineCode`,`Alt3PartNo`,`Alt3EPartsPartNo`,`Alt4LineCode`,`Alt4PartNo`,`Alt4EPartsPartNo`,`Alt5LineCode`,`Alt5PartNo`,`Alt5EPartsPartNo`,`Alt6LineCode`,`Alt6PartNo`,`Alt6EPartsPartNo`,`Alt7LineCode`,`Alt7PartNo`,`Alt7EPartsPartNo`,`Alt8LineCode`,`Alt8PartNo`,`Alt8EPartsPartNo`,`Alt9LineCode`,`Alt9PartNo`,`Alt9EPartsPartNo`,`Alt10LineCode`,`Alt10PartNo`,`Alt10EPartsPartNo`,',NOW(),',FROM itable order by `LineCode`,`PartNo`;');
set sqlquery=REPLACE(sqlquery,'inaid',inaid);
set sqlquery=REPLACE(sqlquery,'itable',itable);
SET #stat = sqlquery;
PREPARE stmt from #stat;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
I have tried to add Now() by using concat but this does not work and throws invalid syntax error.
#42000You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '14:00:06,FROM Ats order by LineCode,PartNo' at line 1
UPDATE:
Do not need CONCAT to add NOW() into the statement.
DECLARE sqlquery text;
SET sqlquery = 'insert into partial_log select inaid, `PartNo`,`LineCode`,`EPartsPartNo`,`Surcharge`,`Price`,`CustomerDescription`,`ShortCode`,`PlusCode`,`LiveDate`,`CustomerPartNo`,`Alt1LineCode`,`Alt1PartNo`,`Alt1EPartsPartNo`,`Alt2LineCode`,`Alt2PartNo`,`Alt2EPartsPartNo`,`Alt3LineCode`,`Alt3PartNo`,`Alt3EPartsPartNo`,`Alt4LineCode`,`Alt4PartNo`,`Alt4EPartsPartNo`,`Alt5LineCode`,`Alt5PartNo`,`Alt5EPartsPartNo`,`Alt6LineCode`,`Alt6PartNo`,`Alt6EPartsPartNo`,`Alt7LineCode`,`Alt7PartNo`,`Alt7EPartsPartNo`,`Alt8LineCode`,`Alt8PartNo`,`Alt8EPartsPartNo`,`Alt9LineCode`,`Alt9PartNo`,`Alt9EPartsPartNo`,`Alt10LineCode`,`Alt10PartNo`,`Alt10EPartsPartNo`, NOW() FROM itable order by `LineCode`,`PartNo`;';
set sqlquery=REPLACE(sqlquery,'inaid',inaid);
set sqlquery=REPLACE(sqlquery,'itable',itable);
SET #stat = sqlquery;
PREPARE stmt from #stat;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
The CONCAT is totally unnecessary, and just adding to your confusion.
Your query can be written like this
SET sqlquery = 'insert into partial_log
select inaid, `PartNo`,`LineCode`,
`EPartsPartNo`,`Surcharge`,`Price`,
`CustomerDescription`,`ShortCode`,`PlusCode`,
`LiveDate`,`CustomerPartNo`,`Alt1LineCode`,
`Alt1PartNo`,`Alt1EPartsPartNo`,`Alt2LineCode`,
`Alt2PartNo`,`Alt2EPartsPartNo`,`Alt3LineCode`,
`Alt3PartNo`,`Alt3EPartsPartNo`,`Alt4LineCode`,
`Alt4PartNo`,`Alt4EPartsPartNo`,`Alt5LineCode`,
`Alt5PartNo`,`Alt5EPartsPartNo`,`Alt6LineCode`,
`Alt6PartNo`,`Alt6EPartsPartNo`,`Alt7LineCode`,
`Alt7PartNo`,`Alt7EPartsPartNo`,`Alt8LineCode`,
`Alt8PartNo`,`Alt8EPartsPartNo`,`Alt9LineCode`,
`Alt9PartNo`,`Alt9EPartsPartNo`,`Alt10LineCode`,
`Alt10PartNo`,`Alt10EPartsPartNo`, NOW()
FROM itable order by `LineCode`,`PartNo`;');
In fact as long as the columns are all in the same order in both the partial_log and itable tabless with only the addition of the new column datetime at the end of partial_log you could write the query like this
SET sqlquery = 'insert into partial_log
SELECT a.*, NOW()
FROM itable a
ORDER BY a.LineCode,a.PartNo
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT('sum(case when week = ',week,
'then totaltime else 00 end) AS `',
week, '`')
)INTO #sql
FROM mytable;
SET #sql = CONCAT('SELECT TRIM(UPPER(mytable.empcode))AS CODE,
TRIM(UPPER(mytable.empname)) EMPNAME,
', #sql, '
from mytable
group by empcode
order by empname ');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
I am using pivot table here, want to make sum of totaltime according to week field which is dynamically generated by query, but I got an error :
"#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 'from mytable group by empcode or' at line 6"
I am selecting name field from a table and I want to give alias to the name field as current year like SELECT name AS 'current_year' FROM 'places' . current_year should be like 2014 . Is it possible ?
It's definitely possible, but you have to use a prepared statement:
SET #current_year = YEAR(CURDATE());
SET #s = CONCAT('SELECT name AS \'', #current_year, '\' FROM places');
PREPARE stmt FROM #s;
EXECUTE stmt;
DEMO # SQL Fiddle
Yes it is possible.
Use this
SET #s = CONCAT( 'SELECT name AS \'', YEAR( CURDATE( ) ) , '\' FROM places' ) ;
PREPARE stmt FROM #s ;
EXECUTE stmt;
Please can anyone tell me why this stored procedure is not working , what am I doing wrong.It keeps giving syntax error.
puserid and plimit are the parameters passed in the procedure.
DECLARE uid BIGINT;
DECLARE rangee TINYINT;
SET #uid = puserid;
SET #rangee = plimit * 15;
PREPARE STMT FROM
'IF((SELECT count(type) from notificationrecievers nr where
nr.status=0 and nr.recieverid=?) = 0) THEN
select nr.type,n.senderid,n.postid,u.name,nr.status
from
user u,notifications n,notificationrecievers nr where
nr.recieverid=? and u.userid=n.senderid and
nr.notificationid=n.notid order by n.notid desc
limit 15 OFFSET ?;
ELSE
select nr.type,n.senderid,n.postid,u.name,nr.status
from
user u,notifications n,notificationrecievers nr where
nr.recieverid=? and u.userid=n.senderid and nr.status=0 and
nr.notificationid=n.notid order by n.notid desc;
END IF;';
EXECUTE STMT USING #uid,#uid,#rangee,#uid;
Here's the error that i get.
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 'IF((SELECT count(type) from notificationrecievers nr where nr.status=0 and nr.r' at line 1
You can use only DML in prepared statements.
You can do something like this
IF condition THEN
set #q = 'your query text'
ELSE
set #q = 'another query'
END IF;
PREPARE stmt FROM #q
EXECUTE stmt using ...
DEALLOCATE PREPARE stmt
You can execute some queries before to evaluate your conditions.
Hope it helps