The kind of thing you are talking about,
where you execute a text string that is supplied at runtime, is called
dynamic SQL. I've used it in the database which I use most of the time, DB2, but
had never seen it in MySQL when I was making my initial reply to your question.
I just had a look at the MySQL manual to see if it was there, since I was
concerned that I might have simply overlooked it earlier.
As it turns out, I *had* indeed missed the
existence of dynamic SQL in MySQL! Mind you, the MySQL manual uses the term
'prepared statements' instead of 'dynamic SQL'.
You didn't say what version of MySQL you
are running but if you are using 5.0 or later, dynamic SQL is possible for you.
Have a look at http://dev.mysql.com/doc/refman/5.0/en/sqlps.html.
This would appear to be what you originally wanted.
If you are on MySQL 4.1.x, prepared _SQL_
statements are, apparently, not available but you can accomplish the same effect
with the C API. If you look at http://dev.mysql.com/doc/refman/4.1/en/c-api-prepared-statements.html and
the subsequent parts of that chapter, you will see the API
described. Now, I don't know how
you feel about writing statements that aren't SQL but this may offer an
acceptable way for you to do what you want to do. By the same token, Java offers
ways to send text strings to a program at runtime and having them interpreted to
return whatever you want. However, I know that some people want to be able to do
everything in pure SQL without any application code in the picture; if that is
your situation, you may want to upgrade to 5.0 or 5.1 so that you have the
capabilities you need.
Naturally, stored procedures and user
defined functions are still valid ways of solving the problems you
posed.
I apologize profusely for giving you the
impression that what you wanted was not available in MySQL; I simply had never
looked for dynamic SQL capabilities in MySQL and had never stumbled across their
existence in the manual when looking for other things. I spend most of my time
in DB2 and only occasionally work with MySQL and my own copy of MySQL is 4.0.x
which doesn't support prepared statements so I just never knew that they
were there in the later releases.
Sorry for any confusion I
caused!
Rhino
|
No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.1.362 / Virus Database: 267.12.8/162 - Release Date: 05/11/2005
-- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe: http://lists.mysql.com/[EMAIL PROTECTED]