MYSQL與SQLserver之間存儲(chǔ)過(guò)程的轉(zhuǎn)換方式
MYSQL與SQLserver之間存儲(chǔ)過(guò)程的轉(zhuǎn)換
首先先放兩個(gè)存儲(chǔ)過(guò)程來(lái)進(jìn)行對(duì)比
mysql存儲(chǔ)過(guò)程
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; ? ? -- 拼接完成后可以調(diào)用 select @s 語(yǔ)句,查看最終拼接的sql語(yǔ)句是否正確 ? ? prepare stmt from @s;-- 預(yù)編譯一條sql語(yǔ)句,并命名為stmt ? ? execute stmt;-- 執(zhí)行預(yù)編譯sql END
sqlserver存儲(chǔ)過(guò)程
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 ? ? -- 拼接完成后可以調(diào)用 select @s 語(yǔ)句,查看最終拼接的sql語(yǔ)句是否正確 ?? ??? ?print(@s) ?? ??? ?EXEC sp_executesql @s; END
綜合以上同一功能函數(shù)在不同的數(shù)據(jù)庫(kù)中的規(guī)則不同,總結(jié)如下幾點(diǎn)區(qū)別與相互之間的轉(zhuǎn)換規(guī)則:
(1)對(duì)于輸入?yún)?shù)來(lái)說(shuō)
- mysql使用IN cone VARCHAR ( 30 )
- sqlserver使用@cone VARCHAR ( 30 )
注意對(duì)于參數(shù)在下面語(yǔ)句使用中,mysql可以直接使用名稱,二sqlserver要加上@符號(hào)
(2)對(duì)于語(yǔ)句的set來(lái)說(shuō)
- mysql可以直接set 變量
- sqlserver需要在之前事先聲明變量后才可以使用
(3)對(duì)于if語(yǔ)句的執(zhí)行
- mysql使用if 過(guò)程 endif
- sqlserver使用 if begin 過(guò)程 end
(4)對(duì)于定義sql語(yǔ)句的執(zhí)行
- mysql使用prepare stmt from @s; execute stmt;進(jìn)行預(yù)編譯和執(zhí)行
- sqlserver使用EXEC sp_executesql @s
注意:sqlserver也可以使用exec(@s),這樣的話變量聲明一般是varchar類型,若使用sp_executesql必須是Nvarchar的定義類型,具體的區(qū)別可以自行百度查詢
SQLserver轉(zhuǎn)MYSQL存儲(chǔ)過(guò)程的經(jīng)驗(yàn)
總體來(lái)說(shuō),sql sever和Mysql的存儲(chǔ)過(guò)程的思路都是一樣的,但是在語(yǔ)法和結(jié)構(gòu)上還是有很大的區(qū)別的,可以使用如下的轉(zhuǎn)換方式。
1. 存儲(chǔ)過(guò)程的定義方式存在區(qū)別
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. 可直接替換的關(guān)鍵字
smalldatetime | datetime |
money | DECIMAL(18,4) |
numeric | DECIMAL |
max | 8000 |
isnull | ifnull |
getdate | now |
dbo. |
4. select語(yǔ)句起別名的方式有區(qū)別
select 'sunday' day; | SELECT 'sunday' AS day; |
5. if語(yǔ)句的結(jié)構(gòu)存在區(qū)別
if condition statement else statement | if condition then statement else statement end if; |
6. cast語(yǔ)句的目標(biāo)類型存在區(qū)別
目標(biāo)類型可以是任意類型 | 目標(biāo)類型可以是以下類型之一:BINARY,CHAR,DATE,DATETIME,TIME,DECIMAL,SIGNED,UNSIGNED |
7. 動(dòng)態(tài)SQL執(zhí)行語(yǔ)句的書(shū)寫(xiě)方式存在區(qū)別
exec(@sql) | PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; |
8. 調(diào)用其它存儲(chǔ)過(guò)程的方式存在區(qū)別
exec p1 @v1,@v2,@v3 output | call p1(hy_v1,hy_v2,@v3 output ); |
9. 創(chuàng)建臨時(shí)表的書(shū)寫(xiě)方式存在區(qū)別
select 表字段 into #臨時(shí)表名稱 from 正常表 | CREATE TEMPORARY TABLE IF NOT EXISTS 臨時(shí)表名稱 AS SELECT 表字段名稱 FROM 表名稱; |
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
mysql實(shí)現(xiàn)按組區(qū)分后獲取每組前幾名的sql寫(xiě)法
這篇文章主要介紹了mysql實(shí)現(xiàn)按組區(qū)分后獲取每組前幾名的sql寫(xiě)法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2023-03-03mysql中Table is read only的解決方法小結(jié)
本文章總結(jié)了關(guān)于在linux與windows中 mysql出現(xiàn)Table is read only解決辦法總結(jié),有需要的朋友可參考一下2013-01-01