MySQL VS SQL Server之用法差别

跟版网(www.genban.org)提供用法,差别,int,MySQL,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.  
  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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

数据库查询哪个对像里面包含什么字段语句写法: select * from sysobjects o, syscomments s where o.id = s.id and text like %text% and o.xtype = P text 换成需要查的字段 数据库查询哪个对像里面包含表: select o.name from sys.all_sql_modules s,sys
一、 创建用户: 命令:CREATE USER username@host IDENTIFIED BY password; 说明:username - 你将创建的用户名, host - 指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost, 如果想让该用户可以从任意远程主机登陆,可以使用通配符%. password - 该
在mysql中可以用group by对查询出的数据分组 select id,service,name FROM service GROUP BY name,service 如果要查看每组数据的总数,可以 select count(*) FROM service GROUP BY name,service 当要查询group by后的总数,可以这样 select count(*) from(s
mysql count group by统计条数方法 mysql 分组之后如何统计记录条数? gourp by 之后的 count,把group by查询结果当成一个表再count一次 select count(*) as count from(SELECT count(*) FROM 表名 WHERE 条件 GROUP BY id ) a; 实战例子: select count(*)
1.首先停止MySQL服务:service mysqld stop 2.加参数启动mysql:/usr/bin/mysqld_safe --skip-grant-tables 然后就可以无任何限制的访问mysql了 3.root用户登陆系统:mysql -u root -p mysql 4.切换数据库:use mysql 5.显示所有的表:show tables; 这里就可
摘要: SQL的WHERE子句中包含多个AND和OR 示例: SQL解析器在处理操作时会优先处理and操作: 假如有表product字段如下:id、product_id、product_price、product_name,现在要查找产品号为100或者101,并且价格大于200的商品,程序员可能会这样写: select * fr