Dynamically construct and execute SQL in StoredProcedure

Dynamically construct and execute SQL in StoredProcedure

am 24.04.2006 15:31:38 von Fred

Hey!

I need to dynamically create an SQL statement within a MySQL 5.0.20
stored procedure.

-- PSEUDO CODE
create procedure DynSQLTest
(
in sTable varchar(45)
)
begin
declare sSQL varchar(45);

select Concat('select * from ',sTable,' where ...) into sSQL;

EXECUTESQL(sSQL);
end;

Is there a possibility to construct the SQL in a varchar and then
execute it? I spent some time googling but did not find anything.

Thank you for any help!

Fred

Re: Dynamically construct and execute SQL in StoredProcedure

am 24.04.2006 21:14:12 von Bill Karwin

Fred wrote:
> Is there a possibility to construct the SQL in a varchar and then
> execute it? I spent some time googling but did not find anything.

See here:
http://dev.mysql.com/doc/refman/5.0/en/sqlps.html

You can do this in stored procedures. See example in this article:
http://dev.mysql.com/tech-resources/articles/mysql-storedpro c.html

Regards,
Bill K.