MySQL VS SQL Server之用法差别

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

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

1. 标识符限定符

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

2. 字符串相加

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

3. isnull()

数据库isnull()
SQL Serverisnull()
MySQLifnull()

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

4. getdate()

数据库getdate()
SQL Servergetdate()
MySQLnow()

5. newid()

数据库newid()
SQL Servernewid()
MySQLuuid()

6. @@ROWCOUNT

数据库@@ROWCOUNT
SQL Server@@ROWCOUNT
MySQLrow_count()

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

7. SCOPE_IDENTITY()

数据库SCOPE_IDENTITY()
SQL ServerSCOPE_IDENTITY()
MySQLlast_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.  
  5.  
  6. declare @productId int 
  7. declare @quantity int 
  8. declare tempCartCursor cursor for   
  9.         select ProductId, Quantity from @tempShoppingCart  
  10.  
  11. open tempCartCursor  
  12. fetch next from tempCartCursor into @productId, @quantity  
  13. while  @@FETCH_STATUS = 0  
  14. begin 
  15.     update Product set SellCount = SellCount + @quantity    where productId = @productId  
  16.  
  17.     fetch next from tempCartCursor into @productId, @quantity  
  18. end 
  19.  
  20. close tempCartCursor  
  21. deallocate tempCartCursor 

MySQL

  1. declare m_done int default 0;  
  2. declare m_sectionId int;  
  3. declare m_newsId int;  
  4.  
  5. declare _cursor_SN cursor for select sectionid, newsid from _temp_SN;  
  6. declare continue handler for not found set m_done = 1;  
  7.  
  8. create temporary table _temp_SN select sectionid, newsid from SectionNews  group by sectionid, newsid having count(*) > 1;  
  9.  
  10. open _cursor_SN;  
  11. while( m_done = 0 ) do  
  12.     fetch _cursor_SN into m_sectionId, m_newsId;  
  13.       
  14.     if( m_done = 0 ) then   
  15.         -- 具体的处理逻辑  
  16.     end if;  
  17. end while;  
  18. close _cursor_SN;  
  19. 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.        
  10. declare @ResultTable table 
  11. (   
  12.     RowIndex int,   
  13.     ProductID int,   
  14.     ProductName nvarchar(50),   
  15.     CategoryID int,   
  16.     Unit nvarchar(10),   
  17.     UnitPrice money,   
  18.     Quantity int 
  19. );   
  20.        
  21. insert into @ResultTable   
  22. select row_number() over (order by ProductID ascas RowIndex,   
  23.        p.ProductID, p.ProductName, p.CategoryID, p.Unit, p.UnitPrice, p.Quantity   
  24. from   Products as p   
  25. where CategoryID = @CategoryID;   
  26.          
  27. select  @TotalRecords = count(*) from  @ResultTable;   
  28.        
  29. select *   
  30. from   @ResultTable   
  31. where  RowIndex > (@PageSize * @PageIndex) and RowIndex <= (@PageSize * (@PageIndex+1));   
  32.        
  33. 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.    
  9.    set @categoryId = _categoryId;  
  10.    set @startRow = _pageIndex * _pageSize;  
  11.    set @pageSize = _pageSize;  
  12.    
  13.    prepare PageSql from 'select sql_calc_found_rows * from product  where categoryId = ? order by ProductId desc limit ?, ?';  
  14.    execute PageSql using @categoryId, @startRow, @pageSize;  
  15.    deallocate prepare PageSql;  
  16.    set _totalRecCount = found_rows();  
  17.    
  18. end 

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

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

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/255100.html<

(0)
运维的头像运维
上一篇2025-04-30 16:36
下一篇 2025-04-30 16:37

相关推荐

  • 个人主题怎么制作?

    制作个人主题是一个将个人风格、兴趣或专业领域转化为视觉化或结构化内容的过程,无论是用于个人博客、作品集、社交媒体账号还是品牌形象,核心都是围绕“个人特色”展开,以下从定位、内容规划、视觉设计、技术实现四个维度,详细拆解制作个人主题的完整流程,明确主题定位:找到个人特色的核心主题定位是所有工作的起点,需要先回答……

    2025-11-20
    0
  • 社群营销管理关键是什么?

    社群营销的核心在于通过建立有温度、有价值、有归属感的社群,实现用户留存、转化和品牌传播,其管理需贯穿“目标定位-内容运营-用户互动-数据驱动-风险控制”全流程,以下从五个维度展开详细说明:明确社群定位与目标社群管理的首要任务是精准定位,需明确社群的核心价值(如行业交流、产品使用指导、兴趣分享等)、目标用户画像……

    2025-11-20
    0
  • 香港公司网站备案需要什么材料?

    香港公司进行网站备案是一个涉及多部门协调、流程相对严谨的过程,尤其需兼顾中国内地与香港两地的监管要求,由于香港公司注册地与中国内地不同,其网站若主要服务内地用户或使用内地服务器,需根据服务器位置、网站内容性质等,选择对应的备案路径(如工信部ICP备案或公安备案),以下从备案主体资格、流程步骤、材料准备、注意事项……

    2025-11-20
    0
  • 如何企业上云推广

    企业上云已成为数字化转型的核心战略,但推广过程中需结合行业特性、企业痛点与市场需求,构建系统性、多维度的推广体系,以下从市场定位、策略设计、执行落地及效果优化四个维度,详细拆解企业上云推广的实践路径,精准定位:明确目标企业与核心价值企业上云并非“一刀切”的方案,需先锁定目标客户群体,提炼差异化价值主张,客户分层……

    2025-11-20
    0
  • PS设计搜索框的实用技巧有哪些?

    在PS中设计一个美观且功能性的搜索框需要结合创意构思、视觉设计和用户体验考量,以下从设计思路、制作步骤、细节优化及交互预览等方面详细说明,帮助打造符合需求的搜索框,设计前的规划明确使用场景:根据网站或APP的整体风格确定搜索框的调性,例如极简风适合细线条和纯色,科技感适合渐变和发光效果,电商类则可能需要突出搜索……

    2025-11-20
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注