sql server中关键字'as'附近的语法不正确

时间:2011-12-28 09:30:31

标签: sql-server-2005

每当我运行它时,我都有以下查询,它在“As”

附近给出了错误的语法
select 
    count(id_cat) as catcnt,
    id_cat, ccatname, ccatpermalink,
    (select ccatname 
     from btnn_classifiedcategory 
     where syncoperation <>'D' and id_ccat = id_cat) as catname 
from 
    btnn_myclassifides
where 
    syncoperation <> 'D' 
    and Expirydate >= cast(convert(varchar(100), getdate(), 101) as datetime)
    and id_cat in (select id_ccat 
                   from BTNN_ClassifiedCategory 
                   where syncoperation <> 'D' and id_ccat = bcc.id_ccat) as cnt 
                    from BTNN_ClassifiedCategory bcc

这是我的疑问,请帮帮我

2 个答案:

答案 0 :(得分:1)

查询的最后一部分根本不起作用:

    and id_cat in (select id_ccat 
                   from BTNN_ClassifiedCategory 
                   where syncoperation <> 'D' and id_ccat = bcc.id_ccat) as cnt 
                    from BTNN_ClassifiedCategory bcc

此次选择后不应该有as cnt,并且额外的from BTNN_ClassifiedCategory bcc根本不适合...... {/ p>

此查询是否有效:

select 
    count(id_cat) as catcnt,
    id_cat, ccatname, ccatpermalink,
    (select ccatname 
     from btnn_classifiedcategory 
     where syncoperation <>'D' and id_ccat = id_cat) as catname 
from 
    btnn_myclassifides
where 
    syncoperation <> 'D' 
    and Expirydate >= cast(convert(varchar(100), getdate(), 101) as datetime)
    and id_cat in (select id_ccat 
                   from BTNN_ClassifiedCategory AS bcc
                   where syncoperation <> 'D' and id_ccat = bcc.id_ccat)

答案 1 :(得分:0)

您不需要上一个as cnt和最后一个from BTNN_ClassifiedCategory bcc

如果你确保第一个子查询只返回1行,可能会更好:

(select top 1 ccatname 
     from btnn_classifiedcategory 
     where syncoperation <>'D' and id_ccat = id_cat) as catname