Home  >  Article  >  Database  >  MySQL存储过程中实现执行动态SQL语句的方法_MySQL

MySQL存储过程中实现执行动态SQL语句的方法_MySQL

WBOY
WBOYOriginal
2016-06-01 12:59:25781browse

本文实例讲述了MySQL存储过程中实现执行动态SQL语句的方法。分享给大家供大家参考。具体实现方法如下:

mysql>
mysql> delimiter $$
mysql>
mysql> CREATE PROCEDURE set_col_value
  ->    (in_table   VARCHAR(128),
  ->     in_column  VARCHAR(128),
  ->     in_new_value VARCHAR(1000),
  ->     in_where   VARCHAR(4000))
  ->
  -> BEGIN
  ->  DECLARE l_sql VARCHAR(4000);
  ->  SET l_sql=CONCAT_ws(' ',
  ->        'UPDATE',in_table,
  ->          'SET',in_column,'=',in_new_value,
  ->        ' WHERE',in_where);
  ->  SET @sql=l_sql;
  ->  PREPARE s1 FROM @sql;
  ->  EXECUTE s1;
  ->  DEALLOCATE PREPARE s1;
  -> END$$
Query OK, 0 rows affected (0.00 sec)

希望本文所述对大家的MySQL数据库程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn