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 } ] 

-- 若要定义语句块,请使用控制流关键字 BEGINEND

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

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


MySQL和Sql Server的sql语句区别

项目需要做sql server适配,因此需要将所有写的mysql的sql语句转变成sqlserver里面的sql语句。在做适配的过程中,总结出了以下几点关于MySQL和SQLServer的sql语句的不同之处。

1、自增长列的插入:

SQLServer中可以不为自动增长列插入值,

MySQL中需要为自动增长列插入值。

2、获取当前时间函数:

SQLServer写法:getdate()

MySQL写法:now()

3、从数据库定位到表。

Sqlserver写法:库名.dbo.表名 ;或者:库名..表名 (注:中间使用两个点)

select password from Info.dbo.users where userName=’boss’

或者

select password from Info..users where userName=’boss’

mysql写法:库名.表名

select password from Info.users where userName=’boss’

4、判断是否存在某个数据库,若存在,则删除

Sqlserver写法:

IF DB_ID(‘users’) IS NOT NULL

DROP DATABASE users

Mysql写法:

Drop DATABASEif exists users

拓展:若sqlserver数据库正在使用中,删除之前,先要把数据库变成“单一用户”,再删除

ALTER DATABASE users SET SINGLE_USER with ROLLBACK IMMEDIATE IF
DB_ID(‘users’) IS NOT NULL DROP DATABASE users

另附:判断某数据库中是否存在某张表,若存在,则删除

Sqlserver写法:

if exists(select * from sysobjects where name =’Users_test’) drop
table Users_test

Mysql写法:

DROP TABLE IF EXISTS Users_test

5、主键存在,则更新,不存在,则插入

Mysql写法:

INSERT into users (userID,userName,password) VALUES (1,’jmj’,’123’) ON
DUPLICATE KEY UPDATE userName =’jmj’, password =123

Sqlserver没有mysql这样的关键字,只能组合sql语句来实现操作:

if not exists (select userID from users where userID= 1)insert into
users (userID,userName,password) values(1,’jmj’,’123’) else update
users set userName = ’jmj’, password=’123’ where userID = 1

(关于On duplicate key update的两篇文章,推荐给大家!

可遇不可求的Question之SQLServer的INSERT ON DUPLICATE KEY UPDATE语法篇

MySql避免重复插入记录方法(ignore,Replace,ON DUPLICATE KEY UPDATE)

6、符号的使用

mysql对参数可以使用单引号,也可以使用双引号,对字段名和表明可以使用反引号。

sqlserver只能使用单引号,且不能使用反引号。

Mysql写法:

Select password from Users where userName=’boss’ or username=”jmj”

Sqlserver写法:

Select password from Users where userName=’boss’ or username=’jmj’

7、取出查询结果中的第一条数据或者前几条记录(取前几条记录只需要修改对应的数字即可),分页也是使用这个关键字:

SQLServer写法:

select top 1 password from users where userName=’boss’

MySQL写法:

select password from users where userName=’111’limit 0,1

8、查询所有库

SQLServer写法:

select * from [master]..[SysDatabases];

MySQL写法:

SHOW DATABASES;

9、查询指定库中的所有表

SQLServer写法:

select *from 库名.dbo.[SysObjects] where[type]=’U’;

(注:若想知道[type]=’U’代表什么意思,请点击http://blog.csdn.net/winddai/article/details/5815138

MySQL写法:

SHOW TABLES

10、某些关键词的使用
10.1截取字符串

SQLServer只能使用SUBSTRING关键词来截取字符串。

MySQL可以使用SUBSTRING和SUBSTR截取字符串

10.2取得字符串的长度

SQLServer只能使用Len关键词取得字符串的长度。

MySQL可以使用Length取得字符串的长度。

11、相同点

delete,select,insert,drop(删除数据库:drop database
库名),update,create(创建数据库:create database 库名)语句一样。

到目前为止,小编只遇到了这些,若有不足之处,欢迎补充!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值