视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
MySQLVSSQLServer之用法差别_MySQL
2020-11-09 17:27:14 责编:小采
文档


SQL Server

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

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

1. 标识符限定符

数据库 标识符限定符
SQL Server []
MySQL ``

2. 字符串相加

数据库 字符串相加
SQL Server 直接用 +
MySQL concat()

3. isnull()

数据库 isnull()
SQL Server isnull()
MySQL ifnull()

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

4. getdate()

数据库 getdate()
SQL Server getdate()
MySQL now()

5. newid()

数据库 newid()
SQL Server newid()
MySQL uuid()

6. @@ROWCOUNT

数据库 @@ROWCOUNT
SQL Server @@ROWCOUNT
MySQL row_count()

注意:MySQL的这个函数仅对于update, insert, delete有效

7. SCOPE_IDENTITY()

数据库 SCOPE_IDENTITY()
SQL Server SCOPE_IDENTITY()
MySQL last_insert_id()

8. if ... else ...

数据库 if ... else ...
SQL Server
  1. IF Boolean_expression
  2. { sql_statement | statement_block }
  3. [ ELSE
  4. { sql_statement | statement_block } ]
-- 若要定义语句块,请使用控制流关键字 BEGIN 和 END。
MySQL
  1. IF search_condition THEN statement_list
  2. [ELSEIF search_condition THEN statement_list] ...
  3. [ELSE statement_list]
  4. END IF

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

9. declare

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

10. 游标的写法

SQL Server

  1. declare @tempShoppingCart table (ProductId int, Quantity int)
  2. insert into @tempShoppingCart (ProductId, Quantity)
  3. select ProductId, Quantity from ShoppingCart where UserGuid = @UserGuid
  4. declare @productId int
  5. declare @quantity int
  6. declare tempCartCursor cursor for
  7. select ProductId, Quantity from @tempShoppingCart
  8. open tempCartCursor
  9. fetch next from tempCartCursor into @productId, @quantity
  10. while @@FETCH_STATUS = 0
  11. begin
  12. update Product set SellCount = SellCount + @quantity where productId = @productId
  13. fetch next from tempCartCursor into @productId, @quantity
  14. end
  15. close tempCartCursor
  16. deallocate tempCartCursor

MySQL

  1. declare m_done int default 0;
  2. declare m_sectionId int;
  3. declare m_newsId int;
  4. declare _cursor_SN cursor for select sectionid, newsid from _temp_SN;
  5. declare continue handler for not found set m_done = 1;
  6. create temporary table _temp_SN select sectionid, newsid from SectionNews group by sectionid, newsid having count(*) > 1;
  7. open _cursor_SN;
  8. while( m_done = 0 ) do
  9. fetch _cursor_SN into m_sectionId, m_newsId;
  10. if( m_done = 0 ) then
  11. -- 具体的处理逻辑
  12. end if;
  13. end while;
  14. close _cursor_SN;
  15. drop table _temp_SN;

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

11. 分页的处理

SQL Server

  1. create procedure GetProductByCategoryId(
  2. @CategoryID int,
  3. @PageIndex int = 0,
  4. @PageSize int = 20,
  5. @TotalRecords int output
  6. )
  7. as
  8. begin
  9. declare @ResultTable table
  10. (
  11. RowIndex int,
  12. ProductID int,
  13. ProductName nvarchar(50),
  14. CategoryID int,
  15. Unit nvarchar(10),
  16. UnitPrice money,
  17. Quantity int
  18. );
  19. insert into @ResultTable
  20. select row_number() over (order by ProductID asc) as RowIndex,
  21. p.ProductID, p.ProductName, p.CategoryID, p.Unit, p.UnitPrice, p.Quantity
  22. from Products as p
  23. where CategoryID = @CategoryID;
  24. select @TotalRecords = count(*) from @ResultTable;
  25. select *
  26. from @ResultTable
  27. where RowIndex > (@PageSize * @PageIndex) and RowIndex <= (@PageSize * (@PageIndex+1));
  28. end;

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

MySQL

  1. create procedure GetProductsByCategoryId(
  2. in _categoryId int,
  3. in _pageIndex int,
  4. in _pageSize int,
  5. out _totalRecCount int
  6. )
  7. begin
  8. set @categoryId = _categoryId;
  9. set @startRow = _pageIndex * _pageSize;
  10. set @pageSize = _pageSize;
  11. prepare PageSql from 'select sql_calc_found_rows * from product where categoryId = ? order by ProductId desc limit ?, ?';
  12. execute PageSql using @categoryId, @startRow, @pageSize;
  13. deallocate prepare PageSql;
  14. set _totalRecCount = found_rows();
  15. end

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

原文链接:http://www.cnblogs.com/fish-li/archive/2011/04/05/2006107.html

下载本文
显示全文
专题