由于工作的原因:上家公司的数据库全采用MySql,所以不得不用它。因此也学到了MySql的一些知识,但考虑到今后可能没机会使用了,所以想趁现在离职在家休息,打算把这些东西整理一下,也为了万一今后能用上,留个参考的资源。考虑到一直在使用SqlServer,所以就打算直接与SqlServer对比来写。

本文将主要列出MySql与SqlServer不同的地方,且以常用的存储过程的相关内容为主。

1. 标识符限定符

SqlServer []
MySql ``

2. 字符串相加

SqlServer 直接用 +
MySql concat()

3. isnull()

SqlServer isnull()
MySql ifnull()
注意:MySql也有isnull()函数,但意义不一样

4. getdate()

SqlServer getdate()
MySql now()

5. newid()

SqlServer newid()
MySql uuid()

6. @@ROWCOUNT

SqlServer @@ROWCOUNT
MySql row_count()
注意:MySql的这个函数仅对于update, insert, delete有效

7. SCOPE_IDENTITY()

SqlServer SCOPE_IDENTITY()
MySql last_insert_id()

8. if ... else ...

SqlServer
IF Boolean_expression { sql_statement | statement_block }
[ ELSE { sql_statement | statement_block } ] -- 若要定义语句块,请使用控制流关键字 BEGIN 和 END。
MySql
IF search_condition THEN statement_list[ELSEIF search_condition THEN statement_list] ...[ELSE statement_list]
END IF

注意:对于MySql来说,then, end if是必须的。类似的还有其它的流程控制语句,这里就不一一列出。

9. declare

其实,SqlServer和MySql都有这个语句,用于定义变量,但差别在于:在MySql中,DECLARE仅被用在BEGIN ... END复合语句里,并且必须在复合语句的开头,在任何其它语句之前。这个要求在写游标时,会感觉很BT.

10. 游标的写法

SqlServer
declare @tempShoppingCart table (ProductId int, Quantity int)
insert into @tempShoppingCart (ProductId, Quantity)select ProductId, Quantity from ShoppingCart where UserGuid = @UserGuiddeclare @productId int
declare @quantity int
declare tempCartCursor cursor for select ProductId, Quantity from @tempShoppingCartopen tempCartCursor
fetch next from tempCartCursor into @productId, @quantity
while  @@FETCH_STATUS = 0
beginupdate Product set SellCount = SellCount + @quantity    where productId = @productIdfetch next from tempCartCursor into @productId, @quantity
endclose tempCartCursor
deallocate tempCartCursor
MySql
declare m_done int default 0;
declare m_sectionId int;
declare m_newsId int;declare _cursor_SN cursor for select sectionid, newsid from _temp_SN;
declare continue handler for not found set m_done = 1;create temporary table _temp_SN select sectionid, newsid from SectionNews  group by sectionid, newsid having count(*) > 1;open _cursor_SN;
while( m_done = 0 ) dofetch _cursor_SN into m_sectionId, m_newsId;if( m_done = 0 ) then -- 具体的处理逻辑end if;
end while;
close _cursor_SN;
drop table _temp_SN;

注意:为了提高性能,通常在表变量上打开游标,不要直接在数据表上打开游标。

11. 分页的处理

SqlServer
create procedure GetProductByCategoryId( @CategoryID int, @PageIndex int = 0, @PageSize int = 20, @TotalRecords int output
)
as
begindeclare @ResultTable table
( RowIndex int, ProductID int, ProductName nvarchar(50), CategoryID int, Unit nvarchar(10), UnitPrice money, Quantity int
); insert into @ResultTable
select row_number() over (order by ProductID asc) as RowIndex, p.ProductID, p.ProductName, p.CategoryID, p.Unit, p.UnitPrice, p.Quantity
from   Products as p
where CategoryID = @CategoryID; select  @TotalRecords = count(*) from  @ResultTable; select *
from   @ResultTable
where  RowIndex > (@PageSize * @PageIndex) and RowIndex <= (@PageSize * (@PageIndex+1)); end;

当然,SqlServer中并不只有这一种写法,只是这种写法是比较常见而已。

MySql
create procedure GetProductsByCategoryId(in _categoryId int,in _pageIndex int,in _pageSize int,out _totalRecCount int
)
beginset @categoryId = _categoryId;set @startRow = _pageIndex * _pageSize;set @pageSize = _pageSize;prepare PageSql from 'select sql_calc_found_rows * from product  where categoryId = ? order by ProductId desc limit ?, ?';execute PageSql using @categoryId, @startRow, @pageSize;deallocate prepare PageSql;set _totalRecCount = found_rows();end

MySql与SqlServer的差别实在太多,以上只是列出了我认为经常在写存储过程中会遇到的一些具体的差别之处。

去年我将一些MySql的常用函数作了一番整理,如需要请点击此处下载。

转载于:https://www.cnblogs.com/zdh-ry/archive/2012/07/18/2598334.html

MySql与SqlServer的一些常用用法的差别相关推荐

  1. mysql使用方法_Mysql的常用用法

    一.mysql中limit的用法详解[数据分页常用] 在我们使用查询语句的时候,经常要返回前几条或者中间某几行数据,这个时候怎么办呢?不用担心,mysql已经为我们提供了这样一个功能. SELECT  ...

  2. MySQL数据库增删改查常用语句详解

    MySQL数据库增删改查常用语句详解 一 MySQL数据库表结构 1.1 常见数据类型 1.2 常用约束类型 1.3 MySQL存储引擎 二 DDL语句:数据定义语句 2.1 修改数据库密码 2.1. ...

  3. MySQL与SQLServer的区别(一千条语句)

    ER图.分页.差异.Java连接MySQL SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset LIMIT 子句可以被用于强制 ...

  4. mysql将时间戳转成常用可读时间格式

    mysql中一个表的一个时间列是int类型,现在想修改这个字段的值,打算可读日期时间格式转成int,然后修改那个值. 这个转换函数就是UNIX_TIMESTAMP,将可读的时间转换成int类型,具体用 ...

  5. mysql max 命令大全_MySQL常用命令,34道练习题(持续更新中)。

    1.使用SHOW语句找出在服务器上当前存在什么数据库: mysql> SHOW DATABASES; 2.创建一个数据库MYSQLDATA mysql> CREATE DATABASE M ...

  6. python propresql mysql_python数据库操作mysql:pymysql、sqlalchemy常见用法详解

    本文实例讲述了python数据库操作mysql:pymysql.sqlalchemy常见用法.分享给大家供大家参考,具体如下: 相关内容: 使用pymysql直接操作mysql 创建表 查看表 修改表 ...

  7. SQL常用用法相关笔记

    SQL常用用法相关笔记 1).----CAST和CONVERT的用法 SQL中的cast和convert都是用来将一种数据类型的表达式转换为另一种数据类型的表达式. CAST和CONVERT提供相似的 ...

  8. mysql 与sqlserver对比?哪个更好用?

    今日新接到任务,让比对mysql与sqlserver有啥不同,哪个更好用,以便以后做数据库迁移的时候能统计工作量,现在我所在的作坊是用sqlserver给客户安装,大家都知道sqlserver是收费的 ...

  9. MySQL查询分析器EXPLAIN或DESC用法

    MySQL查询分析器EXPLAIN或DESC用法 一.简单例子: MySQL可以通过EXPLAIN或DESC来查看并分析SQL语句的执行情况 mysql> desc one_and_two_kn ...

最新文章

  1. 赠书 | 详解 4 种爬虫技术
  2. linux ls 按 文件名 大小 时间 排序
  3. pat1014. Waiting in Line (30)
  4. dedecms更改php目录名称,dedecms修改专题目录名称(路径)
  5. win10 + 独显 + Anaconda3 + tensorflow_gpu1.13 安装教程(跑bert模型)
  6. php 接口有几种,【后端开辟】php接口有哪些范例?
  7. Django:模板与视图
  8. 第7章[7.18] Ext JS组件嵌入HTML页面
  9. Python知识点汇总
  10. [PyTorch] 基于Python和PyTorch的MNIST的手写数字数据集的分类
  11. 我的k8s随笔:Kubernetes 1.17.0 部署讲解
  12. [uoj30][CF Round #278]Tourists——树链剖分+圆方树
  13. ascii转utf8 php,PHP 将ASCII转换为UTF-8编码
  14. 自媒体如何推广?推广的渠道有哪些?
  15. Spark RDD算子(八)mapPartitions, mapPartitionsWithIndex
  16. python之不同公司不同年份同一财务指标比较
  17. iPhone降级刷机
  18. Java 简单计算器(加法)
  19. java 生成印章源码_java代码生成指定的公章和私章,并且解决服务器不能回显文字的问题...
  20. 宿主机kernel识别kvm-vcpu线程的一种方法

热门文章

  1. android ssl http,Android SSL HTTP请求使用自签名证书和CA
  2. Python机器学习:KNN算法05f超参数
  3. c语言第四作业答案,C语言第一次作业及答案
  4. Envi和Arcgis软件冲突的解决方法
  5. 计算机组装策划案,产品策划书格式
  6. win7 path环境变量被覆盖了怎么恢复_系统小技巧:还原Windows10路径环境变量
  7. MySQL的基础操作命令_mysql的基本操作命令
  8. c语言sizeof(test),解析C语言中的sizeof
  9. 用几何语言表示线段ab的中点c,做完这30道精选题,你的几何图形绝对满分!
  10. linux设置板卡时间,嵌入式VS-RK3288板卡 Linux 修改BOOTDELAY介绍