Mysql procedure insert into temporary table - mysql

I have written a mysql procedure. I want into a temporary table using different select statement. While creating procedure its showing syntax error near at '
SELECT
value as last
FROM
wp_13_rg_lead
LEFT JOIN wp_13_rg_' at line 18
Below is my procedure
DELIMITER $$
CREATE PROCEDURE TEMP_JOIN_WORK ()
BEGIN
start transaction;
CREATE TEMPORARY TABLE IF NOT EXISTS TEMP_JOIN(firstname varchar(500) not null,lastname varchar(500) not null) ;
insert into TEMP_JOIN(SELECT
value
FROM
wp_13_rg_lead
LEFT JOIN wp_13_rg_lead_detail on wp_13_rg_lead.id = wp_13_rg_lead_detail.lead_id
WHERE
wp_13_rg_lead.form_id = 9
AND CAST(wp_13_rg_lead_detail.field_number AS DECIMAL) = CAST(1.3 AS DECIMAL) ,
SELECT
value as lastname
FROM
wp_13_rg_lead
LEFT JOIN wp_13_rg_lead_detail on wp_13_rg_lead.id = wp_13_rg_lead_detail.lead_id
WHERE
wp_13_rg_lead.form_id = 9
AND CAST(wp_13_rg_lead_detail.field_number AS DECIMAL) = CAST(1.6 AS DECIMAL)
);
SELECT * FROM TEMP_JOIN;
commit;
END
$$
DELIMITER;

This should not give syntax error. In workbench, click on create stored procedure and copy the following between BEGIN END. Logic will work or not, that is a separate issue.
CREATE TEMPORARY TABLE IF NOT EXISTS TEMP_JOIN(firstname VARCHAR(500) NOT NULL,lastname VARCHAR(500) NOT NULL) ;
INSERT INTO TEMP_JOIN VALUES((SELECT
`value`
FROM
wp_13_rg_lead
LEFT JOIN wp_13_rg_lead_detail ON wp_13_rg_lead.id = wp_13_rg_lead_detail.lead_id
WHERE
wp_13_rg_lead.form_id = 9
AND CAST(wp_13_rg_lead_detail.field_number AS DECIMAL) = CAST(1.3 AS DECIMAL)) , (
SELECT
`value` AS lastname
FROM
wp_13_rg_lead
LEFT JOIN wp_13_rg_lead_detail ON wp_13_rg_lead.id = wp_13_rg_lead_detail.lead_id
WHERE
wp_13_rg_lead.form_id = 9
AND CAST(wp_13_rg_lead_detail.field_number AS DECIMAL) = CAST(1.6 AS DECIMAL))
);
SELECT * FROM TEMP_JOIN;

Related

mysql syntax error on creating stored procedure

There are tons of similar posts, but still couldn't solve the error.
Here's my stored procedure query:
DROP PROCEDURE IF EXISTS spa_gui_get_next_task;
DELIMITER //
CREATE PROCEDURE `spa_gui_get_next_task`(IN var_user_id INT(11), IN var_task_id INT(11))
BEGIN
-- create temp table
CREATE TEMPORARY TABLE temp_table_user_tasks engine=memory
SELECT
tsk.id,
tsk.request_id,
tsk.sys_index,
tsk.category_group,
tsk.category,
tsk.is_assigned,
tsk.hash_id
FROM
user_tasks as usr
inner join
unassigned_tasks as tsk
on usr.task_id = tsk.id
WHERE
usr.assigned_to = var_user_id
AND
tsk.product_id NOT IN ( SELECT product_id FROM product_progresses WHERE request_id = tsk.request_id )
AND
BINARY hash_id NOT IN ( SELECT hash_id FROM product_match_unmatches WHERE request_id = tsk.request_id AND (is_matched=1 OR auto_unmatched_by IS NOT NULL) )
ORDER BY tsk.id;
-- fetch next row
SELECT qs.*
FROM temp_table_user_tasks as qs
WHERE qs.id = ( SELECT min(qs.id) FROM temp_table_user_tasks as qt WHERE qt.id > var_task_id);
-- drop temp table
DROP TEMPORARY TABLE IF EXISTS temp_table_user_tasks;
END //
DELIMITER ;
I am getting this error:
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 'DELIMITER //
CREATE PROCEDURE `spa_gui_get_next_task`(IN var_user_id INT(11), ' at line 1
Also the same query is working on phpmyadmin 10.1.26-MariaDB, and even tried exporting the same stored procedure from phpmyadmin to another mysql v5.7.12.

How to Create Trigger After Insert with select data from Multiple table with Join in MYSql

I am trying to Create Trigger in MySQL with Select Column data from Join multiple table. But Trigger is not allow me to DECLARE temp variable.
I would like to join 4 table on the bases of newly inserted record in one table and select the data from different table and insert r update in another table (DashboardStatus)
I am getting error [ SQL 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 'SET _BedId = (SELECT bd.BedId
FROM LifetouchHeartRate lthr
JOIN Device' at line 4 */ ]
CREATE TABLE `dashboardstatus` (
`LTHR` INT(11) NULL DEFAULT NULL,
`BedId` INT(11) NULL DEFAULT NULL
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
CREATE TRIGGER triggerDashboard AFTER INSERT ON LifetouchHeartRate for each row
BEGIN
DECLARE _BedId INT
SET _BedId = (SELECT bd.BedId As _BedId
FROM LifetouchHeartRate lthr
JOIN DeviceSession ds ON ds.DeviceSessionID = lthr.ByDevSessionId
JOIN PatientSession ps ON ps.PatientSessionId = ds.ByPatientSessionId
JOIN PatientDetails pd ON pd.PatientDetailsId = ps.ByPatientId
JOIN BedDetails bd ON bd.BedDetailsId = pd.ByBedId
WHERE lthr.LifeTouchHeartRateID = new.LifeTouchHeartRateID Limit 1 );
IF _BedId > 0
BEGIN
INSERT OR REPLACE INTO DashboardStatus (LTHR, BedId) VALUES ( new.LifeTouchHeartRateID, _BedId)
END
END
Remember ;
...
CREATE TRIGGER triggerDashboard AFTER INSERT ON LifetouchHeartRate for each row
BEGIN
-- DECLARE _BedId INT
DECLARE _BedId INT;
...
UPDATE
DELIMITER //
CREATE TRIGGER triggerDashboard AFTER INSERT ON LifetouchHeartRate
FOR EACH ROW
BEGIN
-- DECLARE _BedId INT
DECLARE _BedId INT;
SET _BedId = (SELECT bd.BedId As _BedId
FROM LifetouchHeartRate lthr
JOIN DeviceSession ds ON ds.DeviceSessionID = lthr.ByDevSessionId
JOIN PatientSession ps ON ps.PatientSessionId = ds.ByPatientSessionId
JOIN PatientDetails pd ON pd.PatientDetailsId = ps.ByPatientId
JOIN BedDetails bd ON bd.BedDetailsId = pd.ByBedId
WHERE lthr.LifeTouchHeartRateID = new.LifeTouchHeartRateID Limit 1 );
/*IF _BedId > 0
BEGIN
INSERT OR REPLACE INTO DashboardStatus (LTHR, BedId) VALUES ( new.LifeTouchHeartRateID, _BedId)
END
END*/
IF _BedId > 0 THEN
-- BEGIN
INSERT INTO DashboardStatus (LTHR, BedId) VALUES ( new.LifeTouchHeartRateID, _BedId);
-- END
END IF;
END//
DELIMITER ;
SQL Fiddle demo

how to create a stored procedure containing request where not exists in mysql database

I have a problem with the request "where not exists"
error is :
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 'WHERE NOT EXISTS(SELECT 1 FROM users (WHERE Matricule =
#Matricule AND mot_d' at line 11
here is my procedure
create procedure usp_testInsert(
Matricule int,
mot_de_passe varchar(10),
Nom varchar(10) ,
Prenom varchar(10)
) begin
INSERT INTO users (
Matricule,
mot_de_passe,
Nom,
Prenom)
SELECT
#Matricule,
#mot_de_passe,
#Nom,
#Prenom
WHERE
NOT EXISTS (
SELECT 1
FROM users (
WHERE
Matricule = #Matricule AND
mot_de_passe = #mot_de_passe AND
Nom = #Nom AND Prenom = #Prenom
)
);
end
Some problems in your stored procedure:
It is important to indicate the difference between 9.4. User-Defined Variables and routine parameters 13.1.15. CREATE PROCEDURE and CREATE FUNCTION Syntax, are different variables.
Avoid naming parameters or variables as columns of your tables.
Syntax:
DELIMITER //
create procedure usp_testInsert (
Matricule int,
mot_de_passe varchar(10),
Nom varchar(10) ,
Prenom varchar(10)
)
begin
INSERT INTO users (Matricule, mot_de_passe, Nom, Prenom)
SELECT #Matricule, #mot_de_passe, #Nom, #Prenom
-- FROM ???
WHERE NOT EXISTS (
SELECT 1
/*
FROM users (
WHERE Matricule = #Matricule AND
*/
FROM users
WHERE (
Matricule = #Matricule AND
mot_de_passe = #mot_de_passe AND
Nom = #Nom AND
Prenom = #Prenom
)
);
end//
DELIMITER ;
UPDATE
My version, which certainly will not work, but will help you solve your problem.
DELIMITER //
DROP PROCEDURE IF EXISTS `usp_testInsert`//
CREATE PROCEDURE `usp_testInsert` (
`_Matricule` INT,
`_mot_de_passe` VARCHAR(10),
`_Nom` VARCHAR(10) ,
`_Prenom` VARCHAR(10)
)
BEGIN
INSERT INTO `users` (`Matricule`, `mot_de_passe`, `Nom`, `Prenom`)
SELECT `_Matricule`, `_mot_de_passe`, `_Nom`, `_Prenom`
FROM DUAL
WHERE NOT EXISTS (
SELECT 1
FROM `users`
WHERE (
`Matricule` = `_Matricule` AND
`mot_de_passe` = `_mot_de_passe` AND
`Nom` = `_Nom` AND
`Prenom` = `_Prenom`
)
);
END//
DELIMITER ;

How to pass value for wherein in procedure of mysql

DELIMITER //
DROP PROCEDURE IF EXISTS salary_ref//# MySQL returned an empty result set (i.e. zero rows).
# MySQL returned an empty result set (i.e. zero rows).
CREATE PROCEDURE salary_ref(con int(11),IN id varchar(120),maxval int(11),minval int(11) , taxo int(11))
BEGIN
DECLARE s VARCHAR(50);
IF con = 1 THEN
SELECT `i` . * , `taxo`.`id` , `t`.`item_id` AS id, `u`.`name` AS user_name, `t`.`value` AS val
FROM (
`taxonomy` AS taxo
)
JOIN `item_taxo` AS t ON `t`.`taxo_id` = `taxo`.`id`
INNER JOIN `items` AS i ON `i`.`id` = `t`.`item_id`
INNER JOIN `users` AS u ON `u`.`id` = `i`.`created_by`
WHERE `t`.`value`
BETWEEN minval
AND maxval
AND `t`.`taxo_id` = taxo
AND `i`.`parent_tag_id` in (id);
ELSE
SELECT `i` . * , `taxo`.`id` , `t`.`item_id` AS id, `u`.`name` AS user_name, `t`.`value` AS val
FROM (
`taxonomy` AS taxo
)
JOIN `item_taxo` AS t ON `t`.`taxo_id` = `taxo`.`id`
INNER JOIN `items` AS i ON `i`.`id` = `t`.`item_id`
INNER JOIN `users` AS u ON `u`.`id` = `i`.`created_by`
WHERE `t`.`value`
BETWEEN minval
AND maxval
AND `t`.`taxo_id` = taxo
AND `i`.`parent_tag_id` in (id);
END IF;
END;
//# MySQL returned an empty result set (i.e. zero rows).
DELIMITER ;
//calling of that
call salary_ref(2,"\'36\',\'50\',\'57\'",17000000,0,7)
this is not work for me.
Following changes would solve the issues.
Change 1:
I suggest to not use same parameter names for stored procedure to represent column names of tables.
Unless you handle them properly there would arise an identifier conflict but does not seem to be there.
Change procedure signature as follows:
CREATE PROCEDURE salary_ref(
_con int(11), IN csv_id_values varchar(120),
_maxval int(11), _minval int(11), _taxo int(11)
)
Change 2:
You are trying to pass comma separated values for id field to use in where clause.
But using escape character won't solve your problem and that is not correct way of using input values.
call salary_ref( 2, '36,50,57', 17000000, 0, 7 )
The CSV value '36,50,57' can be used as is for where clause.
See the suggested Change below.
Change 3:
You can use FIND_IN_SET on CSV values instead of IN in the WHERE clause.
WHERE `t`.`value` BETWEEN _minval AND _maxval
AND `t`.`taxo_id` = _taxo
AND FIND_iN_SET( `i`.`parent_tag_id`, csv_id_values );
And, I think your procedure body is incomplete. Your IF con and ELSE are using the same SELECT statement. It is redundant, unless you change it.

MySQL procedure for searching a table with multiple parameters in multiple columns

I am new to MySQL Procedures and I am trying to write a search procedure for a table.
CREATE PROCEDURE `Search`(
IN in_locality VARCHAR(200),
IN in_type VARCHAR(200),
IN in_city VARCHAR(200)
)
BEGIN
SELECT * FROM property_details MRP INNER JOIN property_image MRPI
ON MRP.basic_id=MRPI.basic_id
WHERE ((in_locality = '') or (MRP.locality = in_locality))
AND ((in_property_type = '') or (MRP.property_type = in_property_type))
AND ((in_city = '') or (MRP.city = in_city))
GROUP BY MRP.id;
END
Now this procedure is working for:
CALL Search('','','','mumbai');
but not for:
CALL Search('','',''mumbai','pune'');
In normal SQL I could use this query for that:
SELECT * FROM property_details where city in('mumbai','chennai')
But I don't how to do this in a procedure.
Your CALL examples have got 4 arguments, while your CREATE PROCEDURE statement has only got 3. This is because you are trying to specify multiple cities. For this you can use the FIND_IN_SET function to specify the input parameters as comma-seperated lists (MySQL Docs for FIND_IN_SET)
Instead try this
CREATE PROCEDURE `Search`(
IN in_locality VARCHAR(255),
IN in_type VARCHAR(255),
IN in_city VARCHAR(255)
)
BEGIN
SELECT
*
FROM property_details MRP INNER JOIN property_image MRPI
ON MRP.basic_id=MRPI.basic_id
WHERE ( in_locality = '' XOR FIND_IN_SET( MRP.locality , in_locality ) )
AND ( in_property_type = '' XOR FIND_IN_SET( MRP.property_type , in_property_type ) )
AND ( in_city = '' XOR FIND_IN_SET( MRP.city , in_city ) )
GROUP BY MRP.id;
END
You can then call this using strings with comma-seperated lists in such as the following examples. I also changed them to XOR's because you don't want it searching for an empty string, and changed all 3 searches to be FIND_IN_SET searches.
CALL Search('','','','mumbai');
CALL Search('','','mumbai,pune');