广告
返回顶部
首页 > 资讯 > 数据库 >MYSQL与SQLserver之间存储过程的转换方式
  • 795
分享到

MYSQL与SQLserver之间存储过程的转换方式

摘要

目录Mysql与sqlserver之间存储过程的转换mysql存储过程sqlserver存储过程SQLserver转MYSQL存储过程的经验1. 存储过程的定义方式存在区别2. 批处理分隔符存在差异3. 可直接替换的关键

MYSQL与SQLserver之间存储过程的转换

首先先放两个存储过程来进行对比

mysql存储过程

CREATE DEFINER=`root`@`%` PROCEDURE `searchProduct`(
 
 IN cone VARCHAR ( 30 ),
 IN ctow VARCHAR ( 30 ),
 
 IN page INT,
 IN size INT
 )
BEGIN
   set @s='SELECT *  FROM  productclass where status=0';
 
--   if(pname is not null) and pname!=''
--   then set @s=concat(@s,' and product LIKE \'','%',pname,'%','\'');
--   end if;
  if(cone is not null) and cone!=''
  then set @s=concat(@s,' and class1 LIKE \'','%',cone,'%','\'');
  end if;
  if(ctow is not null) and ctow!=''
  then set @s=concat(@s,' and class2 LIKE \'','%',ctow,'%','\'');
  end if;
  
  
  set @s=concat(@s,' ORDER BY class1,class2,class3,class4');
  if(size>0) then
  set @s=concat(@s,' limit ',(page-1)*size,',',size);
  end if;
  -- 拼接完成后可以调用 select @s 语句,查看最终拼接的sql语句是否正确
  prepare stmt from @s;-- 预编译一条sql语句,并命名为stmt
  execute stmt;-- 执行预编译sql
END

sqlserver存储过程

ALTER PROCEDURE [dbo].[searchProduct]
@cone VARCHAR ( 30 ),@ctow VARCHAR ( 30 ),@page INT,@size INT
AS
BEGIN
 -- routine body Goes here, e.g.
 -- SELECT 'Navicat for SQL Server'
 declare @s Nvarchar(MAX);
 set @s='SELECT *  FROM  productclass where status=0';
 
--   if(pname is not null) and pname!=''
--   then set @s=concat(@s,' and product LIKE \'','%',pname,'%','\'');
--   end if;
  if(@cone is not null) and @cone!=''
  BEGIN
   set @s=concat(@s,' and class1 LIKE ','''%',@cone,'%''');
  END
  if(@ctow is not null) and @ctow!=''
  BEGIN
   set @s=concat(@s,' and class2 LIKE ','''%',@ctow,'%''');
  END
  
  
  set @s=concat(@s,' ORDER BY class1,class2,class3,class4');
  if(@size>0)
  BEGIN
   set @s=concat(@s,'( select top ',@size,' id from productclass
       where id not in ( 
       select top ', (@page-1)*@size,' id from productclass 
     ))')
  END
  -- 拼接完成后可以调用 select @s 语句,查看最终拼接的sql语句是否正确
  print(@s)
  EXEC sp_executesql @s;
END

综合以上同一功能函数在不同的数据库中的规则不同,总结如下几点区别与相互之间的转换规则:

(1)对于输入参数来说

  • mysql使用IN cone VARCHAR ( 30 )
  • sqlserver使用@cone VARCHAR ( 30 )

注意对于参数在下面语句使用中,mysql可以直接使用名称,二sqlserver要加上@符号

(2)对于语句的set来说

  • mysql可以直接set 变量
  • sqlserver需要在之前事先声明变量后才可以使用

(3)对于if语句的执行

  • mysql使用if 过程 endif
  • sqlserver使用 if begin 过程 end

(4)对于定义sql语句的执行

  • mysql使用prepare stmt from @s; execute stmt;进行预编译和执行
  • sqlserver使用EXEC sp_executesql @s

注意:sqlserver也可以使用exec(@s),这样的话变量声明一般是varchar类型,若使用sp_executesql必须是Nvarchar的定义类型,具体的区别可以自行百度查询

SQLserver转MYSQL存储过程的经验

总体来说,sql sever和Mysql的存储过程的思路都是一样的,但是在语法和结构上还是有很大的区别的,可以使用如下的转换方式。

1. 存储过程的定义方式存在区别

CREATE proc p1
aa int
bb varchar(255) output
as
CREATE PROCEDURE p1(
in aa int,
out bb varchar(255)
) begin
statement_list
end;

2. 批处理分隔符存在差异

GOdelimiter $$

3. 可直接替换的关键字

smalldatetimedatetime
moneyDECIMAL(18,4)
numericDECIMAL
max8000
isnullifnull
getdatenow
dbo.

4、select语句起别名的方式有区别

select 'sunday' day;SELECT 'sunday' AS day;

5. if语句的结构存在区别

if condition
statement
else
statement
if condition then
statement
else
statement
end if;

6. cast语句的目标类型存在区别

目标类型可以是任意类型目标类型可以是以下类型之一:BINARY,CHAR,DATE,DATETIME,TIME,DECIMAL,SIGNED,UNSIGNED

7. 动态SQL执行语句的书写方式存在区别

exec(@sql)PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

8. 调用其它存储过程的方式存在区别

exec p1 @v1,@v2,@v3 outputcall p1(hy_v1,hy_v2,@v3 output );

9. 创建临时表的书写方式存在区别

select 表字段 into #临时表名称
from 正常表
CREATE TEMPORARY TABLE IF NOT EXISTS 临时表名称 AS
SELECT 表字段名称 FROM 表名称;

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

您可能感兴趣的文档:

--结束END--

本文标题: MYSQL与SQLserver之间存储过程的转换方式

本文链接: https://www.lsjlt.com/news/173603.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作