Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello Coders, I have the below-mentioned query but It is not executed & throwing error at 'case' and 'end' word. plz help.

Based on condition I want to run Insert Query.

Thanks In Advance

What I have tried:

declare @holidaydt as date='28-Mar-2020'
declare @lastworkingdt as date='27-Mar-2020'
declare @tml as int
select @tml=count(*) from Balances where TrnDate=@holidaydt

CASE when @tml >0 then 'Do Nothing' WHEN @tml=0 THEN 
insert  into balances(acno,TrnDate,OpeningBalance,ClosingBalance,Branchcode)
(select acno,@holidaydt,OpeningBalance,ClosingBalance,Branchcode from balances where TrnDate=@lastworkingdt) END
Posted
Updated 25-Apr-21 2:45am
Comments
Richard MacCutchan 25-Apr-21 6:54am    
What error?
Hemil Gandhi 25-Apr-21 7:38am    
Incorrect Syntax near "CASE"
Incorrect Syntax near "END"
Richard MacCutchan 25-Apr-21 7:55am    
Check the syntax in the documentation for the version of SQL that you are using. I do not think that a complete SELECT statement can be followed by a CASE clause.

1 solution

If I understand your problem correctly you can use IF instead of CASE. For example
SQL
declare @holidaydt as date='28-Mar-2020'
declare @lastworkingdt as date='27-Mar-2020'
declare @tml as int
select @tml=count(*) from Balances where TrnDate=@holidaydt

IF @tml=0 BEGIN
   insert  into balances(acno,TrnDate,OpeningBalance,ClosingBalance,Branchcode)
   select acno,@holidaydt,OpeningBalance,ClosingBalance,Branchcode 
   from balances 
   where TrnDate=@lastworkingdt;
END
 
Share this answer
 
v2

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