博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql 存储过程项目小结
阅读量:6072 次
发布时间:2019-06-20

本文共 3467 字,大约阅读时间需要 11 分钟。

1. false :0  true 1 切记

官方文档:http://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html

  •  , 

    These types are synonyms for . A value of zero is considered false. Nonzero values are considered true:

    mysql> SELECT IF(0, 'true', 'false');+------------------------+| IF(0, 'true', 'false') |+------------------------+| false                  |+------------------------+mysql> SELECT IF(1, 'true', 'false');+------------------------+| IF(1, 'true', 'false') |+------------------------+| true                   |+------------------------+mysql> SELECT IF(2, 'true', 'false');+------------------------+| IF(2, 'true', 'false') |+------------------------+| true                   |+------------------------+

    However, the values TRUE and FALSE are merely aliases for 1 and 0, respectively, as shown here:

    mysql> SELECT IF(0 = FALSE, 'true', 'false');+--------------------------------+| IF(0 = FALSE, 'true', 'false') |+--------------------------------+| true                           |+--------------------------------+mysql> SELECT IF(1 = TRUE, 'true', 'false');+-------------------------------+| IF(1 = TRUE, 'true', 'false') |+-------------------------------+| true                          |+-------------------------------+mysql> SELECT IF(2 = TRUE, 'true', 'false');+-------------------------------+| IF(2 = TRUE, 'true', 'false') |+-------------------------------+| false                         |+-------------------------------+mysql> SELECT IF(2 = FALSE, 'true', 'false');+--------------------------------+| IF(2 = FALSE, 'true', 'false') |+--------------------------------+| false                          |+--------------------------------+

    The last two statements display the results shown because 2 is equal to neither 1 nor 0.

2 存储过程中执行动态sql

官方文档:

http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html

The first example shows how to create a prepared statement by using a string literal to supply the text of the statement:

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;

The second example is similar, but supplies the text of the statement as a user variable:

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;

Here is an additional example which demonstrates how to choose the table on which to perform a query at runtime, by storing the name of the table as a user variable:

mysql> USE test;mysql> CREATE TABLE t1 (a INT NOT NULL);mysql> INSERT INTO t1 VALUES (4), (8), (11), (32), (80);mysql> SET @table = 't1';mysql> SET @s = CONCAT('SELECT * FROM ', @table);mysql> PREPARE stmt3 FROM @s;mysql> EXECUTE stmt3;+----+| a  |+----+|  4 ||  8 || 11 || 32 || 80 |+----+mysql> DEALLOCATE PREPARE stmt3;

 

总结:

执行动态sql,分三步走:

1. PREPARE;

 Syntax:

PREPARE stmt_name FROM preparable_stmt

2. EXECUTE;

   Syntax:

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

3. DEALLOCATE PREPARE;

  Syntax:

{DEALLOCATE | DROP} PREPARE stmt_name

 3. 存储过程中的事务

 语法:

START TRANSACTION [WITH CONSISTENT SNAPSHOT]BEGIN [WORK]COMMIT [WORK] [AND [NO] CHAIN] [[NO] RELEASE]ROLLBACK [WORK] [AND [NO] CHAIN] [[NO] RELEASE]SET autocommit = {
0 | 1}

 

转载地址:http://vuigx.baihongyu.com/

你可能感兴趣的文章
三位对我影响最大的老师
查看>>
一个gulp用于开发与生产的示例
查看>>
2015区域赛起航
查看>>
服务器电脑名称改后,需要修改那些内容。
查看>>
第186天:js深入理解构造函数和原型对象
查看>>
页面自动刷新
查看>>
职业规划
查看>>
ansible小结
查看>>
银联支付Java开发
查看>>
最小编辑距离
查看>>
++a和--a以及a++和a--
查看>>
ios--控件--自定义封装一个控件
查看>>
第一章 Oracle10g数据库新特性
查看>>
linux之DNS服务
查看>>
仿射变换详解 warpAffine
查看>>
字符串的属性和方法的调用
查看>>
Genymotion虚拟机启动时get no IP address的解决方法汇总
查看>>
HTML5之tabindex属性
查看>>
分页查询和redis
查看>>
windwos下开发的php上传至linux服务器下需要注意些什么问题?
查看>>