Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-server-2005
//Below is my code for my solution
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) 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)
begin
select 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'
end
else 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)
begin
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)
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) 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'
end
else(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)
begin
select 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'
end
end
else
begin
select 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 facing
 
Msg 4145, Level 15, State 1, Line 1
An expression of non-boolean type specified in a context where a condition is expected, near 'and'.
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'else'.
Msg 4145, Level 15, State 1, Line 5
An expression of non-boolean type specified in a context where a condition is expected, near 'or'.
Msg 4145, Level 15, State 1, Line 8
An expression of non-boolean type specified in a context where a condition is expected, near 'begin'.
Msg 156, Level 15, State 1, Line 11
Incorrect syntax near the keyword 'else'.
Msg 156, Level 15, State 1, Line 16
Incorrect syntax near the keyword 'else'.
Posted 7-Mar-13 18:53pm
ntitish1.5K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi
Where you are comparing values... ? and your Query also return More than one columns
 
 
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) =? 
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,
GVPrabu
  Permalink  
v3
Comments
ntitish at 8-Mar-13 0:05am
   
sir cant we use two select statements in if condition foe example like below
if (select * from student_details where student_id='1001') and (select * from student_details where student_name='pavan')
begin
 
end
else
begin
 
end
ntitish at 8-Mar-13 0:09am
   
actually my problem is
(select * from student_details where student_id='1001') having zero records
(select * from student_details where student_name='pavan') having Zero records means
it should not go inside of the if condition
gvprabu at 8-Mar-13 0:14am
   
Check the IF EXISTS .... why u need to take all column for checking
ntitish at 8-Mar-13 0:25am
   
//for example the below code will work ha sir
 
if exists(select * from student_details where student_id='1001') and (select * from student_details where student_name='pavan')
begin
select * from student_details where student_id='1001' and student_Fee_Type='annual'
end
else if exists(select * from student_details where student_id='1001') or (select * from student_details where student_name='pavan')
begin
select * from student_details where student_id='1001' and student_Fee_Type='month'
end
else
begin
select* from student_details
end
 
//the above syntax is correct ha sir...
gvprabu at 8-Mar-13 0:48am
   
Try this...
 
if exists(select 1 from student_details where student_id='1001' AND student_name='pavan')
begin
select Column_Name from student_details where student_id='1001' and student_Fee_Type='annual'
end
else if exists(select 1 from student_details where student_id='1002' AND student_name='suresh')
begin
select Column_Name from student_details where student_id='1001' and student_Fee_Type='month'
end
else
begin
select Column_Name from student_details
end
ntitish at 8-Mar-13 1:11am
   
thanks sir i am getting the solution thanks for spending your valuable time on this error
gvprabu at 8-Mar-13 1:27am
   
Hi Nitish,
Read some basic TSQL Programming... then it will help you... welcome
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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) 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))
begin
select 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'
end
else 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))
begin
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)
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) 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'
end
else 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)
begin
select 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'
end
end
else
begin
select 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
  Permalink  
v3
Comments
ntitish at 8-Mar-13 0:14am
   
Msg 4145, Level 15, State 1, Line 2
An expression of non-boolean type specified in a context where a condition is expected, near 'begin'.
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'else'.
Msg 4145, Level 15, State 1, Line 6
An expression of non-boolean type specified in a context where a condition is expected, near 'begin'.
Msg 156, Level 15, State 1, Line 16
Incorrect syntax near the keyword 'else'.
 

//these are my errors...
Davidduraisamy at 8-Mar-13 0:31am
   
Check updated code and let me know the issue..
ntitish at 8-Mar-13 0:43am
   
sir in my query i am not using update query sir...
i am minimizing my code and i am asking my problem in a different manner...
for example the below code i have to execute..
 
if exists(select * from student_details where student_id='1001') and (select * from student_details where student_name='pavan')
begin
select * from student_details where student_id='1001' and student_Fee_Type='annual'
end
else if exists(select * from student_details where student_id='1001') or (select * from student_details where student_name='pavan')
begin
select * from student_details where student_id='1001' and student_Fee_Type='month' end
else
begin select* from student_details
end

//in if i am using two select statements with 'and' operator
if there is no records in one select statement also it should not go inside into if condition.or if there are records in both the select statements means it should go into if condition
//in else if i am using again two select statements with 'or' operator
if there is no records in both the select statements means it should not go inside the else if condition.or if there is a record in one select statement means it should go inside the else if condition
ntitish at 8-Mar-13 1:11am
   
thanks sir i am getting the solution thanks for spending your valuable time on this error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Kornfeld Eliyahu Peter 169
1 George Jonsson 145
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 8 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100