MYSQL与SQLserver之间存储过程的转换方式
作者:AI_Frank 时间:2024-01-26 20:52:53
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. 批处理分隔符存在差异
GO | delimiter $$ |
3. 可直接替换的关键字
smalldatetime | datetime |
money | DECIMAL(18,4) |
numeric | DECIMAL |
max | 8000 |
isnull | ifnull |
getdate | now |
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 output | call p1(hy_v1,hy_v2,@v3 output ); |
9. 创建临时表的书写方式存在区别
select 表字段 into #临时表名称 from 正常表 | CREATE TEMPORARY TABLE IF NOT EXISTS 临时表名称 AS SELECT 表字段名称 FROM 表名称; |
来源:https://blog.csdn.net/qq_30653631/article/details/105832406
标签:MYSQL,SQLserver,存储过程
0
投稿
猜你喜欢
CentOS Linux更改MySQL数据库目录位置具体操作
2024-01-23 18:13:49
InnoDB引擎数据库主从复制同步新的分享
2024-01-15 10:33:32
ASP在服务器自动解压RAR文件
2010-04-24 16:06:00
Python实现24点小游戏
2022-10-16 01:29:20
六行python代码的爱心曲线详解
2022-04-09 23:15:17
用Python逐行分析文件方法
2022-07-10 10:17:49
Python简单实现区域生长方式
2023-08-08 12:07:38
Pytest 自动化测试框架的使用
2022-11-12 16:47:46
Python全角与半角之间相互转换的方法总结
2023-12-25 03:50:50
在Docker上部署Python的Flask框架的教程
2023-03-27 02:55:38
MySQL分区表实现按月份归类
2024-01-17 12:56:36
在Vue-cli里应用Vuex的state和mutations方法
2024-04-29 13:09:51
浅析mysql 共享表空间与独享表空间以及他们之间的转化
2024-01-28 04:20:53
MySQL中隐藏空间问题浅析
2009-11-24 09:04:00
Python命名空间的本质和加载顺序
2022-06-30 20:56:21
Python Flask-Login实现用户会话管理
2023-06-05 13:13:50
jQuery 取得 background-position 的值
2009-04-05 16:02:00
php处理抢购类功能的高并发请求
2023-11-21 01:30:54
python实现TF-IDF算法解析
2021-06-02 03:27:51
Access中的模糊查询
2007-11-18 14:57:00