我可以帮您进行翻译。此文涉及编程方面,您有两个表格,第一个表格包含以下列:
id code Name
1 c1 chk1
2 c2 chk2
3 c3 chk3
第二个表格包含以下列:
id,Name,Chk1,chk2,Chk3
如果table1动态更新为'value '4,'c4','ch4'',我需要将'Chk4'列添加到table2中。请问如何编写存储过程来执行此操作?
我尝试了以下存储过程,但它无法正常工作。
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