procedure for comparing results of two sql statements - mysql

1)Below is the code I'm trying for a procedure where I need to compare the count from two different tables. (tbl1 and tbl2 are in parameters to be passed).
But in the above code the values for 'a' and 'b' are set as statements. I need the resultant value(count) from the two statements to compare in the 'IF' condition.
declare a integer;
declare b integer;
set #a:=concat('select count(*) from ', tbl1);
/*PREPARE stmt1 FROM #a;
execute stmt1;
deallocate PREPARE stmt1;*/
set #b:= concat('select count(*) from ', tbl2);
/*PREPARE stmt2 FROM #b;
execute stmt2;
deallocate PREPARE stmt2;*/
if
#a=#b
then
select 1;
else
select 2;
end if;
2) Actually where I am facing a problem to set 'tbl1' and 'tbl2' as parameters in procedure.
The below code works fine if the table names are given directly, but i need them as parameters.
CREATE PROCEDURE myproc (in tbl1 varchar(50), in tbl2 varchar(50))
declare a integer;
declare b integer;
set #a:=(select count(*) from tbl1);
/*PREPARE stmt1 FROM #a;
execute stmt1;
deallocate PREPARE stmt1;*/
set #b:= (select count(*) from tbl2);
/*PREPARE stmt2 FROM #b;
execute stmt2;
deallocate PREPARE stmt2;*/
if
#a=#b
then
select 1;
else
select 2;
end if;
Hope anyone out there can help me with the solution.

As mentioned in the comments above, it is likely that you shouldn't be doing this at all... but, for what it's worth:
SET #sql := CONCAT('SELECT (
SELECT COUNT(*) FROM `',REPLACE('`','``',tbl1),'`
) = (
SELECT COUNT(*) FROM `',REPLACE('`','``',tbl2),'`
);');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET #sql := NULL;
REPLACE() has been used to try and avoid SQL injection (it's somewhat crude, but it's the best one can do without further information).

Related

How to set the result of a query as a queryable table?

I am trying to use the result of a query as a table.
This query works fine:
SELECT date, number FROM `table_A`
The query below as well --> its result is table_B as a string of character not the table itself
SELECT nametable FROM `list_repository` WHERE id=1
But the combined one does not:
SELECT date, number FROM (SELECT nametable FROM `list_repository` WHERE id=1) A
I expect the resulting query to be
SELECT date, number FROM `table_B`
I tried to set a variable but it does not work either:
DECLARE x VARCHAR(150) ;
SET table=( SELECT `nametable` FROM `list_repository` WHERE id=1);
SELECT * from `table`. But it would not work
Thank you for your help!
Identifiers (db, table, column names etc) in SQL are static. Therefore you can't populate them at run-time. But you can build a query as a string and execute it via dynamic SQL. Something along the lines of
SET #sql = NULL;
SELECT CONCAT("SELECT * FROM ", nametable)
INTO #sql
FROM list_repository
WHERE id = 1;
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
An example of wrapping it up into a stored procedure
DELIMITER //
CREATE PROCEDURE sp1 (IN id INT)
BEGIN
SET #sql = NULL;
SELECT CONCAT("SELECT * FROM ", nametable)
INTO #sql
FROM list_repository
WHERE id = id;
SELECT #sql;
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END//
And then invoking it
CALL sp1(1);

mysql stored procedure multiple parameters

I am trying to add multiple dynamic columns in the select statement, (stored procedure) But it's not working at my end, Can you please help me on same.
BEGIN SET #col1 = col1;SET #col1 = col2;SET #getID = CONCAT('SELECT ' ,#col1 = col1, #col2 = col2,' FROM',tablename_In,' WHERE `type`= type ORDER BY id DESC LIMIT ?,?'); PREPARE stmt FROM #getID;SET #START =_START; SET #LIMIT = _LIMIT;EXECUTE stmt USING #START, #LIMIT; DEALLOCATE PREPARE stmt; END
Also, I have another stored procedure it's also not working with between query:
BEGIN SET #getID = CONCAT('SELECT count(id) as co FROM ',tablename_In,' WHERE',est_time,' BETWEEN',start_date,'AND',end_date);PREPARE stmt FROM #getID; EXECUTE stmt; DEALLOCATE PREPARE stmt; END

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

Stored procedure output variable

I am trying to run a procedure that takes a parameter 'table' for the query, and result as the output parameter. However, it shows as undeclared variable: result
I have doubled checked that no spelling mistake but still have no idea how it happened. Would someone please provide some help or guidance
CREATE DEFINER=`root`#`localhost` PROCEDURE `Function`(IN table varchar(10), OUT result varchar (10))
BEGIN
SET #q = CONCAT ('
Select `field` from `',table,'` into result limit 1;');
PREPARE stmt from #q;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
Try:
...
-- SET #q = CONCAT ('Select `field` from `',table,'` into result limit 1;');
SET #`q` := CONCAT('SELECT `der`.`field`
FROM (SELECT `field` FROM `', `table`, '` LIMIT 1) `der`,
(SELECT #`result` := NULL) `init`
INTO #`result`;');
PREPARE `stmt` from #`q`;
EXECUTE `stmt`;
SET `result` := #`result`;
DEALLOCATE PREPARE `stmt`;
...
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.
SQL Fiddle demo

MySQL How to get results after PREPARE and EXECUTE in Stored Procedure?

My current code is :
DELIMITER \\
CREATE PROCEDURE sample (IN _car VARCHAR(15))
BEGIN
DECLARE _a INTEGER;
SET #s = CONCAT('SELECT COUNT(*) FROM train WHERE ', _car, '<=0;');
PREPARE stmt1 FROM #s;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
END\\
But I wanted to capture the answer of the SELECT statement to my _a variable.
I tried changing my code to
SET #s = CONCAT('SELECT COUNT(*) INTO', _a,' FROM train WHERE ', _car, '<=0;');
But that didn't work.
Help, please?
SOLVED!
DELIMITER \\
CREATE PROCEDURE sample (IN _car VARCHAR(15))
BEGIN
DECLARE _a INTEGER;
SET #var = NULL;
SET #s = CONCAT('SELECT COUNT(*) INTO #var FROM train WHERE ', _car, '<=0;');
PREPARE stmt1 FROM #s;
EXECUTE stmt1;
SELECT #var;
DEALLOCATE PREPARE stmt1;
END\\
:D
As stated here you need to include the variable assignment in the original statement declaration. So you statement would be something like:
SELECT COUNT(*) FROM train WHERE ?<=0 INTO _a
Then you you would execute it with:
EXECUTE stmt1 using _car;
And get the result with:
select _a;
Let me know if it works.