Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:


Please provide the approriate syntax for inserting values into a table with a condition

CASE as depicted below



SQL
Insert Into Transactions (Name, PERIOD_COUNT, Act_Prime_Amt, Act_Avg_Amt) 
        Values (Master.Name, lnPeriodCount, 
        CASE WHEN (lnPeriodCount <= Master.Act_Prime_Pd, 0, Master.Act_Prime_Amt), 
        CASE WHEN  (lnPeriodCount <= Master.Act_Avg_Pd, 0, Master.Act_Avg_Amt))
Posted
Updated 28-Nov-14 14:29pm
v4
Comments
PIEBALDconsult 28-Nov-14 20:26pm    
Those semi-colons don't belong in there.
Member 10744248 28-Nov-14 20:30pm    
Is the syntax right please aside of the semi colon
DamithSL 28-Nov-14 21:49pm    
are you insert from another table data?

use SQL INSERT INTO SELECT statement

http://www.w3schools.com/sql/sql_insert_into_select.asp[^]

Eg:
SQL
create table #tbl_Test(id int identity(1,1),name nvarchar(max))
declare @name nvarchar(max) ='';
insert into #tbl_Test(name)select  case when @name ='' then 'Me' else @name end

select *from #tbl_test


Try it :)
 
Share this answer
 
Comments
Maciej Los 29-Nov-14 4:56am    
+5
King Fisher 29-Nov-14 5:05am    
Thank you :)
Try this:
SQL
Insert Into Transactions (Name, PERIOD_COUNT, Act_Prime_Amt, Act_Avg_Amt)
        SELECT Master.Name, lnPeriodCount,
        CASE WHEN lnPeriodCount <= Master.Act_Prime_Pd THEN 0 ELSE Master.Act_Prime_Amt END,
        CASE WHEN  lnPeriodCount <= Master.Act_Avg_Pd THEN 0 ELSE Master.Act_Avg_Amt END


For further information, please see: CASE (Transact-SQL)[^]
 
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