Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert this sql server SP to ORACLE SP

Status
Not open for further replies.

crazyitguy

IS-IT--Management
Jul 20, 2006
36
US
Can someone help me convert this SQL server SP to ORACLE. I am mostly interested in the "WITH a AS" and "OVER (ORDER BY..." statements.

Thanks

Code:
ALTER PROCEDURE [dbo].[GetAccountsSubset] 
	@startRowIndex 		int,
	@maximumRows		int,
	@sortExpression     nvarchar(100) = null
AS

WITH a AS
(
	SELECT accounts.accountId, accounts.description, accounts.active,
			accounts.accountNumber, accounts.accountTypeId,accountTypes.accountType,accountTypes.increaseOnDebit, ROW_NUMBER()
		OVER (

	ORDER BY CASE WHEN @sortExpression = 'accountId' THEN CONVERT(varchar(50), accounts.accountId)
                  WHEN @sortExpression = 'description' THEN accounts.description
                  WHEN @sortExpression = 'active' THEN CONVERT(varchar(50), accounts.active)
                  WHEN @sortExpression = 'accountNumber' THEN CONVERT(varchar(50), accounts.accountNumber)
                  WHEN @sortExpression = 'accountTypeId' THEN CONVERT(varchar(50), accounts.accountTypeId)
                  ELSE CONVERT(varchar(50), accountId)
END

) AS 'rowNumber'
			FROM accounts,accountTypes
			WHERE accounts.accountTypeId = accountTypes.accountTypeId
)

SELECT * 
FROM a
WHERE rowNumber between @startRowIndex  and (@startRowIndex + @maximumRows) - 1
 
From this really helpful website:
Case statement:
Code:
CASE expression
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2
...
WHEN condition_n THEN result_n
ELSE result END
and as an example
Code:
select table_name,
CASE owner
WHEN 'SYS' THEN 'The owner is SYS'
WHEN 'SYSTEM' THEN 'The owner is SYSTEM'
ELSE 'The owner is another value' END
from all_tables;

For the over (order by... are you aiming to count, rank sum?? I can't work that out from your post I'm afraid. If you can post what your eventual aim is I'll be happy to help some more.

Fee

The question should be [red]Is it worth trying to do?[/red] not [blue] Can it be done?[/blue]
 
With" and windowing functions are both supported from Oracle 9.2 onwards. "With" is supported as a method of factoring out subqueries but not for recursive statements.
 
The purpose is to fill a .Net gridview with Paging and sorting enabled.

"Count, rank sum"

I do not know what that means.

" over (order by..." is just to sort the records.

Thanks
 
Dagon: Are you saying that the WITH statement should work?

Here is what I have so far.. What needs to change?

Thanks

Code:
create or replace PROCEDURE GetAccountsSubset
(
     startRowIndex 		int,
     maximumRows		int,
     sortExpression     varchar2(50)
)
AS
BEGIN
     WITH a AS
     (
          SELECT accounts.accountId, accounts.description, 
          accounts.active, accounts.accountNumber,  
          accounts.accountTypeId,accountTypes.accountType,
          accountTypes.increaseOnDebit, ROW_NUMBER()
          OVER (
                 ORDER BY CASE
                 WHEN sortExpression = 'accountId' THEN  accounts.accountId
                 WHEN sortExpression = 'description' THEN accounts.description
                 WHEN sortExpression = 'active' THEN accounts.active
                 WHEN sortExpression = 'accountNumber' THEN accounts.accountNumber
                 WHEN sortExpression = 'accountTypeId' THEN accounts.accountTypeId
                 ELSE accountId
                 END;
                ) AS 'rowNumber'
           FROM accounts,accountTypes
           WHERE accounts.accountTypeId = accountTypes.accountTypeId;
      )
      SELECT *
      FROM a
      WHERE rowNumber between startRowIndex
      AND (startRowIndex + maximumRows) - 1;
END GetAccountsSubset;
 
CrazyItGuy:

Do you get an error message or are the results not what you expect?

(If you need to see the error then type SHOW ERRORS after the compilation fails.)

Fee

The question should be [red]Is it worth trying to do?[/red] not [blue] Can it be done?[/blue]
 
You have a couple of semi-colons and quotes which Oracle doesn't like. If I remove those, the syntax works on my 9.2 system:

Code:
WITH a AS
     (
          SELECT accounts.accountId, accounts.description, 
          accounts.active, accounts.accountNumber,  
          accounts.accountTypeId,accountTypes.accountType,
          accountTypes.increaseOnDebit, ROW_NUMBER()
          OVER (
                 ORDER BY CASE
                 WHEN sortExpression = 'accountId' THEN  accounts.accountId
                 WHEN sortExpression = 'description' THEN accounts.description
                 WHEN sortExpression = 'active' THEN accounts.active
                 WHEN sortExpression = 'accountNumber' THEN accounts.accountNumber
                 WHEN sortExpression = 'accountTypeId' THEN accounts.accountTypeId
                 ELSE accountId
                 END
                ) AS rowNumber
           FROM accounts,accountTypes
           WHERE accounts.accountTypeId = accountTypes.accountTypeId
      )
      SELECT *
      FROM a
      WHERE rowNumber between startRowIndex
      AND (startRowIndex + maximumRows) - 1
 
Thanks...

Here is the code so far. I had to convert each sort expr into chars.

I am now getting an error:

Code:
9/5      PLS-00428: an INTO clause is expected in this SELECT statement

Code:
create or replace PROCEDURE GetAccountsSubset
(
     startRowIndex    in     int,
     maximumRows    in    int,
     sortExpression in     varchar2
)
AS
BEGIN
    WITH a AS
     (
          SELECT accounts.accountId, accounts.description,
          accounts.active, accounts.accountNumber,
          accounts.accountTypeId,accountTypes.accountType,
          accountTypes.increaseOnDebit, ROW_NUMBER()
          OVER (
                 ORDER BY CASE
                 WHEN sortExpression = 'accountId' THEN  TO_CHAR(accounts.accountId)
                 WHEN sortExpression = 'description' THEN accounts.description
                 WHEN sortExpression = 'active' THEN TO_CHAR(accounts.active)
                 WHEN sortExpression = 'accountNumber' THEN TO_CHAR(accounts.accountNumber)
                 WHEN sortExpression = 'accountTypeId' THEN TO_CHAR(accounts.accountTypeId)
                 ELSE TO_CHAR(accountId)
                 END
                ) AS rowNumber
           FROM accounts,accountTypes
           WHERE accounts.accountTypeId = accountTypes.accountTypeId
      )
      SELECT *
      FROM a
      WHERE rowNumber between startRowIndex
      AND (startRowIndex + maximumRows) - 1;
END GetAccountsSubset;
 
That is correct. PL/SQL expects you to select into a variable. The syntax is:

select x
into y
from dual;

However, this only works if you are selecting a single row. To loop through an entire result set from a table, you must use something called a cursor (I don't know if sql*server has the equivalent concept).

It might be a good idea for you to peruse the PL/SQL manuals. These can be found at:

 
Those are 9i manuals but, if you hunt around there, they also have the 10g versions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top