I have 2 tables 1st table contains following columns,
id code Name
1 c1 chk1
2 c2 chk2
3 c3 chk3
2nd table contains following columns,
id,Name,Chk1,chk2,Chk3
i have to add the column 'Chk4' into table2 if table1 is updated with value '4,'c4','ch4' dynamically.How to write procedure to perform this?
i've tried the following procedure but its not working fine.
create proc Add_Check
as
begin
declare @Column varchar(50)
declare @query varchar(255)
declare @query1 varchar(255)
set @Column= (select top 1 QUOTENAME(Name)
from table1 where id=(Select MAX id) from table1))
if exists(select 1 from table1
where Name=@Column)
begin
set @query = 'alter table table2 add ' + @Column + ' Varchar (50)'
set @query1 = 'alter table table2 add ' + @Column + '_CompletedDate Varchar (50)'
exec(@query)
end
end
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…