SQL查询定义列的别名时出错

时间:2010-09-04 13:00:02

标签: asp.net

我的以下sql查询给出了以下错误,这是什么意思。我是以绞拧的方式定义别名fir柱。这个你能帮我吗。 我的错误就是这个,我把查询放在错误的下方。

Error in SELECT clause: expression near 'Year'.
Missing FROM clause.
Unable to parse query text.



SELECT [tbl_students].passing_year as Passing Year,[tbl_branch].branch_name as Branch_Name FROM [tbl_students], [tbl_course], [tbl_branch] WHERE [tbl_students].course_id=@courseId 
                        AND   [tbl_students].branch_id IN(64) 
                        AND   [tbl_students].course_id=[tbl_course].course_id AND [tbl_students].branch_id=[tbl_branch].branch_id                        
                        AND  (@firstYrPercent is null OR [tbl_students].first_year_percent>=@firstYrPercent)
                        AND  (@secondYrpercent is null OR [tbl_students].second_year_percent>=@secondYrPercent)
                        AND  (@thirdYrPercent is null OR[tbl_students].third_year_percent>=@thirdYrPercent)
                        AND  (@finalYearpercent is null OR [tbl_students].final_year_percent>=@finalYearpercent)
                        AND  (@currentDegeePercentage is null OR [tbl_students].current_degree_percent>=@currentDegeePercentage)
                        AND  (@passoutYear is null OR [tbl_students].passing_year>=@passoutYear) 
                        AND  (@currentBacklog is null OR [tbl_students].current_backlog=@currentBacklog)
                        AND  (@sex is null OR [tbl_students].gender=@sex) 
                        AND  (@eGap is null OR [tbl_students].gapin_education<=@eGap)
                        AND  (@highSchoolPercentge is null OR [tbl_students].highschool_percentage>=@highSchoolPercentge)
                        AND  (@higherSchoolPercentage is null OR [tbl_students].ssc_percentage>=@higherSchoolPercentage)
                        AND  (@grauationPercentage is null OR [tbl_students].graduation_percentage>=@grauationPercentage)
                        AND  (@diplomaPercentage is null OR [tbl_students].diploma_percentage>=@diplomaPercentage)
                        AND  (@noOfAtkt is null OR [tbl_students].number_of_ATKT<=@noOfAtkt)
                        AND  (@validDate is null OR [tbl_students].DOB>=@validDate)

1 个答案:

答案 0 :(得分:2)

您需要使用方括号转义传递年份列(请参阅第一行):

SELECT [tbl_students].passing_year as [Passing Year],
       [tbl_branch].branch_name    as Branch_Name 
FROM  [tbl_students], [tbl_course], [tbl_branch] 
WHERE [tbl_students].course_id=@courseId 
AND   [tbl_students].branch_id IN(64) 
AND   [tbl_students].course_id=[tbl_course].course_id AND [tbl_students].branch_id=[tbl_branch].branch_id                        
AND  (@firstYrPercent is null OR [tbl_students].first_year_percent>=@firstYrPercent)
AND  (@secondYrpercent is null OR [tbl_students].second_year_percent>=@secondYrPercent)
AND  (@thirdYrPercent is null OR[tbl_students].third_year_percent>=@thirdYrPercent)
AND  (@finalYearpercent is null OR [tbl_students].final_year_percent>=@finalYearpercent)
AND  (@currentDegeePercentage is null OR [tbl_students].current_degree_percent>=@currentDegeePercentage)
AND  (@passoutYear is null OR [tbl_students].passing_year>=@passoutYear) 
AND  (@currentBacklog is null OR [tbl_students].current_backlog=@currentBacklog)
AND  (@sex is null OR [tbl_students].gender=@sex) 
AND  (@eGap is null OR [tbl_students].gapin_education<=@eGap)
AND  (@highSchoolPercentge is null OR [tbl_students].highschool_percentage>=@highSchoolPercentge)
AND  (@higherSchoolPercentage is null OR [tbl_students].ssc_percentage>=@higherSchoolPercentage)
AND  (@grauationPercentage is null OR [tbl_students].graduation_percentage>=@grauationPercentage)
AND  (@diplomaPercentage is null OR [tbl_students].diploma_percentage>=@diplomaPercentage)
AND  (@noOfAtkt is null OR [tbl_students].number_of_ATKT<=@noOfAtkt)
AND  (@validDate is null OR [tbl_students].DOB>=@validDate)