That's not possible. While inserting you need to get only one value for one column.
Either as suggest first check not exist select query and then insert.
yes, you can use case or subquery to get a single value to replace if statement above.
updated
If you wish this type of situation, then you can create dynamic query for this.
--declare table1 table ( id int, value varchar(10) )
--drop table Table1
begin tran
create table table1 ( id int, value varchar(10), value1 varchar(10), value2 varchar(10))
insert into table1 values( 1,'001','A','A1')
insert into table1 values(2, '002','B','B1')
insert into table1 values( 3,'003','C','C1')
-----insert example
--always use nvarchar for dynamic query, other wise gives error
declare @sql nvarchar(max), @columnlist nvarchar(max) = 'id', @columnlistvalue nvarchar(max) ='4'
declare @checkvalue varchar = '1',@checkvalue2 varchar = '2',@checkvalue3 varchar,
@columnvalueFirst varchar(10) = 'this entry done by dynamic query on condition basis'
if(@checkvalue = 1)
begin
set @columnlist = @columnlist + ',value'
set @columnlistvalue = @columnlistvalue + ',''004''' --or you can add @checkvalue
end
if(@checkvalue2 = 2)
begin
--this is example, you can add another condition. Right now if else condition gives same output
if( len(@columnlist) > 0)
begin
set @columnlist = @columnlist + ',value1'
set @columnlistvalue = @columnlistvalue + ',''D''' --or you can add @checkvalue2
end
else
begin
set @columnlist = @columnlist + ',value'
set @columnlistvalue = @columnlistvalue + ',''D'''
end
end
if(@checkvalue3 = 3)
begin
--this is example, you can add another condition. Right now if else condition gives same output
if( len(@columnlist) > 0)
begin
set @columnlist = @columnlist + ',value2'
set @columnlistvalue = @columnlistvalue + ',''D1''' --or you can add @checkvalue3
end
else
begin
set @columnlist = @columnlist + ',value2'
set @columnlistvalue = @columnlistvalue + ',''D1'''
end
end
select * from table1
set @sql = 'insert into table1 ('+ @columnlist +') values ( ' + @columnlistvalue + ')'
select @sql --you can check that what is wrong by
exec sp_executesql @sql
select * from table1
rollback
INSERTstatement with anIFclause - if you need to check something (like the existence of a row), you need to check this first, and then either do yourINSERT(or skip it)