在SQL查询中分配值

时间:2013-04-26 06:24:59

标签: sql db2

我有以下查询

  SELECT
  R.CATENTRY_ID_PARENT AS PARENT,
  R.CATENTRY_ID_CHILD AS CHILD,
D.NAME AS NAME, 
INTEGERVALUE AS INTEGERVALUE,STRINGVALUE AS STRINGVALUE,
                D.SHORTDESCRIPTION AS SDESC, D.LONGDESCRIPTION AS LDESC, D.keyword AS KEYWORD, C.MFPARTNUMBER AS HOLIMAGEPATH, avl.topimage AS THUMB,
                    (case when (exists (Select INTEGERVALUE from xstoreattrval M1 where M1.store_id =10051 and M1.xstoreattr_id in (select xstoreattr_id from xstoreattr M2 where M2.name in ('hasClubExlusives')))) then INTEGERVALUE else null end) as INTEGERVALUE,
                    (case when (exists (Select STRINGVALUE from attrvalue M3 where M3.attribute_id in (select attribute_id from attribute M4 where M4.name in ('clubExclusive') and M4.catentry_id=CAT1ID))) then STRINGVALUE else null end) as STRINGVALUE,
                    C.CATENTRY_ID as CAT1ID
                    FROM CATENTREL R, CATENTRY C, CATENTDESC D, XCATENTAVL avl,                 
                    (Select INTEGERVALUE from xstoreattrval M1 where M1.store_id =10051 and M1.xstoreattr_id in (select xstoreattr_id from xstoreattr M2 where M2.name in ('hasClubExlusives'))) as INTEGERVALUE,
                    (Select STRINGVALUE from attrvalue M3 where M3.attribute_id in (select attribute_id from attribute M4 where M4.name in ('clubExclusive') and M4.catentry_id=CAT1ID)) as STRINGVALUE
                    --INNER JOIN ATTRVALUE V ON  V.CATENTRY_ID = C.CATENTRY_ID 
                    --INNER JOIN ATTRIBUTE AT ON V.ATTRIBUTE_ID = AT.ATTRIBUTE_ID       
                    WHERE C.CATENTRY_ID = D.CATENTRY_ID                     
                    AND C.BUYABLE = 1 
                    AND C.MARKFORDELETE = 0 
                    AND D.PUBLISHED = 1 
                    AND R.CATENTRY_ID_PARENT = C.CATENTRY_ID 
                    --AND NOT (INTEGERVALUE1 = '0' AND STRINGVALUE2='true')
                    AND C.CATENTRY_ID = AVL.CATENTRY_ID 
                    AND C.CATENTTYPE_ID = 'ProductBean' 
                    AND R.CATENTRY_ID_PARENT IN ( 
                    SELECT DISTINCT CC.CATENTRY_ID 
                    FROM CATENTRY CC, CATGPENREL RR, CATGROUP GG, CATGRPDESC DD 
                    WHERE RR.CATALOG_ID =  10901
                    AND CC.CATENTRY_ID = RR.CATENTRY_ID 
                    AND GG.CATGROUP_ID = RR.CATGROUP_ID 
                    AND GG.CATGROUP_ID = DD.CATGROUP_ID 
                    AND DD.NAME NOT LIKE 'PULLED%'  
                    ) ORDER BY R.CATENTRY_ID_PARENT FOR READ ONLY WITH UR

然而,在第二种情况下,CAT1ID的分配不起作用。它抛出如下错误

Error: DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703, SQLERRMC=CAT1ID, DRIVER=4.1.85

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

在你的第二个CASE中有最内在的查询

select attribute_id from attribute M4 where M4.name in ('clubExclusive') and M4.catentry_id=CAT1ID

并且在此查询中,sql正在尝试在CAT1D表中查找atrribute。实际上,只有在大多数查询中才有CAT1D字段。

您可以使用CURSOR并将CAT1D值转换为变量,然后在主查询中使用。