在预期条件的上下文中指定的非布尔类型的表达式,在''begin'附近。 消息156,级别15,状态1,行11 关键字''else''附近的语法不正确。 消息156,级别15,状态1,行16 关键字附近的语法不正确'else''。解决方案 您好 您在哪里比较价值......?并且您的查询还返回多个列 如果( SELECT Student_ID,Fee_Type,SUM(金额) AS 已付 FROM FeePaid_Details WHERE (Student_ID = ' PS20130001') AND (Fee_Type = ' 入场费') AND (Month_Details = ' jan') GROUP BY Student_ID,Fee_Type) =? 和( SELECT Student_ID,Fee_Type,SUM(金额) AS 已付款 FROM FeePaid_Details WHERE (Student_ID = ' PS20130001') AND (Fee_Type = ' 入场费') AND (Month_Details = ' feb') GROUP BY Student_ID,Fee_Type) =? 您的查询本身就是错误的...... 。 查看链接... IF ... ELSE(Transact-SQL) [ ^ ] SQL IF ... ELSE声明 [ ^ ] 问候, GVPrabu 如果存在((SELECT Student_ID,Fee_Type,SUM(金额)AS从FeePaid_Details支付WHERE(Student_ID =''PS20130001'')和(Fee_Type =''入场费'')和(Month_Details =''jan'')GROUP BY Student_ID,Fee_Type)和(SELECT Student_ID,Fee_Type, SUM(金额)AS从FeePaid_Details支付WHERE(Student_ID ='''PS20130001'')和(Fee_Type =''入场费'')和(Month_Details =''feb'')GROUP BY Student_ID,Fee_Type)) begin 选择q.Student_ID,q.Fee_Type,q.amount,q.amount-p.paid as Current_month_due,q.amount-g.paid as previous_month_due,(q.amount -g.paid)+(q.amount-p.paid)作为来自Student_Fee_Quotations的join_amount q join(选择d.Student_ID,d.Fee_Type,sum(d.amount)从FeePaid_Details d支付,其中d.Studen t_ID ='''PS20130001''和d.Fee_Type =''入场费''和d.Month_Details =''jan''组由d.Student_ID,d.Fee_Type)为g on q.Student_ID =''PS20130001''和q.Fee_Type =''入场费''加入(选择d.Student_ID,d.Fee_Type,sum(d.amount)从FeePaid_Details d支付,其中d.Student_ID =''PS20130001''和d.Fee_Type =''入场费''和d.Month_Details =''feb''组由d.Student_ID,d.Fee_Type)作为p on q.Student_ID ='''PS20130001''和q.Fee_Type =''入场费''br / > 结束 否则存在((SELECT Student_ID,Fee_Type,SUM(金额)AS从FeePaid_Details支付WHERE(Student_ID ='''PS20130001'')和(Fee_Type =''入场费'')AND(Month_Details =''jan'')GROUP BY Student_ID,Fee_Type)或(SELECT Student_ID,Fee_Type,SUM(金额)AS从FeePaid_Details支付WHERE(Student_ID ='''PS20130001'')和(Fee_Type =''入场费'')和(Month_Details =''f eb'')GROUP BY Student_ID,Fee_Type)) begin 如果存在(SELECT Student_ID,Fee_Type,SUM(金额)AS从FeePaid_Details支付WHERE(Student_ID ='' PS20130001'')AND(Fee_Type =''入场费'')和(Month_Details =''jan'')GROUP BY Student_ID,Fee_Type) begin select q .Student_ID,q.Fee_Type,q.amount,q.amount as Current_month_due,q.amount-g.paid as previous_month_due,(q.amount-g.paid)+(q.amount)as total_amount from Student_Fee_Quotations q join(select d.Student_ID,d.Fee_Type,sum(d.amount)从FeePaid_Details d支付,其中d.Student_ID =''PS20130001''和d.Fee_Type =''入场费''和d.Month_Details =''jan'' d.Student_ID,d.Fee_Type)分组为q.Student_ID =''PS20130001''和q.Fee_Type =''入场费'' 结束 否则如果存在(SELECT Student_ID,Fee_Type,SUM(金额)AS从FeePaid_Details WHERE(Student_ID ='''PS20130001'')和(Fee_Ty)支付pe =''入场费'')AND(Month_Details =''feb'')GROUP BY Student_ID,Fee_Type) begin select q.Student_ID,q.Fee_Type ,q.amount,q.amount-p.paid作为Current_month_due,0作为previous_month_due,(q.amount-p.paid)作为来自Student_Fee_Quotations的join_amount q join(选择d.Student_ID,d.Fee_Type,sum(d.amount)从FeePaid_Details d支付,其中d.Student_ID =''PS20130001''和d.Fee_Type =''入场费''和d.Month_Details =''feb''组由d.Student_ID,d.Fee_Type)作为p on q .Student_ID ='''PS20130001''和q.Fee_Type =''入场费'' 结束 结束 其他 begin 选择Student_ID,Fee_Type,金额,金额为Current_month_due,0为previous_month_due,金额为来自Student_Fee_Quotations的total_amount,其中Student_ID ='''PS20130001''和Fee_Type =''入场费'' 结束 //Below is my code for my solutionif (SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = 'PS20130001') AND (Fee_Type = 'Admission Fee') AND (Month_Details = 'jan') GROUP BY Student_ID, Fee_Type) and (SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = 'PS20130001') AND (Fee_Type = 'Admission Fee') AND (Month_Details = 'feb') GROUP BY Student_ID, Fee_Type)beginselect q.Student_ID,q.Fee_Type,q.amount,q.amount-p.paid as Current_month_due,q.amount-g.paid as previous_month_due,(q.amount-g.paid)+(q.amount-p.paid) as total_amount from Student_Fee_Quotations q join(select d.Student_ID,d.Fee_Type,sum(d.amount) as paid from FeePaid_Details d where d.Student_ID='PS20130001' and d.Fee_Type='Admission Fee' and d.Month_Details='jan' group by d.Student_ID,d.Fee_Type )as g on q.Student_ID='PS20130001' and q.Fee_Type='Admission Fee' join (select d.Student_ID,d.Fee_Type,sum(d.amount) as paid from FeePaid_Details d where d.Student_ID='PS20130001' and d.Fee_Type='Admission Fee' and d.Month_Details='feb' group by d.Student_ID,d.Fee_Type ) as p on q.Student_ID='PS20130001' and q.Fee_Type='Admission Fee'endelse if (SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = 'PS20130001') AND (Fee_Type = 'Admission Fee') AND (Month_Details = 'jan') GROUP BY Student_ID, Fee_Type) or (SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = 'PS20130001') AND (Fee_Type = 'Admission Fee') AND (Month_Details = 'feb') GROUP BY Student_ID, Fee_Type)beginif (SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = 'PS20130001') AND (Fee_Type = 'Admission Fee') AND (Month_Details = 'jan') GROUP BY Student_ID, Fee_Type)beginselect q.Student_ID,q.Fee_Type,q.amount,q.amount as Current_month_due,q.amount-g.paid as previous_month_due,(q.amount-g.paid)+(q.amount) as total_amount from Student_Fee_Quotations q join(select d.Student_ID,d.Fee_Type,sum(d.amount) as paid from FeePaid_Details d where d.Student_ID='PS20130001' and d.Fee_Type='Admission Fee' and d.Month_Details='jan' group by d.Student_ID,d.Fee_Type )as g on q.Student_ID='PS20130001' and q.Fee_Type='Admission Fee'endelse(SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = 'PS20130001') AND (Fee_Type = 'Admission Fee') AND (Month_Details = 'feb') GROUP BY Student_ID, Fee_Type)beginselect q.Student_ID,q.Fee_Type,q.amount,q.amount-p.paid as Current_month_due,0 as previous_month_due,(q.amount-p.paid) as total_amount from Student_Fee_Quotations q join(select d.Student_ID,d.Fee_Type,sum(d.amount) as paid from FeePaid_Details d where d.Student_ID='PS20130001' and d.Fee_Type='Admission Fee' and d.Month_Details='feb' group by d.Student_ID,d.Fee_Type ) as p on q.Student_ID='PS20130001' and q.Fee_Type='Admission Fee'endendelsebeginselect Student_ID,Fee_Type,amount,amount as Current_month_due,0 as previous_month_due,amount as total_amount from Student_Fee_Quotations where Student_ID='PS20130001' and Fee_Type='Admission Fee'end//after executing this these are the errors i am facingMsg 4145, Level 15, State 1, Line 1An expression of non-boolean type specified in a context where a condition is expected, near ''and''.Msg 156, Level 15, State 1, Line 5Incorrect syntax near the keyword ''else''.Msg 4145, Level 15, State 1, Line 5An expression of non-boolean type specified in a context where a condition is expected, near ''or''.Msg 4145, Level 15, State 1, Line 8An expression of non-boolean type specified in a context where a condition is expected, near ''begin''.Msg 156, Level 15, State 1, Line 11Incorrect syntax near the keyword ''else''.Msg 156, Level 15, State 1, Line 16Incorrect syntax near the keyword ''else''. 解决方案 Hi Where you are comparing values... ? and your Query also return More than one columnsif (SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = 'PS20130001') AND (Fee_Type = 'Admission Fee') AND (Month_Details = 'jan') GROUP BY Student_ID, Fee_Type) =? and (SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = 'PS20130001') AND (Fee_Type = 'Admission Fee') AND (Month_Details = 'feb') GROUP BY Student_ID, Fee_Type) =?Your Query it self wrong....Check the links... IF...ELSE (Transact-SQL)[^]SQL IF...ELSE Statement[^]Regards,GVPrabuif exists((SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = ''PS20130001'') AND (Fee_Type = ''Admission Fee'') AND (Month_Details = ''jan'') GROUP BY Student_ID, Fee_Type) and (SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = ''PS20130001'') AND (Fee_Type = ''Admission Fee'') AND (Month_Details = ''feb'') GROUP BY Student_ID, Fee_Type))beginselect q.Student_ID,q.Fee_Type,q.amount,q.amount-p.paid as Current_month_due,q.amount-g.paid as previous_month_due,(q.amount-g.paid)+(q.amount-p.paid) as total_amount from Student_Fee_Quotations q join(select d.Student_ID,d.Fee_Type,sum(d.amount) as paid from FeePaid_Details d where d.Student_ID=''PS20130001'' and d.Fee_Type=''Admission Fee'' and d.Month_Details=''jan'' group by d.Student_ID,d.Fee_Type )as g on q.Student_ID=''PS20130001'' and q.Fee_Type=''Admission Fee'' join (select d.Student_ID,d.Fee_Type,sum(d.amount) as paid from FeePaid_Details d where d.Student_ID=''PS20130001'' and d.Fee_Type=''Admission Fee'' and d.Month_Details=''feb'' group by d.Student_ID,d.Fee_Type ) as p on q.Student_ID=''PS20130001'' and q.Fee_Type=''Admission Fee''endelse if exists((SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = ''PS20130001'') AND (Fee_Type = ''Admission Fee'') AND (Month_Details = ''jan'') GROUP BY Student_ID, Fee_Type) or (SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = ''PS20130001'') AND (Fee_Type = ''Admission Fee'') AND (Month_Details = ''feb'') GROUP BY Student_ID, Fee_Type))beginif exists(SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = ''PS20130001'') AND (Fee_Type = ''Admission Fee'') AND (Month_Details = ''jan'') GROUP BY Student_ID, Fee_Type)beginselect q.Student_ID,q.Fee_Type,q.amount,q.amount as Current_month_due,q.amount-g.paid as previous_month_due,(q.amount-g.paid)+(q.amount) as total_amount from Student_Fee_Quotations q join(select d.Student_ID,d.Fee_Type,sum(d.amount) as paid from FeePaid_Details d where d.Student_ID=''PS20130001'' and d.Fee_Type=''Admission Fee'' and d.Month_Details=''jan'' group by d.Student_ID,d.Fee_Type )as g on q.Student_ID=''PS20130001'' and q.Fee_Type=''Admission Fee''endelse if exists(SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = ''PS20130001'') AND (Fee_Type = ''Admission Fee'') AND (Month_Details = ''feb'') GROUP BY Student_ID, Fee_Type)beginselect q.Student_ID,q.Fee_Type,q.amount,q.amount-p.paid as Current_month_due,0 as previous_month_due,(q.amount-p.paid) as total_amount from Student_Fee_Quotations q join(select d.Student_ID,d.Fee_Type,sum(d.amount) as paid from FeePaid_Details d where d.Student_ID=''PS20130001'' and d.Fee_Type=''Admission Fee'' and d.Month_Details=''feb'' group by d.Student_ID,d.Fee_Type ) as p on q.Student_ID=''PS20130001'' and q.Fee_Type=''Admission Fee''endendelsebeginselect Student_ID,Fee_Type,amount,amount as Current_month_due,0 as previous_month_due,amount as total_amount from Student_Fee_Quotations where Student_ID=''PS20130001'' and Fee_Type=''Admission Fee''end 这篇关于在if函数中面对SQL查询中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 20:16