Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
//Below is my code for my solution
SQL
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

Hi
Where you are comparing values... ? and your Query also return More than one columns

SQL
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
 
Share this answer
 
v3
Comments
ntitish 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 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 8-Mar-13 0:14am    
Check the IF EXISTS .... why u need to take all column for checking
ntitish 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 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
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
 
Share this answer
 
v3
Comments
ntitish 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 8-Mar-13 0:31am    
Check updated code and let me know the issue..
ntitish 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900