I have been trying to change existing table and insert the result into temporary table with memoery engine
1) Step - I swaped rows and columns from existing table
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(`parameterId` = ', `parameterId`, ',`valueId`,NULL)) AS parameter', `parameterId`)
) INTO #sql
FROM product_parameter;
SET #sql = CONCAT('SELECT productId , ', #sql , ' FROM product_parameter GROUP BY productId');
PREPARE stmt FROM #sql;
When I execute the stmt statement it will show me a the result
2) Step - I would like to insert the result to temporary table or for testing into physical table
CREATE TEMPORARY TABLE IF NOT EXISTS temporaryTable ENGINE=MEMORY AS
Is it possible to acomplish my goal by one query? Or should i create table then somehow add the result data?
You should create the temporary table and the populate it.
I am creating a MySQL Event but it relates with table record id,
so when that record is deleted at that time this MySQL Event should be deleted.
It worked with my code but what if record is directly deleted from phpMyAdmin?
Trying this scenario, the Event is not deleted and record does not exist. So an error occured.
$this->db->insert("invoice", $data);
$last = $this->db->insert_id();
if ($last)
$str = $this->db->last_query();
$this->db->query("SET GLOBAL event_scheduler = 'ON'");
$this->db->query("CREATE EVENT rec" . $last . " ON SCHEDULE
EVERY 1 MONTH STARTS DATE_SUB(NOW(),INTERVAL '0:01' HOUR_MINUTE)
ON COMPLETION PRESERVE ENABLE DO $str");
How can I overcome this problem? I Heard that i can use a trigger but How?
Make a trigger like this:
SET #ename:='rec';
SET #sql_text = concat('CREATE TRIGGER delete_invoice_event AFTER DELETE on TABLE FOR EACH ROW BEGIN DROP EVENT IF EXISTS ', #ename ,'old.id END');
PREPARE stmt FROM #sql_text;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
You have to use PREPARE statement, because your event name is dynamic, based on the deleted record id that would be accessible by old.id
DROP FUNCTION IF EXISTS `opalv3`.`LineRecordTemp` $$
CREATE FUNCTION `opalv3`.`LineRecordTemp` ( TABLE_ID INT, COLUMN_DATA TEXT) RETURNS INT
BEGIN
SET #sql= NULL;
SELECT CONCAT('UPDATE TABLE SET c_name = \'',ATTR_DATA,'\' WHERE t_id =',TABLE_ID) INTO #sql;
PREPARE stmt FROM #sql ;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
RETURN 0;
END $$
I am getting the error "Dynamic SQL is not allowed in stored function or trigger"
Is there any way through which I can run the Dynamic query from the MYSQL function
How to find nearest value from mysql database table of give value, what i want to do is:
CASE1: if i am giving input 2500 to pincode then i am retrieving record corresponding to that pincode value from database.
CASE2: if record not exits for entered pincode(2500) , i have to get nearest value of 2500 and its corresponding records too.
Stored procedure i am currently calling for CASE1 sp_findlocation('XXXXXXXX','2500',#addresss)
DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `sp_findlocation`$$
CREATE PROCEDURE `sp_findlocation`(IN mobile VARCHAR(20),IN pincode VARCHAR(20),OUT address VARCHAR(100))
BEGIN
IF(pincode IS NOT NULL AND pincode!= '') THEN
SET #c1 = '';
SET #c2 = '';
SET #c3 = '';
SET #qry='select locality,store_name,contact_number INTO #c1,#c2,#c3 from test_upload where pin=? limit 1';
PREPARE stmt FROM #qry;
SET #c = pincode;
EXECUTE stmt USING #c;
DEALLOCATE PREPARE stmt;
IF(#c1!='' || #c2!=''||#c3!='') THEN
SET address= CONCAT(#c1, '|',#c2,'|',#c3);
ELSE
SET address= "No Result";
END IF;
ELSE
SET address="Please enter valid pin code";
END IF;
END$$
DELIMITER ;
what i will do for geting records including CASE2?
I have created procedure as below. I am passing table name and limit along with username,
DELIMITER $$
DROP PROCEDURE IF EXISTS `GetPosteingang`$$
CREATE PROCEDURE `GetPosteingang`
(
IN stlimit INT,
IN tblname VARCHAR(100),
IN userId INT
)
BEGIN
DECLARE t1 VARCHAR(5000);
SET #t1 =
"SELECT msg_id,msg_from_name as fromname,msg_titel as title,msg_date as date,msg_gelesen,msg_replied,msg_nodel,'msg' as type
FROM "+#tblname+"
UNION
SELECT files_id as msg_id,from_username as fromname,files_oname as title,files_time as date,files_name as msg_gelesen,files_extension as msg_replied,files_filesize as msg_nodel,'file' as type
FROM community_files
WHERE user_id = "+#userId+"
ORDER BY date DESC
LIMIT "+#stlimit+",30";
#SET #t1 =CONCAT('SELECT * FROM ',tab_name );
PREPARE STMT FROM #t1;
EXECUTE STMT;
DEALLOCATE PREPARE STMT;
END $$
DELIMITER ;
But when I call this,
CALL GetPosteingang('1','community_msgin8','658468');
It is giving me error,
CALL GetPosteingang('1','community_msgin8','658468') 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 'NULL' at line 1
Please help me out.
+ is not a string concatenation operator—use MySQL's CONCAT() function instead;
user-defined (session) variables (which are prefixed with #) are different to stored-program/declared variables (which have no prefix);
beware of your procedure being called with a tblname that results in SQL injection;
column names in a UNION are determined by the first query; and
parameterise your prepared statement, where possible.
Therefore:
CREATE PROCEDURE `GetPosteingang`
(
IN stlimit INT,
IN tblname VARCHAR(100),
IN userId INT
)
BEGIN
SET #t1 := CONCAT("
SELECT msg_id,
msg_from_name AS fromname,
msg_titel AS title,
msg_date AS date,
msg_gelesen,
msg_replied,
msg_nodel,
'msg' AS type
FROM `", REPLACE('`','``',tblname), "`
UNION
SELECT files_id,
from_username,
files_oname,
files_time,
files_name,
files_extension,
files_filesize,
'file' as type
FROM community_files
WHERE user_id = ?
ORDER BY date DESC
LIMIT ?, 30
", #t2 := userId, #t3 := stlimit;
PREPARE stmt FROM #t1;
EXECUTE stmt USING #t2, #t3;
DEALLOCATE PREPARE stmt;
END$$
However, all that said, having a variable table name is a strong indicator that your schema is badly denormalised—consider merging all such tables together into a single one with columns that identify their differences.
The code above is missing the end parenthesis for the CONCAT function. So it should be this:
CREATE PROCEDURE `GetPosteingang`
(
IN stlimit INT,
IN tblname VARCHAR(100),
IN userId INT
)
BEGIN
SET #t1 := CONCAT("
SELECT msg_id,
msg_from_name AS fromname,
msg_titel AS title,
msg_date AS date,
msg_gelesen,
msg_replied,
msg_nodel,
'msg' AS type
FROM `", REPLACE('`','``',tblname), "`
UNION
SELECT files_id,
from_username,
files_oname,
files_time,
files_name,
files_extension,
files_filesize,
'file' as type
FROM community_files
WHERE user_id = ?
ORDER BY date DESC
LIMIT ?, 30
"), #t2 := userId, #t3 := stlimit;
PREPARE stmt FROM #t1;
EXECUTE stmt USING #t2, #t3;
DEALLOCATE PREPARE stmt;
END$$