If I have the output of a prepared statement; how do I use it as the source of my query?
CALL `myProcedure`;
PREPARE stmnt FROM #allSQL;
EXECUTE stmnt;
DEALLOCATE PREPARE stmnt;
So, I get an output from the EXECUTE stmnt; (let's call it tmp) and I would like to run a query along the lines of:
SELECT * FROM (EXECUTE stmnt) AS tmp WHERE this = that;
You could prepare a CREATE TEMPORARY TABLE statement that contains the result of the query:
SET #createSQL = CONCAT('CREATE TEMPORARY TABLE tmp AS ', #allSQL);
PREPARE stmt FROM #createSQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT * FROM tmp WHERE this = that;
Related
if I want to call a procedure with different input in each time input1,input2, input 3, ... and different output out1,out2,...and so on
proc(input1,out1) proc(input2,out2)...
can I made only one prepared statement inside a while loop to do that job? something like this
while c <= 10 DO
SET #sql = CONCAT('call proc(input',c,',out',c')');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
set c=c+1;
end while;
To do point, I have simple query like
SELECT * FROM mytable WHERE concat(firstName, ' ', lastName) in ('Adan Jack');
Query above run smoothly. But how if I combine that condition using Dynamic Query that using concat before?
I did this:
BEGIN
set #cond = concat(concat("firstName"," ", "lastName"), " in ('Adan Jack')";
set #query = concat("SELECT * FROM mytable WHERE ", #cond);
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
But no result and cause error.
Thanks.
Here is the simplified query that doesn't work.
SET #abc = CONCAT('%','string','%');
SET #query = CONCAT('SELECT *
FROM table
WHERE column LIKE ',#abc);
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
I need to use CONCAT with SELECT because there lots of other variables in real query.
Real query works fine when I use some simple COLUMN=xyz in WHERE clause. But nothing works when I try to use LIKE %xyz%...
Use it like this
SET #abc = CONCAT('"%','string','%"');
SET #query = CONCAT('SELECT *
FROM table
WHERE column LIKE ',#abc);
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Check the first line I have added " to show #abc like "%string%"
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;
I have a series of 16 PREPARE, EXECUTE and DEALLOCATE statements (in a stored procedure), each inserting rows into a diffterent table (table 1 to table16). eg:
SET #Command1 = CONCAT("insert into TABLE1" , ...etc.. );
PREPARE stmt1 FROM #Command1 ;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
SET #Command1 = CONCAT("insert into TABLE2" , ...etc.. );
PREPARE stmt1 FROM #Command1 ;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
.
.
.
SET #Command1 = CONCAT("insert into TABLE16" , ...etc.. );
PREPARE stmt1 FROM #Command1 ;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
When I execute the stored procedure, the INSERT works intermittently. Sometimes all the 16 inserts works, but sometimes they don't.
In the last CALL of the stored procedure, the first 2 inserts (into TABLE1 and TABLE2 ) and the last 4 inserts (TABLE 13 to 16) work, but not the inserts into Table 3 to 12.
Can you explain why? Can't be because I'm using the same variable/handle command1 and stmt1?
In MySQL, the combinations of Prepare and Execute commands should be used with placeholders - saving you time with concat strings (maybe one of your concats simply goes wrong) and of course - prevent sql injections (yey!)
So... just build your statements like this one:
-- in MySQL ? is a placeholder
set #sampleQuery = 'select name into #testValue from myTable where id = ?';
set #idParam = 'NT54X9';
prepare sampleStatement from #sampleQuery;
execute sampleStatement using #idParam; -- `using` is the key point here
deallocate prepare sampleStatement;
select #testValue