尝试在DB2 db上运行查询时获得以下错误

时间:2013-10-21 08:56:23

标签: sql db2

我得到这个错误

DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=((select * from ( select rownumber() over(;BEGIN-OF-STATEMENT;<delete>, DRIVER=3.50.152

当我运行以下查询时:

((SELECT ORGANIZATION.ORGNAME AS ORGNAME
        ,SUBSCRIPTION.ID AS SUBSCRIPTIONID
        ,SUBSCRIPTION.NUMBEROFAVAILABLESEATS AS AVAILABLESEATS
    FROM SUBSCRIPTION SUBSCRIPTION
        ,ORGANIZATION ORGANIZATION
        ,CUSTOMER CUSTOMER
        ,CUSTOMERACCOUNT CUSTOMERACCOUNT
    WHERE ORGANIZATION.ISDELETED=0
      AND SUBSCRIPTION.DELETED=0
      AND CUSTOMERACCOUNT.ID=SUBSCRIPTION.CUSTOMERACCOUNTID
      AND CUSTOMER.ISDELETED=0
      AND CUSTOMERACCOUNT.CUSTOMERID=CUSTOMER.ID
      AND CUSTOMER.PARTYID=ORGANIZATION.ID
      AND ISLIMITEDSEAT=1 
UNION ALL 
  SELECT ORGANIZATION.ORGNAME
        ,SUBSCRIPTION.ID AS SUBSCRIPTIONID
        ,-1 AS AVAILABLESEATS
    FROM SUBSCRIPTION SUBSCRIPTION
        ,ORGANIZATION ORGANIZATION
        ,CUSTOMER CUSTOMER
        ,CUSTOMERACCOUNT CUSTOMERACCOUNT
    WHERE ORGANIZATION.ISDELETED=0
      AND SUBSCRIPTION.DELETED=0
      AND CUSTOMERACCOUNT.ID=SUBSCRIPTION.CUSTOMERACCOUNTID
      AND CUSTOMER.ISDELETED=0
      AND CUSTOMERACCOUNT.CUSTOMERID=CUSTOMER.ID
      AND CUSTOMER.PARTYID=ORGANIZATION.ID
      AND ISLIMITEDSEAT=0
 ) as results
  order by ORGNAME
) with ur

Hibrnate解码查询:

"((select * from ( select rownumber() over(order by ORGNAME)with ur) as rownumber_, ORGANIZATION.ORGNAME AS ORGNAME,SUBSCRIPTION.ID AS SUBSCRIPTIONID,SUBSCRIPTION.NUMBEROFAVAILABLESEATS AS AVAILABLESEATS FROM SUBSCRIPTION SUBSCRIPTION,ORGANIZATION ORGANIZATION,CUSTOMER CUSTOMER,CUSTOMERACCOUNT CUSTOMERACCOUNT WHERE ORGANIZATION.ISDELETED=0 AND SUBSCRIPTION.DELETED=0 AND CUSTOMERACCOUNT.ID=SUBSCRIPTION.CUSTOMERACCOUNTID AND CUSTOMER.ISDELETED=0 AND CUSTOMERACCOUNT.CUSTOMERID=CUSTOMER.ID AND CUSTOMER.PARTYID=ORGANIZATION.ID AND ISLIMITEDSEAT=1 UNION ALL SELECT ORGANIZATION.ORGNAME,SUBSCRIPTION.ID AS SUBSCRIPTIONID,-1 AS AVAILABLESEATS FROM SUBSCRIPTION SUBSCRIPTION,ORGANIZATION ORGANIZATION,CUSTOMER CUSTOMER,CUSTOMERACCOUNT CUSTOMERACCOUNT WHERE ORGANIZATION.ISDELETED=0 AND SUBSCRIPTION.DELETED=0 AND CUSTOMERACCOUNT.ID=SUBSCRIPTION.CUSTOMERACCOUNTID AND  CUSTOMER.ISDELETED=0 AND CUSTOMERACCOUNT.CUSTOMERID=CUSTOMER.ID AND CUSTOMER.PARTYID= ORGANIZATION.ID AND ISLIMITEDSEAT=0) results order by ORGNAME)with ur ) as temp_ where rownumber_ <= ?"

3 个答案:

答案 0 :(得分:0)

我将查询格式化了一下。我不确定语法是否正确。 order by看起来有些孤独(没有匹配的选择),我不确定with应该做什么。

((SELECT ORGANIZATION.ORGNAME AS ORGNAME, 
    SUBSCRIPTION.ID AS SUBSCRIPTIONID,
    SUBSCRIPTION.NUMBEROFAVAILABLESEATS AS AVAILABLESEATS 
  FROM SUBSCRIPTION SUBSCRIPTION,
       ORGANIZATION ORGANIZATION,
       CUSTOMER CUSTOMER,
       CUSTOMERACCOUNT CUSTOMERACCOUNT 
  WHERE ORGANIZATION.ISDELETED=0 AND SUBSCRIPTION.DELETED=0 
    AND CUSTOMERACCOUNT.ID=SUBSCRIPTION.CUSTOMERACCOUNTID 
    AND CUSTOMER.ISDELETED=0 
    AND CUSTOMERACCOUNT.CUSTOMERID=CUSTOMER.ID
    AND CUSTOMER.PARTYID=ORGANIZATION.ID 
    AND ISLIMITEDSEAT=1 
UNION ALL 
  SELECT ORGANIZATION.ORGNAME,
         SUBSCRIPTION.ID AS SUBSCRIPTIONID,
         -1 AS AVAILABLESEATS 
  FROM SUBSCRIPTION SUBSCRIPTION,
       ORGANIZATION ORGANIZATION,
       CUSTOMER CUSTOMER,
       CUSTOMERACCOUNT CUSTOMERACCOUNT 
  WHERE ORGANIZATION.ISDELETED=0 
    AND SUBSCRIPTION.DELETED=0 
    AND CUSTOMERACCOUNT.ID=SUBSCRIPTION.CUSTOMERACCOUNTID 
    AND  CUSTOMER.ISDELETED=0 
    AND CUSTOMERACCOUNT.CUSTOMERID=CUSTOMER.ID 
    AND CUSTOMER.PARTYID=ORGANIZATION.ID 
    AND ISLIMITEDSEAT=0
 ) results order by ORGNAME
)with ur

答案 1 :(得分:0)

the following thing worked for me :

SELECT RESULTS.ORGNAME,
       RESULTS.SUBSCRIPTIONID,
       RESULTS.AVAILABLESEATS FROM(
                                     (SELECT ORGANIZATION.ORGNAME ,SUBSCRIPTION.ID AS SUBSCRIPTIONID,-1 AS AVAILABLESEATS
                                      FROM SUBSCRIPTION SUBSCRIPTION,ORG` v`ANIZATION ORGANIZATION,CUSTOMER CUSTOMER,CUSTOMERACCOUNT CUSTOMERACCOUNT
                                      WHERE ORGANIZATION.ISDELETED=0`enter code here`
                                        AND SUBSCRIPTION.DELETED=0
                                        AND CUSTOMERACCOUNT.ID=SUBSCRIPTION.CUSTOMERACCOUNTID
                                        AND CUSTOMER.ISDELETED=0
                                        AND CUSTOMERACCOUNT.CUSTOMERID=CUSTOMER.ID
                                        AND CUSTOMER.PARTYID=ORGANIZATION.ID
                                        AND SUBSCRIPTION.ISLIMITEDSEAT=0)
                                   UNION ALL
                                     (SELECT ORGANIZATION.ORGNAME ,SUBSCRIPTION.ID AS SUBSCRIPTIONID, SUBSCRIPTION.NUMBEROFAVAILABLESEATS AS AVAILABLESEATS
                                      FROM SUBSCRIPTION SUBSCRIPTION,ORGANIZATION ORGANIZATION,CUSTOMER CUSTOMER,CUSTOMERACCOUNT CUSTOMERACCOUNT
                                      WHERE ORGANIZATION.ISDELETED=0
                                        AND SUBSCRIPTION.DELETED=0
                                        AND CUSTOMERACCOUNT.ID=SUBSCRIPTION.CUSTOMERACCOUNTID
                                        AND CUSTOMER.ISDELETED=0
                                        AND CUSTOMERACCOUNT.CUSTOMERID=CUSTOMER.ID
                                        AND CUSTOMER.PARTYID=ORGANIZATION.ID
                                        AND SUBSCRIPTION.ISLIMITEDSEAT=1))AS RESULTS

答案 2 :(得分:0)

您原来的问题可能是由于括号的位置,但由于所发生的事情的长度和复杂性,很难解开。

我会将几个建议放在一起,给你一个更短的查询,这也应该明显更快,更容易让下一个人理解。

让我们来看看发生了什么。你有两个几乎相同的SELECT粘在一起。假设我们将它们网格化为单个SELECT。

一些逻辑,以CASE表达式的形式,总体上要比两个合并并重新排序的SELECTS少得多。

除非您创建缩写,否则重复每个表名都没有意义:

  FROM SUBSCRIPTION SUBSCRIPTION,

如果在FROM子句中指定连接条件而不是WHERE子句,则通常会认为下一个人更清楚。

SELECT ORGANIZATION.ORGNAME,
       SUBSCRIPTION.ID         AS SUBSCRIPTIONID,
       CASE WHEN SUBSCRIPTION.ISLIMITEDSEAT = 1
            THEN SUBSCRIPTION.NUMBEROFAVAILABLESEATS
            ELSE -1 
        END                    AS AVAILABLESEATS
  FROM ORGANIZATION
  JOIN CUSTOMER
    ON  CUSTOMER.PARTYID = ORGANIZATION.ID
  JOIN CUSTOMERACCOUNT
    ON  CUSTOMERACCOUNT.CUSTOMERID = CUSTOMER.ID
  JOIN SUBSCRIPTION
    AND SUBSCRIPTION.CUSTOMERACCOUNTID = CUSTOMERACCOUNT.ID
  WHERE ORGANIZATION.ISDELETED=0
    AND SUBSCRIPTION.DELETED=0
    AND CUSTOMER.ISDELETED=0
    AND SUBSCRIPTION.ISLIMITEDSEAT IN (0,1) 

如果SUBSCRIPTION.ISLIMITEDSEAT是一个只能为0或1的标志,则删除最后一行。