如何使用mysql中的“With”关键字将MSSQL存储过程转换为Mysql SP?

时间:2014-01-11 05:03:10

标签: mysql sql-server stored-procedures

这是我的MSSQL存储过程,我希望将此SP转换为MySQL,但我无法理解在MySql中使用什么而不是'with'关键字,所以任何人都帮助我??? Thanx提前!!!!!

ALTER PROCEDURE [dbo].[TPortRateOnKm_SP_List]
@qtype varchar(MAX) = NULL,
@query varchar(MAX)= NULL,
@Sortname varchar(MAX) =NULL,
@sortorder varchar(MAX) =NULL ,
@PageNo int,
@RecordsPerPage int,
@likesearch int
AS 
BEGIN 
DECLARE @temp INT;
DECLARE @qry varchar(MAX) = '';
DECLARE @qry1 varchar(MAX) = '';
set @temp  = (@PageNo - 1) * @RecordsPerPage

If @query is not null and @query <> ''
Begin
Set @qry1= ' Where '+ @query    
End

Else
Begin 
Set @qry1= ''
End

If @query is not null and @query <> ''

Begin
Set @query= ' Where ('+ @query + ') and (RowNo > ' + 
            cast(((@PageNo-1) *@RecordsPerPage) as varchar) + '  AND RowNo <= '    
            + cast(@PageNo * @RecordsPerPage as varchar)+ ') '  
End

Else

Begin 
Set @query= ' Where (RowNo > ' + cast(((@PageNo-1) *@RecordsPerPage) as varchar) + '  AND RowNo <= ' + cast(@PageNo * @RecordsPerPage as varchar)+ ') '
End


If @sortorder is not null and @sortorder <> ''

Begin
Set @sortorder= ' Order By '+ @sortorder
End

Else

Begin 
Set @sortorder= ' ORDER BY VehicleId'
End

这里是'With'关键字

set @qry = 'Set dateformat dmy ;WITH  CustomTable as
(Select ROW_NUMBER() OVER ( '+ @sortorder+')  as RowNo,A.* From
(select Convert(varchar,T.WefDate,103) As WefDate,T.VehicleId as 
                VehicleId,V.VehicleNo as VehicleNo,T.StartKm,T.EndKm,T.Rate from 
                TrnRateOnKmRange T left outer join MasterVehicle V on             
                V.VehicleId=T.VehicleId) A '

   Set @qry= @qry + @qry1

   Set @qry= @qry + ' )

   SELECT CAST(RowNo AS INT) as 
               RowNo,WefDate,VehicleId,VehicleNo,StartKm,EndKm,Rate
   FROM CustomTable' 

   Set @qry=@qry +  @query + @sortorder

   EXECUTE (@qry)

               END

任何人都可以这样做吗?

0 个答案:

没有答案