0

Good day,

I would like to change some program code (mostly on SQL code) because the database already change from DB2 to Oracle.

Here is 1 example that I successful changed but I am not really understand about it, and I cant find it from google.

The following is the original SQL Query code (using DB2) :

SELECT * 
FROM (SELECT T0.CREATEDBY AS C1, row_number() OVER ( ORDER BY T0.GROUPNAME) AS rownum 
FROM IBSADMIN.CCGROUP T0 
WHERE T0.GROUPID != 0001 AND T0.GROUPID != 001 AND T0.CHANNEL = 'CC') AS tname
WHERE rownum BETWEEN 1 AND 20

Here is the SQL Query code after edit by me (successfully get data from Oracle) :

SELECT * 
FROM (SELECT T0.CREATEDBY AS C1, row_number() OVER ( ORDER BY T0.GROUPNAME) AS rownum1 
FROM IBSADMIN.CCGROUP T0 
WHERE T0.GROUPID != 0001 AND T0.GROUPID != 001 AND T0.CHANNEL = 'CC') tname
WHERE rownum1 BETWEEN 1 AND 20

As I analyze, I get error if I didnt change the rownum to rownum1, error is ORA-00923: FROM keyword not found where expected .Thus I change it to rownum1, I think the rownum should be a keyword in DB2, is there any keyword like this for Oracle also?

In the line 4 last part, from DB2 code, it end with As tname. If I put the same things in Oracle code, I get error ORA-00933: SQL command not properly ended . Thus I erase the As . I not so understand how is the As tname means, As the SQL query in the bracket:

(SELECT T0.CREATEDBY AS C1, row_number() OVER ( ORDER BY T0.GROUPNAME) AS rownum1 
    FROM IBSADMIN.CCGROUP T0 
    WHERE T0.GROUPID != 0001 AND T0.GROUPID != 001 AND T0.CHANNEL = 'CC')

It return me 2 columns, thus I am not understand how is the As tname interate with the 2 column.

Kindly advise.

4

2 回答 2

2
  1. rownum是 Oracle 中的保留字 - 在 Oracle 中,rownum是一个伪列,您可以引用它来获取结果集的行号(在排序之前)。这就是为什么在转换为 Oracle 时需要更改别名的原因。我更喜欢那些更明显不同的东西rownum——rn或者rnk是不错的选择。

  2. tname是查询中内联视图的别名。在 Oracle 中,您不能使用AS关键字来分配表别名(您可以在定义列别名时选择使用它,这就是AS rownum1有效的原因,但您也可以完全摆脱AS)。在这种情况下,tname别名从不使用,因此在 Oracle 中您可以省略它。我知道在某些数据库 (SQL Server) 中需要内联视图的别名——我不确定 DB2 是否需要表别名。

听起来您正确地修改了查询(尽管我对您选择的别名而不是rownum.

于 2013-10-01T03:57:38.320 回答
0

可能没有人对此仍然感兴趣,但我在这里找到了这个解决方案:

http://www.sqllines.com/db2-to-oracle/fetch_first_rows_only

使用 FETCH FIRST x ROWS ONLY,例如:

select * from a_table  FETCH FIRST 1 ROWS ONLY;

只会给你表格的第​​一行,我用过它,它的工作方式和我预期的一样。

于 2014-11-21T16:51:33.133 回答