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的一些常用用法的差别的更多相关文章

  1. SqlServer与MySql的一些常用用法的差别

    最近学习了一下mySql,总结一下SqlServer不同一些用法: 操作符优先级以下列表显示了操作符优先级的由低到高的顺序.排列在同一行的操作符具有相同的优先级.:=||, OR, XOR&& ...

  2. 日期时间函数 mysql 和sqlserver 中对于常用函数的日期和时间函数的区别

    1. sqlserver中获取时间用getdate(),默认返回格式是2019-01-21 13:58:33.053,具体的年月日,时分秒毫米,年月日之间用短线连接,时分秒之间用冒号连接,秒和毫米之间 ...

  3. 字符串函数 mysql 和sqlserver 中对于字符串的常用函数和区别

    1. 对于字符串大小写的统一 mysql和sqlserver中都有同名函数lower()和upper(),但是mysql中还有另外一对函数,达到同样的目的,lcase()和ucase(),也就是英文中 ...

  4. MySQL的mysqldump工具的基本用法

    导出要用到MySQL的mysqldump工具,基本用法是:    shell> mysqldump [OPTIONS] database [tables]    如果你不给定任何表,整个数据库将 ...

  5. spring boot 配置双数据源mysql、sqlServer

    背景:原来一直都是使用mysql数据库,在application.properties 中配置数据库信息 spring.datasource.url=jdbc:mysql://xxxx/test sp ...

  6. iptables-25个常用用法【转】

    本文介绍25个常用的iptables用法.如果你对iptables还不甚了解,可以参考上一篇iptables详细教程:基础.架构.清空规则.追加规则.应用实例,看完这篇文章,你就能明白iptables ...

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

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

  8. Mysql和sqlServer命令比较

    http://cool.china.blog.163.com/blog/static/697310642010111202531210 Mysql和sqlServer命令比较 按语句功能划分,依次讲解 ...

  9. MySQL replace 和 replace into 的用法

    mysql replace实例说明: UPDATE tb1 SET f1=REPLACE(f1, 'abc', 'def'); REPLACE(str,from_str,to_str) 在字符串 st ...

随机推荐

  1. .Net需要掌握的知识

    一.C#开发 1.C#基础 变量定义 如何变量的初始化 变量的作用域 常量 字符串处理 使用正则表达式 什么是CTS类型?数据类型如何分类以及各个数据类型范围 类型的转化分类 显式转换何隐式转化如何区 ...

  2. 深度信任网络的快速学习算法(Hinton的论文)

    也没啥原创,就是在学习深度学习的过程中丰富一下我的博客,嘿嘿. 不喜勿喷! Hinton是深度学习方面的大牛,跟着大牛走一般不会错吧-- 来源:A fast learning algorithm fo ...

  3. Linux netmask

    wget http://mirrors.sohu.com/ubuntu/pool/universe/n/netmask/netmask_2.3.12.tar.gz tar xf netmask_2.3 ...

  4. Ant build ${renderscript.opt.level}问题解决方案

    问题如下: BUILD FAILEDD:\adt-bundle-windows-x86_64-20131030\sdk\tools\ant\build.xml:653: The following e ...

  5. javascript中怎么让一个页面执行多个window.onload?

    我们都知道在javascript中window.onload 只能有一个如果有多个的话后面的会覆盖前面的,今天我们来看看怎么让一个页面执行多个window.onload <script type ...

  6. javascript学习-原生javascript的小特效(改变透明度效果)

    前些日子看了个视频所以就模仿它的技术来为大家做出几个简单的JS小特效 ( 以下效果兼容IE5+ IE5-以下没有测试哦) 今天为大家演示的效果是一个div,鼠标经过的时候透明度为100%,鼠标移出的时 ...

  7. MediaPlayer的生命周期

  8. FSL安装

    本文介绍如何安装医学图像处理软件fsl. 安装环境:Win8和Ubuntu 14.04 LTS(双系统和虚拟机皆可) 1. 安装ubuntu 14.04 LTS 2. 打开终端,设置ip代理:expo ...

  9. Linux runlevel 运行级别

    runlevel可以认为是系统状态,形象一点,您可以认为runlevel有点象微软的windows操作系统中的Normal,safemode,和Command prompt only. Linux系统 ...

  10. java语法糖3 深入剖析Java中的装箱和拆箱

    装箱 在Java SE5之前,如果要生成一个数值为10的Integer对象,必须这样进行: Integer i = new Integer(10); 而在从Java SE5开始就提供了自动装箱的特性, ...