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
Related
In the procedure I'm writing I need to store the result of a dynamic SQL query into a local variable. Something like this:
DELIMITER //
CREATE PROCEDURE test_maxsum(uid varchar(25))
BEGIN
DECLARE maxsum int;
SET #SQL = CONCAT('SELECT MAX(sum_val) FROM ', uid);
PREPARE stmt FROM #SQL;
EXECUTE stmt INTO maxsum;
DEALLOCATE PREPARE stmt;
SELECT maxsum;
END//
DELIMITER ;
Local variables cannot be assigned in prepared statements. You can use user-defined variables though:
DELIMITER //
CREATE PROCEDURE test_maxsum(uid varchar(25))
BEGIN
SET #SQL = CONCAT('SELECT MAX(sum_val) INTO #maxsum FROM ', uid);
PREPARE stmt FROM #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT #maxsum;
END//
DELIMITER ;
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, ? as value1, ? as value2 FROM tableName WHERE id=1 ');
PREPARE stmt FROM #Query;
EXECUTE stmt USING #v1, #v2;
DEALLOCATE PREPARE stmt;
While reading values in ASP.Net the value in byte array.
ERROR
Exception: Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'.
How can I get the value of #v1 and #v2.
Please help me.
The value of #v2 get NULL from first query that why the second query stored procedure query not return any thing.
so check the value IS NULL by
IF #v2 IS NULL THEN
SET #v2 = 0;
END IF;
Then stored procedure return the result set
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.
can i somehow set parameter for name of table in query without using a prepare statement?
This is example:
SET #tableName = 'Customer';
SELECT * FROM #tableName;
Thanks
Depending on the version of MySQL you are using, you may be able to use something like:
SET #tableName = 'Customer';
SET #s = CONCAT('SELECT * FROM ', #tableName);
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
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).