无法获取行号

时间:2013-01-08 07:34:00

标签: sql oracle

我在一个包中有这个代码:

    BEGIN
      IF user_status_i = 'all' THEN
        OPEN l_get_data FOR

        SELECT 
           row_number() OVER (ORDER BY u.us_fullname) rn,
           a2.av_value as first_name, a3.av_value as last_name, a4.av_value as email, a5.av_value as phone, 
           u.us_id, u.us_fullname, a6.av_value as user_type, a7.av_value as line_manager_1, 
           a8.av_value as line_manager_2, a9.av_value as line_manager_3, a10.av_value as line_manager_4, 
           a11.av_value as line_manager_5, u.us_flags, a12.av_value as business_unit, 
           a13.av_value as position, a14.av_value as middlename
           FROM us_user u

                 INNER JOIN av_attrvalue a1 
                     ON a1.AV_TOKEN = 'USER:'||u.US_ID AND a1.AV_AP_ID='InSign' AND a1.av_at_id = 'Code' AND a1.av_value = id_i
                 INNER JOIN av_attrvalue a2 
                     ON a2.AV_TOKEN = 'USER:'||u.US_ID AND a2.AV_AP_ID='InSign' AND a2.av_at_id = 'FIRSTNAME' 
                 INNER JOIN av_attrvalue a3 
                     ON a3.AV_TOKEN = 'USER:'||u.US_ID AND a3.AV_AP_ID='InSign' AND a3.av_at_id = 'LASTNAME' 
                 INNER JOIN av_attrvalue a4 
                     ON a4.AV_TOKEN = 'USER:'||u.US_ID AND a4.AV_AP_ID='InSign' AND a4.av_at_id = 'ADDR' 
                 LEFT OUTER JOIN av_attrvalue a5 
                     ON a5.AV_TOKEN = 'USER:'||u.US_ID AND a5.AV_AP_ID='InSign' AND a5.av_at_id = 'MobilePhone' 
                 INNER JOIN av_attrvalue a6 
                     ON a6.AV_TOKEN = 'USER:'||u.US_ID AND a6.AV_AP_ID='InSign' AND a6.av_at_id = 'Type' AND a6.av_value != 'trAgent'
                 LEFT OUTER JOIN av_attrvalue a7 
                     ON a7.AV_TOKEN = 'USER:'||u.US_ID AND a7.AV_AP_ID='InSign' AND a7.av_at_id = 'Line1'
                 LEFT OUTER JOIN av_attrvalue a8 
                     ON a8.AV_TOKEN = 'USER:'||u.US_ID AND a8.AV_AP_ID='InSign' AND a8.av_at_id = 'Line2' 
                 LEFT OUTER JOIN av_attrvalue a9 
                     ON a9.AV_TOKEN = 'USER:'||u.US_ID AND a9.AV_AP_ID='InSign' AND a9.av_at_id = 'Line3' 
                 LEFT OUTER JOIN av_attrvalue a10 
                     ON a10.AV_TOKEN = 'USER:'||u.US_ID AND a10.AV_AP_ID='InSign' AND a10.av_at_id = 'Line4' 
                 LEFT OUTER JOIN av_attrvalue a11 
                     ON a11.AV_TOKEN = 'USER:'||u.US_ID AND a11.AV_AP_ID='InSign' AND a11.av_at_id = 'Line5' 
                 LEFT OUTER JOIN av_attrvalue a12 
                     ON a12.AV_TOKEN = 'USER:'||u.US_ID AND a12.AV_AP_ID='InSign' AND a12.av_at_id = 'Unit' 
                 LEFT OUTER JOIN av_attrvalue a13 
                     ON a13.AV_TOKEN = 'USER:'||u.US_ID AND a13.AV_AP_ID='InSign' AND a13.av_at_id = 'Position'
           LEFT OUTER JOIN av_attrvalue a14
                     ON a14.AV_TOKEN = 'USER:'||u.US_ID AND a14.AV_AP_ID='InSign' AND a14.av_at_id = 'MIDDLENAME' 
                 INNER JOIN hds_hub_companies hc ON hc.company_id = a1.av_value   

WHERE rn BETWEEN (pageN_i * records_i) - records_i + 1 AND (pageN_i * records_i);



      END IF;

我正在尝试获取行号,但是当我执行它时,我遇到了2个错误:

Error(1323,5): PL/SQL: SQL Statement ignored

Error(1361,14): PL/SQL: ORA-00904: "RN": invalid identifier

我从昨天起就一直在与此作斗争,我确信有一些我不知道的小事

2 个答案:

答案 0 :(得分:1)

您需要将其作为一个块才能工作,因为在同一个块中无法访问别名。

select * From(
 SELECT 
     row_number() OVER (ORDER BY u.us_fullname) rn,
     a2.av_value as first_name, a3.av_value as last_name, a4.av_value as email, a5.av_value as phone, 
     u.us_id, u.us_fullname, a6.av_value as user_type, a7.av_value as line_manager_1, 
     a8.av_value as line_manager_2, a9.av_value as line_manager_3, a10.av_value as line_manager_4, 
     a11.av_value as line_manager_5, u.us_flags, a12.av_value as business_unit, 
     a13.av_value as position, a14.av_value as middlename
     FROM us_user u

           INNER JOIN av_attrvalue a1 
               ON a1.AV_TOKEN = 'USER:'||u.US_ID AND a1.AV_AP_ID='InSign' AND a1.av_at_id = 'Code' AND a1.av_value = id_i
           INNER JOIN av_attrvalue a2 
               ON a2.AV_TOKEN = 'USER:'||u.US_ID AND a2.AV_AP_ID='InSign' AND a2.av_at_id = 'FIRSTNAME' 
           INNER JOIN av_attrvalue a3 
               ON a3.AV_TOKEN = 'USER:'||u.US_ID AND a3.AV_AP_ID='InSign' AND a3.av_at_id = 'LASTNAME' 
           INNER JOIN av_attrvalue a4 
               ON a4.AV_TOKEN = 'USER:'||u.US_ID AND a4.AV_AP_ID='InSign' AND a4.av_at_id = 'ADDR' 
           LEFT OUTER JOIN av_attrvalue a5 
               ON a5.AV_TOKEN = 'USER:'||u.US_ID AND a5.AV_AP_ID='InSign' AND a5.av_at_id = 'MobilePhone' 
           INNER JOIN av_attrvalue a6 
               ON a6.AV_TOKEN = 'USER:'||u.US_ID AND a6.AV_AP_ID='InSign' AND a6.av_at_id = 'Type' AND a6.av_value != 'trAgent'
           LEFT OUTER JOIN av_attrvalue a7 
               ON a7.AV_TOKEN = 'USER:'||u.US_ID AND a7.AV_AP_ID='InSign' AND a7.av_at_id = 'Line1'
           LEFT OUTER JOIN av_attrvalue a8 
               ON a8.AV_TOKEN = 'USER:'||u.US_ID AND a8.AV_AP_ID='InSign' AND a8.av_at_id = 'Line2' 
           LEFT OUTER JOIN av_attrvalue a9 
               ON a9.AV_TOKEN = 'USER:'||u.US_ID AND a9.AV_AP_ID='InSign' AND a9.av_at_id = 'Line3' 
           LEFT OUTER JOIN av_attrvalue a10 
               ON a10.AV_TOKEN = 'USER:'||u.US_ID AND a10.AV_AP_ID='InSign' AND a10.av_at_id = 'Line4' 
           LEFT OUTER JOIN av_attrvalue a11 
               ON a11.AV_TOKEN = 'USER:'||u.US_ID AND a11.AV_AP_ID='InSign' AND a11.av_at_id = 'Line5' 
           LEFT OUTER JOIN av_attrvalue a12 
               ON a12.AV_TOKEN = 'USER:'||u.US_ID AND a12.AV_AP_ID='InSign' AND a12.av_at_id = 'Unit' 
           LEFT OUTER JOIN av_attrvalue a13 
               ON a13.AV_TOKEN = 'USER:'||u.US_ID AND a13.AV_AP_ID='InSign' AND a13.av_at_id = 'Position'
     LEFT OUTER JOIN av_attrvalue a14
               ON a14.AV_TOKEN = 'USER:'||u.US_ID AND a14.AV_AP_ID='InSign' AND a14.av_at_id = 'MIDDLENAME' 
           INNER JOIN hds_hub_companies hc ON hc.company_id = a1.av_value   
)x
WHERE rn BETWEEN (pageN_i * records_i) - records_i + 1 AND (pageN_i * records_i);

答案 1 :(得分:1)

别名rn在同一语句范围中不可用。您需要使用派生表,如下所示

SELECT * FROM
(
SELECT 
           row_number() OVER (ORDER BY u.us_fullname) rn,
           a2.av_value as first_name, a3.av_value as last_name, a4.av_value as email, a5.av_value as phone, 
           u.us_id, u.us_fullname, a6.av_value as user_type, a7.av_value as line_manager_1, 
           a8.av_value as line_manager_2, a9.av_value as line_manager_3, a10.av_value as line_manager_4, 
           a11.av_value as line_manager_5, u.us_flags, a12.av_value as business_unit, 
           a13.av_value as position, a14.av_value as middlename
           FROM us_user u

                 INNER JOIN av_attrvalue a1 
                     ON a1.AV_TOKEN = 'USER:'||u.US_ID AND a1.AV_AP_ID='InSign' AND a1.av_at_id = 'Code' AND a1.av_value = id_i
                 INNER JOIN av_attrvalue a2 
                     ON a2.AV_TOKEN = 'USER:'||u.US_ID AND a2.AV_AP_ID='InSign' AND a2.av_at_id = 'FIRSTNAME' 
                 INNER JOIN av_attrvalue a3 
                     ON a3.AV_TOKEN = 'USER:'||u.US_ID AND a3.AV_AP_ID='InSign' AND a3.av_at_id = 'LASTNAME' 
                 INNER JOIN av_attrvalue a4 
                     ON a4.AV_TOKEN = 'USER:'||u.US_ID AND a4.AV_AP_ID='InSign' AND a4.av_at_id = 'ADDR' 
                 LEFT OUTER JOIN av_attrvalue a5 
                     ON a5.AV_TOKEN = 'USER:'||u.US_ID AND a5.AV_AP_ID='InSign' AND a5.av_at_id = 'MobilePhone' 
                 INNER JOIN av_attrvalue a6 
                     ON a6.AV_TOKEN = 'USER:'||u.US_ID AND a6.AV_AP_ID='InSign' AND a6.av_at_id = 'Type' AND a6.av_value != 'trAgent'
                 LEFT OUTER JOIN av_attrvalue a7 
                     ON a7.AV_TOKEN = 'USER:'||u.US_ID AND a7.AV_AP_ID='InSign' AND a7.av_at_id = 'Line1'
                 LEFT OUTER JOIN av_attrvalue a8 
                     ON a8.AV_TOKEN = 'USER:'||u.US_ID AND a8.AV_AP_ID='InSign' AND a8.av_at_id = 'Line2' 
                 LEFT OUTER JOIN av_attrvalue a9 
                     ON a9.AV_TOKEN = 'USER:'||u.US_ID AND a9.AV_AP_ID='InSign' AND a9.av_at_id = 'Line3' 
                 LEFT OUTER JOIN av_attrvalue a10 
                     ON a10.AV_TOKEN = 'USER:'||u.US_ID AND a10.AV_AP_ID='InSign' AND a10.av_at_id = 'Line4' 
                 LEFT OUTER JOIN av_attrvalue a11 
                     ON a11.AV_TOKEN = 'USER:'||u.US_ID AND a11.AV_AP_ID='InSign' AND a11.av_at_id = 'Line5' 
                 LEFT OUTER JOIN av_attrvalue a12 
                     ON a12.AV_TOKEN = 'USER:'||u.US_ID AND a12.AV_AP_ID='InSign' AND a12.av_at_id = 'Unit' 
                 LEFT OUTER JOIN av_attrvalue a13 
                     ON a13.AV_TOKEN = 'USER:'||u.US_ID AND a13.AV_AP_ID='InSign' AND a13.av_at_id = 'Position'
           LEFT OUTER JOIN av_attrvalue a14
                     ON a14.AV_TOKEN = 'USER:'||u.US_ID AND a14.AV_AP_ID='InSign' AND a14.av_at_id = 'MIDDLENAME' 
                 INNER JOIN hds_hub_companies hc ON hc.company_id = a1.av_value   
) T
WHERE rn BETWEEN (pageN_i * records_i) - records_i + 1 AND (pageN_i * records_i);