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