Use dynamic SQL to solve the problem

xiaoxiao2021-03-06  63

We often encounter a stored procedure to do a certain order. Usually the conditions of Order By have only one fixed, which is far less than enough. You need to replace it with a variable, and oracle does not recognize the string behind the ORDER BY. what can we do about it? Let's take a look at me: iforderby = 'Objectid desc' THEN - Sort Open Outcur for Select * from (Select Rownum Rowno, T. * From (Select ObjectID, Hits, PostTime From Cartoon ORDER BY ObjectID Desc) ) Twhere rown u = = (= startrow;

elsif myorderby = 'HITS DESC' then - Sort CTR OPEN outcur FOR SELECT * FROM (SELECT rownum rowno, t * FROM (SELECT OBJECTID, HITS, POSTTIME FROM CARTOON ORDER BY HITS DESC ') tWHERE rownum = startRow; - (. SELECT rownum rowno, t * FROM (SELECT OBJECTID, HITS, pOSTTIME FROM CARTOON ORDER bY HITS DESC ') tWHERE rownum = Startrow; end if; although it is commonly used in two sorting methods, it is more complicated and not universal. At this time my colleague is good: ----------------------------------------- ---------------------------------- if (myorderby is not null) Thenmysql: = 'SELECT * FROM (SELECT Rownum Rowno, T. * from (select ObjectID, Hits, PostTime From Cartoon ORDER BY '|| myorderby ||') T where rownum <'|| endrow ||') WHERE ROWNO> = '|| Startrow; Elsemysql: = 'Select * from (Select Rownum Rowno, T. * From (Select ObjectID, Hits, PostTime From Cartoon ORDER BY PostTime Desc) T where rownum <' || endrow || ') Where rowno> =' || StarTrow; endiff ;

Open outcur for mysql;

How, I feel different. I hope you can have a better way.

转载请注明原文地址:https://www.9cbs.com/read-84682.html

New Post(0)