`

MySql与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 = @UserGuid


declare @productId int
declare @quantity int
declare tempCartCursor cursor for
select ProductId, Quantity from @tempShoppingCart

open tempCartCursor
fetch next from tempCartCursor into @productId, @quantity
while  @@FETCH_STATUS = 0
begin
update Product set SellCount = SellCount + @quantity where productId = @productId

fetch next from tempCartCursor into @productId, @quantity
end

close 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 ) do
fetch _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
begin
    
declare @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
)
begin

   set @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与SqlServer的一些常用用法的差别[借鉴].pdf

    MySql与SqlServer的一些常用用法的差别[借鉴].pdf

    sqlServer ,Oracle,mysql驱动包

    里面整理了常用数据库的驱动包 sqlServer mysql Oracle.并有三个baseDao,使用jdbc的方式连接数据库。并封装了常用的增删查改的方法。 代码示例 private static final String driver="com.mysql.jdbc.Driver"; //...

    mysql、sqlserver、oracle分页,java分页统一接口实现

    MySQL、SQL Server、Oracle 分页是关系数据库管理系统中最基本也是最常用的操作之一,而 Java 分页统一接口实现则是对数据库操作的抽象和封装。本文将对 MySQL、SQL Server、Oracle 分页的实现进行详细的介绍,并...

    SQLserver2016-数据库系统概述.pptx

    实体: 属性: 联系: 教师 讲授 n 课程 1 姓名 职称 课程名 称 学分 概念层数据模型最常用的方法 SQLserver2016-数据库系统概述全文共16页,当前为第11页。 层次 模型 网状 模型 关系 模型 面向对象 模型 组织层...

    MySQL数据库转移,access,sql server 转 MySQL 的图文教程

    MySQL 是一个广泛使用的关系型数据库管理系统,而 Access 和 SQL Server 是另外两个常用的数据库管理系统。在某些情况下,我们需要将数据从 Access 或 SQL Server 转移到 MySQL。这篇教程将指导您如何使用 ODBC ...

    常用SQL 语句大全

    9、in 的使用方法 10、两张关联表,删除主表中已经在副表中没有的信息 11、四表联查问题 12、日程安排提前五分钟提醒 13、一条sql 语句搞定数据库分页 14、前10条记录 15、选择每组中的最大数 第三部分、 技巧...

    MySql 5.1 参考手册.chm

    1.8.5. MySQL与标准SQL的差别 1.8.6. MySQL处理约束的方式 2. 安装MySQL 2.1. 一般安装问题 2.1.1. MySQL支持的操作系统 2.1.2. 选择要安装的MySQL分发版 2.1.3. 怎样获得MySQL 2.1.4. 通过MD5校验和或GnuPG验证...

    MySQL 5.1中文手冊

    1.8.5. MySQL与标准SQL的差别 1.8.6. MySQL处理约束的方式 2. 安装MySQL 2.1. 一般安装问题 2.1.1. MySQL支持的操作系统 2.1.2. 选择要安装的MySQL分发版 2.1.3. 怎样获得MySQL 2.1.4. 通过MD5校验和或GnuPG验证...

    MySQL 5.1参考手册

    1.8.5. MySQL与标准SQL的差别 1.8.6. MySQL处理约束的方式 2. 安装MySQL 2.1. 一般安装问题 2.1.1. MySQL支持的操作系统 2.1.2. 选择要安装的MySQL分发版 2.1.3. 怎样获得MySQL 2.1.4. 通过MD5校验和或GnuPG...

    SQL Server 数据库索引其索引的小技巧

    一、什么是索引 减少磁盘I/O和逻辑读次数的最佳方法之一就是使用【索引】 索引允许SQL Server在表中查找数据而不需要扫描整个表。 1.1、索引的好处: 当表没有聚集索引时,成为【堆或堆表】 【堆】是一堆未加工的...

    MySQL 5.1官方简体中文参考手册

    1.8.5. MySQL与标准SQL的差别 1.8.6. MySQL处理约束的方式 2. 安装MySQL 2.1. 一般安装问题 2.1.1. MySQL支持的操作系统 2.1.2. 选择要安装的MySQL分发版 2.1.3. 怎样获得MySQL 2.1.4. 通过MD5校验和或GnuPG验证...

    MySQL 5.1参考手册中文版

    1.8.5. MySQL与标准SQL的差别 1.8.6. MySQL处理约束的方式 2. 安装MySQL 2.1. 一般安装问题 2.1.1. MySQL支持的操作系统 2.1.2. 选择要安装的MySQL分发版 2.1.3. 怎样获得MySQL 2.1.4. 通过MD5校验和或GnuPG...

    MySQL 5.1参考手册 (中文版)

    1.8.5. MySQL与标准SQL的差别 1.8.6. MySQL处理约束的方式 2. 安装MySQL 2.1. 一般安装问题 2.1.1. MySQL支持的操作系统 2.1.2. 选择要安装的MySQL分发版 2.1.3. 怎样获得MySQL 2.1.4. 通过MD5校验和或GnuPG验证...

    mysql5.1中文手册

    MySQL与标准SQL的差别 1.8.6. MySQL处理约束的方式 2. 安装MySQL 2.1. 一般安装问题 2.1.1. MySQL支持的操作系统 2.1.2. 选择要安装的MySQL分发版 2.1.3. 怎样获得MySQL 2.1.4. 通过MD5校验和...

    MYSQL中文手册

    1.8.5. MySQL与标准SQL的差别 1.8.6. MySQL处理约束的方式 2. 安装MySQL 2.1. 一般安装问题 2.1.1. MySQL支持的操作系统 2.1.2. 选择要安装的MySQL分发版 2.1.3. 怎样获得MySQL 2.1.4. 通过MD5校验和或GnuPG...

    mysql官方中文参考手册

    1.8.5. MySQL与标准SQL的差别 1.8.6. MySQL处理约束的方式 2. 安装MySQL 2.1. 一般安装问题 2.1.1. MySQL支持的操作系统 2.1.2. 选择要安装的MySQL分发版 2.1.3. 怎样获得MySQL 2.1.4. 通过MD5校验和或GnuPG验证...

Global site tag (gtag.js) - Google Analytics