Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
my question is
how to Use Sql Server WITH CTE in Mysql StoredProcedure
i kne its not in mysql but please help me to solve this query
Please Give me solution of "With"
SQL
<pre>
     ;WITH CTECostCentre AS
    (
        SELECT *,COUNT(t1.CostCentreID) OVER()  COUNT FROM
        (
            SELECT ROW_NUMBER() OVER (ORDER BY
                CASE WHEN SortType = &#39;DESC&#39; THEN
                    CASE
                        WHEN SortExpression = &#39;CategoryName&#39; THEN CategoryName
                    END
                END DESC
            ) AS ROW,te
            FROM  categorymaster C
            WHERE (
                     (IsSearchByText = 1 AND (SearchText IS NULL OR LEN(SearchText)=0)
                     OR REPLACE(C.CostCentreName, &#39; &#39;, &#39;&#39;) LIKE &#39;%&#39; + REPLACE(SearchText,&#39; &#39;,&#39;&#39;) + &#39;%&#39;
                     OR REPLACE(CG.CategoryName, &#39; &#39;, &#39;&#39;) LIKE &#39;%&#39; + REPLACE(SearchText,&#39; &#39;,&#39;&#39;) + &#39;%&#39;

                     )
                   )
                ) AS t1
    )

    SELECT * FROM CTECostCentre WHERE ROW BETWEEN FromIndex AND ToIndex
</pre>
Posted
Comments
virusstorm 18-May-15 14:45pm    
You use CTE's like you would an inline view, just helps to clean up the SQL and make things look cleaner. With that said, you can write it basically like this:

SELECT *
FROM (###CONTENTS OF CTE###)
WHERE ROW BETWEEN FromIndex AND ToIndex

I don't think MySQL has the ROW_NUMBER() function, so you will need to dump that data into a temp table with an identity column to get your row numbers.
Manish Dalwadi 18-May-15 23:03pm    
i am gotting errors (row_number, over(),etc..,etc..) if you knew then can you plese tell me how to handle this sp in mysql i request you coz i am working in mysql first time, and tried google but not geting result as i want
virusstorm 19-May-15 15:03pm    
First of all, if you are tired of using Google because you are not getting the result you want, you might not be asking the right question. Second of all, the people here help you at the cost of their own time, be mindful how you word your responses. Lastly, it is hard to determine what your CTE is doing. The formatting is a little off when you pasted it on here (doesn't look like it encoded everything correctly). Also, we need to know what the table looks like and if you can provide sample data of the input and outputs, we can help rewrite the CTE to work in MySQL.
Manish Dalwadi 24-May-15 7:09am    
thanks sir,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900