SQL Server 2008错误4145

时间:2012-08-04 10:42:18

标签: sql database sql-server-2008 syntax

我在SQL

中使用了以下代码
SELECT 
        Stu_info.Id_num, 
        Stu_info.Stu_name, 
        Development_fee.Dvf, 
        Tuition_fee.Acy, 
        Tuition_fee.Tui_fee, 
        Registration_fee.Reg_fee, 
        Form_fill_up_fee.Acy, 
        Form_fill_up_fee.FFF, 
        Examination_fee.E_typ, 
        Examination_fee.Exm_fee, 
        monthly_instal.Instm, 
        monthly_instal.Paid     

FROM 
        [SUIMT].[dbo].[Stu_info], 
        [SUIMT].[dbo].[Development_fee], 
        [SUIMT].[dbo].[Tuition_fee], 
        [SUIMT].[dbo].[Registration_fee], 
        [SUIMT].[dbo].[Form_fill_up_fee], 
        [SUIMT].[dbo].[Examination_fee], 
        [SUIMT].[dbo].[monthly_instal]
WHERE 
        Development_fee.Id_num, 
        Tuition_fee.Id_num, 
        Registration_fee.Id_num, 
        Form_fill_up_fee.Id_num, 
        Examination_fee.Id_num, 
        monthly_instal.Id_num   = Stu_info.Id_num

但显示错误,表示

"Msg 4145, Level 15, State 1, Line 3
An expression of non-boolean type specified in a context where a
condition is expected, near ','".

请你帮我解决这个问题?我很感激提供一个例子,所以我可以更容易理解。

4 个答案:

答案 0 :(得分:1)

要实现的WHERE条款是什么?你是说这个吗?

WHERE
      Stu_info.Id_num = Development_fee.Id_num
  AND Stu_info.Id_num = Tuition_fee.Id_num
  AND Stu_info.Id_num = Registration_fee.Id_num
  AND Stu_info.Id_num = Form_fill_up_fee.Id_num
  AND Stu_info.Id_num = Examination_fee.Id_num
  AND Stu_info.Id_num = monthly_instal.Id_num

此外,使用FROM tableA, TableB, TableC是一种非常古老的符号表示法。 ANSI-92标准使用JOIN ...

SELECT
  Stu_info.Id_num, Stu_info.Stu_name, Development_fee.Dvf, Tuition_fee.Acy, Tuition_fee.Tui_fee, Registration_fee.Reg_fee, Form_fill_up_fee.Acy, Form_fill_up_fee.FFF, Examination_fee.E_typ, Examination_fee.Exm_fee, monthly_instal.Instm, monthly_instal.Paid     
FROM
  [SUIMT].[dbo].[Stu_info]
INNER JOIN
  [SUIMT].[dbo].[Development_fee]
    ON Stu_info.Id_num = Development_fee.Id_num
INNER JOIN
  [SUIMT].[dbo].[Tuition_fee]
    ON Stu_info.Id_num = Tuition_fee.Id_num
INNER JOIN
  [SUIMT].[dbo].[Registration_fee]
    ON Stu_info.Id_num = Registration_fee.Id_num
INNER JOIN
  [SUIMT].[dbo].[Form_fill_up_fee]
    ON Stu_info.Id_num = Form_fill_up_fee.Id_num
INNER JOIN
  [SUIMT].[dbo].[Examination_fee]
    ON Stu_info.Id_num = Examination_fee.Id_num
INNER JOIN
  [SUIMT].[dbo].[monthly_instal]
    ON Stu_info.Id_num = monthly_instal.Id_num

答案 1 :(得分:1)

您应该使用正确的ANSI SQL-92 JOIN语法INNER JOIN, LEFT OUTER JOIN等)和表别名,以使您的查询更易理解和更易于阅读 - 例如:

SELECT 
    stu.Id_num, stu.Stu_name, 
    df.Dvf, 
    tf.Acy, tf.Tui_fee, 
    rf.Reg_fee, 
    ffuf.Acy, ffuf.FFF, 
    ef.E_typ, ef.Exm_fee, 
    mi.Instm, mi.Paid     
FROM 
    [SUIMT].[dbo].[Stu_info] stu
INNER JOIN 
    [SUIMT].[dbo].[Development_fee] df ON df.Id_num = stu.id_num
INNER JOIN 
    [SUIMT].[dbo].[Tuition_fee] tf ON tf.Id_num = stu.id_num
INNER JOIN 
    [SUIMT].[dbo].[Registration_fee] rf ON rf.Id_num = stu.id_num
INNER JOIN  
    [SUIMT].[dbo].[Form_fill_up_fee] ffuf ON ffuf.Id_num = stu.id_num
INNER JOIN 
    [SUIMT].[dbo].[Examination_fee] ef ON ef.Id_num = stu.id_num
INNER JOIN  
    [SUIMT].[dbo].[monthly_instal] mi ON mi.Id_Num = stu.Id_num

这样,你根本不需要一个丑陋的WHERE条款......

答案 2 :(得分:0)

将您的条款分为ANDOR,而不是逗号

答案 3 :(得分:0)

SELECT Stu_info.Id_num, Stu_info.Stu_name, Development_fee.Dvf, Tuition_fee.Acy, Tuition_fee.Tui_fee, Registration_fee.Reg_fee, Form_fill_up_fee.Acy, Form_fill_up_fee.FFF, Examination_fee.E_typ, Examination_fee.Exm_fee, monthly_instal.Instm, monthly_instal.Paid     
FROM [SUIMT].[dbo].[Stu_info], 
    [SUIMT].[dbo].[Development_fee], 
    [SUIMT].[dbo].[Tuition_fee], 
    [SUIMT].[dbo].[Registration_fee], 
    [SUIMT].[dbo].[Form_fill_up_fee], 
    [SUIMT].[dbo].[Examination_fee], 
    [SUIMT].[dbo].[monthly_instal]
WHERE Development_fee.Id_num=Tuition_fee.Id_num and 
      Registration_fee.Id_num=Form_fill_up_fee.Id_num and 
      Examination_fee.Id_num=monthly_instal.Id_num  and
      monthly_instal.Id_num=Stu_info.Id_num