Click here to Skip to main content
15,939,772 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table. In which so many columns like Empcode,FinancialYear, Quarter1,Quarter2,Quarter3,Quarter4 etc.
I want to insert data based on condition so i want dynamicalyy insert column..
like

SQL
if(@Month=1)
 begin
 set @Quart='Quarter'+4
 end
 if(@Month=4)
 begin
 set @Quart='Quarter'+1
 end
 if(@Month=7)
 begin
 set @Quart='Quarter'+2
 end
 if(@Month=10)
 begin
 set @Quart='Quarter'+3
 end


insert into Direct_Summary_Policies(EmpCode,Year,financialYear,@Quart) values('E28619',DATEPART(YEAR,@FROMDATE),@FinYaer,56000)
Posted

Create Procedure and Insert values conditionally.
pass parameters for condition.
 
Share this answer
 
Try doing this

SQL
DECLARE @SQLString nvarchar(500);

SET @SQLString =
     N'insert into Direct_Summary_Policies(EmpCode,Year,financialYear,@Quart) values('E28619',DATEPART(YEAR,@FROMDATE),@FinYaer,56000)';

SET @ParmDefinition = N'@Quart VARCHAR(30), @FROMDATE DATETIME, @FinYaer INT';

EXECUTE sp_executesql @SQLString, @ParmDefinition, @Quart, @FROMDATE, @FinYaer
 
Share this answer
 

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