Insert query on procedure - mysql

I have a procedure like this
set #dt=now();
SET #query = CONCAT('insert into tbl_Name (Name , Created_Date , Created_By) values (?,?,?)');
PREPARE stmt FROM #sql;
EXECUTE stmt using #tbl_column_value, #dt, #tbl_user_id;
END IF;
DEALLOCATE PREPARE stmt;
im getting this error
java.sql.SQLException: Incorrect arguments to EXECUTE

Related

why does the in paratemers can not be used in PREPARE statement

I am studying mysql. Here is my code of practicing stored procedure.
CREATE DEFINER=`raxuser`#`%` PROCEDURE `test`(in fc_frm_date char(5), in fc_to_date char(5))
BEGIN
declare frm_date char(5);
declare to_date char(5);
select fc_frm_date into #frm_date;
select fc_to_date into #to_date;
select distinct #frm_date, #to_date, fc_frm_date, fc_to_date;
set #s = CONCAT('select distinct #frm_date, #to_date, fc_frm_date, fc_to_date;');
PREPARE stmt FROM #s; EXECUTE stmt; DEALLOCATE PREPARE stmt;
END
When I run it , the first select query get correct while the second one shows error code as
Error Code: 1054 Unknown column 'fc_frm_date' in 'field list'
I thought the two select queries should be equal but not. Can anyone help to explain?
Many thanks.
I think the right way to do this (UNTESTED) is:
set #s = CONCAT('select distinct ? , ? , ? , ?;');
PREPARE stmt FROM #s;
EXECUTE stmt USING #frm_date, #to_date, fc_frm_date, fc_to_date;
DEALLOCATE PREPARE stmt;
Try it:
CREATE DEFINER=`raxuser`#`%` PROCEDURE `test`(fc_frm_date char(5), fc_to_date char(5))
BEGIN
DECLARE frm_date CHAR(5);
DECLARE to_date CHAR(5);
SELECT fc_frm_date INTO #frm_date;
SELECT fc_to_date INTO #to_date;
SELECT fc_frm_date INTO #fc_frm_date;
SELECT fc_to_date INTO #fc_to_date;
SET #s = 'select distinct #frm_date, #to_date, #fc_frm_date, #fc_to_date';
PREPARE stmt FROM #s; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SELECT #s;
END

SUBSTRING_INDEX and MySQL Stored Procedures

I am finding it hard to get a MySQL stored procedure to work when I use the SUBSTRING_INDEX and MID and LOCATE functions. The first Select I have works, the other 2 do not. Any help in getting this to work would be awesome.
CREATE DEFINER=`root`#`localhost` PROCEDURE `Address`(TBL VARCHAR(50))
BEGIN
SET #table_name = TBL;
SELECT CONCAT("UPDATE kalo.",#table_name,' SET AD1 = REPLACE(AD1,"."," ")') INTO #SQL;
PREPARE stmt FROM #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT CONCAT("UPDATE kalo.",#table_name,' SET STNO = SUBSTRING_INDEX(AD1, ' ', +1)') INTO #SQL;
PREPARE stmt FROM #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT CONCAT("UPDATE kalo.",#table_name,' SET STNM = MID(AD1, LOCATE(' ', AD1) + 1)') INTO #SQL;
PREPARE stmt FROM #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END

Error Code: 1193. Unknown system variable 'sys_time_in_current_table'

i am getting following error, while i have tried on google,but didn't find the solution.
Mysql workbench version is 6.2;
Mysql version is 5.6.21.
CREATE DEFINER=`developer`#`%` PROCEDURE `sp_tr069_update_performance_performanceinventory_test`(
in temp_db varchar(256),
in nocout_db varchar(256)
)
BEGIN
declare sys_time_in_current_table int ;
declare sys_time_in_perf_inventory_status int;
set #temporary_query = concat('set sys_time_in_current_table = (select sys_timestamp from ',temp_db,'.','performance_performanceinventory limit 1 )');
PREPARE stmt from #temporary_query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
set #temporary_query1 = concat('set sys_time_in_perf_inventory_status = (select sys_timestamp from ',nocout_db,'.performance_inventorystatus order by id desc limit 1) ;');
PREPARE stmt1 from #temporary_query1;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
set #temporary_query=NULL;
-- set #temporary_query=concat();
-- prepare stmt from ;
select `sys_time_in_current_table`,`sys_time_in_perf_inventory_status`;
IF `sys_time_in_current_table`>`sys_time_in_perf_inventory_status`
THEN
select 1;
set #temporary_query2 =
concat('UPDATE ', nocout_db,'.`performance_inventorystatus` inv_status
INNER JOIN ', temp_db,'.`performance_performanceinventory` perf_inv on
inv_status.device_name=perf_inv.device_name and
inv_status.service_name=perf_inv.service_name and
inv_status.data_source=perf_inv.data_source
SET
inv_status.device_name=perf_inv.`device_name` ,
inv_status.service_name=perf_inv.`service_name` ,
inv_status.machine_name=perf_inv.`machine_name` ,
inv_status.site_name=perf_inv.`site_name` ,
inv_status.ip_address=perf_inv.`ip_address` ,
inv_status.data_source=perf_inv.`data_source` ,
inv_status.severity=perf_inv.`severity` ,
inv_status.current_value=perf_inv.`current_value` ,
inv_status.min_value=perf_inv.`min_value` ,
inv_status.max_value=perf_inv.`max_value` ,
inv_status.avg_value=perf_inv.`avg_value` ,
inv_status.warning_threshold=perf_inv.`warning_threshold` ,
inv_status.critical_threshold=perf_inv.`critical_threshold` ,
inv_status.sys_timestamp=perf_inv.`sys_timestamp` ,
inv_status.check_timestamp=perf_inv.`check_timestamp`;') ;
PREPARE stmt2 from #temporary_query2;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;
set #temporary_query3 = concat('INSERT INTO ',nocout_db,'.`performance_inventorystatus`
(
`device_name`,
`service_name`,
`machine_name`,
`site_name`,
`ip_address`,
`data_source`,
`severity`,
`current_value`,
`min_value`,
`max_value`,
`avg_value`,
`warning_threshold`,
`critical_threshold`,
`sys_timestamp`,
`check_timestamp`)
SELECT
`performance_performanceinventory`.`device_name`,
`performance_performanceinventory`.`service_name`,
`performance_performanceinventory`.`machine_name`,
`performance_performanceinventory`.`site_name`,
`performance_performanceinventory`.`ip_address`,
`performance_performanceinventory`.`data_source`,
`performance_performanceinventory`.`severity`,
`performance_performanceinventory`.`current_value`,
`performance_performanceinventory`.`min_value`,
`performance_performanceinventory`.`max_value`,
`performance_performanceinventory`.`avg_value`,
`performance_performanceinventory`.`warning_threshold`,
`performance_performanceinventory`.`critical_threshold`,
`performance_performanceinventory`.`sys_timestamp`,
`performance_performanceinventory`.`check_timestamp`
FROM ', temp_db,'.`performance_performanceinventory`
left join ', nocout_db,'.performance_inventorystatus inv_status on
performance_performanceinventory.device_name=inv_status.device_name and
performance_performanceinventory.service_name=inv_status.service_name and
performance_performanceinventory.data_source=inv_status.data_source
where inv_status.device_name is null ;');
PREPARE stmt3 from #temporary_query3;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
set #temporary_query4 = concat('INSERT INTO', nocout_db,'.`performance_performanceinventory`
(
`device_name`,
`service_name`,
`machine_name`,
`site_name`,
`ip_address`,
`data_source`,
`severity`,
`current_value`,
`min_value`,
`max_value`,
`avg_value`,
`warning_threshold`,
`critical_threshold`,
`sys_timestamp`,
`check_timestamp`)
SELECT
`performance_performanceinventory`.`device_name`,
`performance_performanceinventory`.`service_name`,
`performance_performanceinventory`.`machine_name`,
`performance_performanceinventory`.`site_name`,
`performance_performanceinventory`.`ip_address`,
`performance_performanceinventory`.`data_source`,
`performance_performanceinventory`.`severity`,
`performance_performanceinventory`.`current_value`,
`performance_performanceinventory`.`min_value`,
`performance_performanceinventory`.`max_value`,
`performance_performanceinventory`.`avg_value`,
`performance_performanceinventory`.`warning_threshold`,
`performance_performanceinventory`.`critical_threshold`,
`performance_performanceinventory`.`sys_timestamp`,
`performance_performanceinventory`.`check_timestamp`
FROM ', temp_db,'.`performance_performanceinventory` ;');
PREPARE stmt4 from #temporary_query4;
EXECUTE stmt4;
DEALLOCATE PREPARE stmt4;
set #temporary_query5 = concat('Truncate ', temp_db,'.`performance_performanceinventory`');
PREPARE stmt5 from #temporary_query5;
EXECUTE stmt5;
DEALLOCATE PREPARE stmt5;
END IF;
END
When i execute this stored procedure,then its done ....
but its throwing run time error when i call this procedure....
call nocout_bhutan_testing.sp_tr069_update_performance_performanceinventory_test('tr069_temp_machine_1', 'nocout_bhutan_testing');

mysql prepared statment giving error

i am trying to write a stored procedure which updates the field of a table as specified in the argument .i am getting syntax error in #sql statment.please help
create procedure new_upd1(ind int(3),attribute varchar(30),pk int(11),new_value varchar(30))
begin
set #att=attribute;
set #primk=pk;
set #updated=new_value;
if ind=1 then
set #sql='update department set ?=? where departmentid=?';
prepare stmt from #sql;
execute stmt using #att,#updated,#primk;
deallocate prepare stmt;
end if;
end
Parameters don't work for column or table names, only for values. Do it like this:
create procedure new_upd1(ind int(3),attribute varchar(30),pk int(11),new_value varchar(30))
begin
set #att=attribute;
set #primk=pk;
set #updated=new_value;
if ind=1 then
set #sql=CONCAT('update department set ', #att, '=? where departmentid=?;');
prepare stmt from #sql;
execute stmt using #updated,#primk;
deallocate prepare stmt;
end if;
end

MySQL stored procedure return variable values from seconds query?

SET #v1 = '';
SET #v2 = '';
SET #Query = CONCAT('SELECT sum(colName1), sum(colName2) INTO #v1, #v2 FROM tableName WHERE id=1 ');
PREPARE stmt FROM #Query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET #Query = CONCAT('SELECT id, name,',#v1,' as value1, ',#v2,' as value2 FROM tableName WHERE id=1 ');
PREPARE stmt FROM #Query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
While reading values the throw an Exception - Column 'value1' does not belong to table.
How can I get the value of #v1 and #v2.
Please help me.
The #v2 has null value that why the stored procedure gives error.