Create multiple columns in dynamic way and perform update operation on it :
create table #temp(Name varchar(30))
insert into #temp(Name) values('Masum')
select * from #temp
DECLARE @i INT=1
declare @x int
select @x=5
declare @n varchar(max)
set @n='REZA'
WHILE @i<=@x
BEGIN
EXEC ('ALTER TABLE #temp ADD Col'+@i+' VARCHAR(100);')
exec ('Update #temp set Col'+@i+'=''' + @n + '''')
SET @i=@i+1
END
create table #temp(Name varchar(30))
insert into #temp(Name) values('Masum')
select * from #temp
DECLARE @i INT=1
declare @x int
select @x=5
declare @n varchar(max)
set @n='REZA'
WHILE @i<=@x
BEGIN
EXEC ('ALTER TABLE #temp ADD Col'+@i+' VARCHAR(100);')
exec ('Update #temp set Col'+@i+'=''' + @n + '''')
SET @i=@i+1
END
No comments:
Post a Comment