| 订阅 | 在线投稿
分享
 
 
 

用MySQL实现SQL Server的sp_executesql

2008-08-15 06:29:29 编辑來源:互联网 国际版 评论
 
 
本文为【用MySQL实现SQL Server的sp_executesql】的汉字拼音对照版显示拼音
  congMySQL 5.0 kaishizhichileyigequanxindeSQLjufa

  PREPARE stmt_name FROM preparable_stmt;

  EXECUTE stmt_name [USING @var_name [, @var_name] ...];

  {DEALLOCATE | DROP} PREPARE stmt_name;

  tongguotawomenjiukeyishixianleisi MS SQL de sp_executesql zhixingdongtaiSQLyuju

  tongshiyekeyifangzhizhurushigongji

  weileyouyigeganxingderenshi

  xiamianxiangeijigexiaolizi

  mysql> PREPARE stmt1 FROM 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';

  mysql> SET @a = 3;

  mysql> SET @b = 4;

  mysql> EXECUTE stmt1 USING @a, @b;

  +------------+

  | hypotenuse |

  +------------+

  | 5 |

  +------------+

  mysql> DEALLOCATE PREPARE stmt1;

  mysql> SET @s = 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';

  mysql> PREPARE stmt2 FROM @s;

  mysql> SET @a = 6;

  mysql> SET @b = 8;

  mysql> EXECUTE stmt2 USING @a, @b;

  +------------+

  | hypotenuse |

  +------------+

  | 10 |

  +------------+

  mysql> DEALLOCATE PREPARE stmt2;

  ruguonideMySQL banbenshi 5.0.7 huozhegenggaodenihuankeyizai LIMIT zijuzhong使shiyongtashiliruxiamysql> SET @a=1;mysql> PREPARE STMT FROM "SELECT * FROM tbl LIMIT ?";

  mysql> EXECUTE STMT USING @a;

  mysql> SET @skip=1; SET @numrows=5;

  mysql> PREPARE STMT FROM "SELECT * FROM tbl LIMIT ?, ?";

  mysql> EXECUTE STMT USING @skip, @numrows; 使shiyong PREPARE dejigezhuyidian

  APREPARE stmt_name FROM preparable_stmt;

  yudingyiyigeyujubingjiangtafugei stmt_name stmt_name shibuqufendaxiaoxiede

  B ji使shi preparable_stmt yujuzhongde ? suodaibiaodeshiyigezifuchuanniyebuxuyaojiang ? yongyinhaobaohanqilai

  C ruguoxinde PREPARE yuju使shiyongleyigeyicunzaide stmt_name nameyuanyoudejiangbeilijishifang ji使shizhegexinde PREPARE yujuyinweicuowuerbunengbeizhengquezhixing

  D PREPARE stmt_name dezuoyongyushidangqiankehuduanlianjiehuihuakejian

  E yaoshifangyigeyudingyiyujudeziyuankeyi使shiyong DEALLOCATE PREPARE jufa

  F EXECUTE stmt_name jufazhongruguo stmt_name bucunzaijianghuiyinfayigecuowu

  G ruguozaizhongzhikehuduanlianjiehuihuashimeiyouxianshiditiaoyong DEALLOCATE PREPARE jufashifangziyuanfuwuqiduanhuizijidongshifangta

  H zaiyudingyiyujuzhongCREATE TABLE, DELETE, DO, INSERT, REPLACE, SELECT, SET, UPDATE, hedabufende SHOW jufabeizhichi

  G PREPARE yujubukeyiyongyucunchuguochengzidingyihanshudancong MySQL 5.0.13 kaishitakeyibeiyongyucunchuguochengrengbuzhichizaihanshuzhong使shiyong xiamiangeigeshili CREATE PROCEDURE `p1`(IN id INT UNSIGNED,IN name VARCHAR(11))BEGIN lable_exit: BEGIN SET @SqlCmd = 'SELECT * FROM tA '; IF id IS NOT NULL THEN SET @SqlCmd = CONCAT(@SqlCmd , 'WHERE id=?'); PREPARE stmt FROM @SqlCmd; SET @a = id; EXECUTE stmt USING @a; LEAVE lable_exit; END IF; IF name IS NOT NULL THEN SET @SqlCmd = CONCAT(@SqlCmd , 'WHERE name LIKE ?'); PREPARE stmt FROM @SqlCmd; SET @a = CONCAT(name, '%'); EXECUTE stmt USING @a; LEAVE lable_exit; END IF; END lable_exit;END; CALL `p1`(1,NULL);CALL `p1`(NULL,'QQ');DROP PROCEDURE `p1`;原文
 
 
从MySQL 5.0 开始,支持了一个全新的SQL句法: PREPARE stmt_name FROM preparable_stmt; EXECUTE stmt_name [USING @var_name [, @var_name] ...]; {DEALLOCATE | DROP} PREPARE stmt_name; 通过它,我们就可以实现类似 MS SQL 的 sp_executesql 执行动态SQL语句! 同时也可以防止注入式攻击! 为了有一个感性的认识, 下面先给几个小例子: mysql> PREPARE stmt1 FROM 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse'; mysql> SET @a = 3; mysql> SET @b = 4; mysql> EXECUTE stmt1 USING @a, @b; +------------+ | hypotenuse | +------------+ | 5 | +------------+ mysql> DEALLOCATE PREPARE stmt1; mysql> SET @s = 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse'; mysql> PREPARE stmt2 FROM @s; mysql> SET @a = 6; mysql> SET @b = 8; mysql> EXECUTE stmt2 USING @a, @b; +------------+ | hypotenuse | +------------+ | 10 | +------------+ mysql> DEALLOCATE PREPARE stmt2; 如果你的MySQL 版本是 5.0.7 或者更高的,你还可以在 LIMIT 子句中使用它,示例如下:mysql> SET @a=1;mysql> PREPARE STMT FROM "SELECT * FROM tbl LIMIT ?"; mysql> EXECUTE STMT USING @a; mysql> SET @skip=1; SET @numrows=5; mysql> PREPARE STMT FROM "SELECT * FROM tbl LIMIT ?, ?"; mysql> EXECUTE STMT USING @skip, @numrows; 使用 PREPARE 的几个注意点: A:PREPARE stmt_name FROM preparable_stmt; 预定义一个语句,并将它赋给 stmt_name ,stmt_name 是不区分大小写的。 B: 即使 preparable_stmt 语句中的 ? 所代表的是一个字符串,你也不需要将 ? 用引号包含起来。 C: 如果新的 PREPARE 语句使用了一个已存在的 stmt_name ,那么原有的将被立即释放! 即使这个新的 PREPARE 语句因为错误而不能被正确执行。 D: PREPARE stmt_name 的作用域是当前客户端连接会话可见。 E: 要释放一个预定义语句的资源,可以使用 DEALLOCATE PREPARE 句法。 F: EXECUTE stmt_name 句法中,如果 stmt_name 不存在,将会引发一个错误。 G: 如果在终止客户端连接会话时,没有显式地调用 DEALLOCATE PREPARE 句法释放资源,服务器端会自己动释放它。 H: 在预定义语句中,CREATE TABLE, DELETE, DO, INSERT, REPLACE, SELECT, SET, UPDATE, 和大部分的 SHOW 句法被支持。 G: PREPARE 语句不可以用于存储过程,自定义函数!但从 MySQL 5.0.13 开始,它可以被用于存储过程,仍不支持在函数中使用! 下面给个示例: CREATE PROCEDURE `p1`(IN id INT UNSIGNED,IN name VARCHAR(11))BEGIN lable_exit: BEGIN SET @SqlCmd = 'SELECT * FROM tA '; IF id IS NOT NULL THEN SET @SqlCmd = CONCAT(@SqlCmd , 'WHERE id=?'); PREPARE stmt FROM @SqlCmd; SET @a = id; EXECUTE stmt USING @a; LEAVE lable_exit; END IF; IF name IS NOT NULL THEN SET @SqlCmd = CONCAT(@SqlCmd , 'WHERE name LIKE ?'); PREPARE stmt FROM @SqlCmd; SET @a = CONCAT(name, '%'); EXECUTE stmt USING @a; LEAVE lable_exit; END IF; END lable_exit;END; CALL `p1`(1,NULL);CALL `p1`(NULL,'QQ');DROP PROCEDURE `p1`;
󰈣󰈤
日版宠物情人插曲《Winding Road》歌词

日版宠物情人2017的插曲,很带节奏感,日语的,女生唱的。 最后听见是在第8集的时候女主手割伤了,然后男主用嘴帮她吸了一下,插曲就出来了。 歌手:Def...

兄弟共妻,我成了他们夜里的美食

老钟家的两个儿子很特别,就是跟其他的人不太一样,魔一般的执着。兄弟俩都到了要结婚的年龄了,不管自家老爹怎么磨破嘴皮子,兄弟俩说不娶就不娶,老父母为兄弟两操碎了心...

网络安全治理:国家安全保障的主要方向是打击犯罪,而不是处置和惩罚受害者

来源:中国青年报 新的攻击方法不断涌现,黑客几乎永远占据网络攻击的上风,我们不可能通过技术手段杜绝网络攻击。国家安全保障的主要方向是打击犯罪,而不是处置和惩罚...

 
 
 
>>返回首页<<
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 
 热帖排行
 
清淡自然的清纯模特
倩女幽魂_美丽女生
[i]黄山
雪棉
 
 
王朝网络微信公众号
微信扫码关注本站公众号wangchaonetcn
 
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
©2005- 王朝网络 版权所有