【发布时间】:2014-06-14 22:25:15
【问题描述】:
我有 2 个表,第一个表包含以下列,
id code Name
1 c1 chk1
2 c2 chk2
3 c3 chk3
第二个表格包含以下列,
id,Name,Chk1,chk2,Chk3
如果 table1 动态更新值为 '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
【问题讨论】:
-
现在检查我的答案。更新您的程序脚本后,我会更新我的答案。
-
它不工作还不够。请告诉我们执行 SP 时发生了什么
标签: sql sql-server sql-server-2008 sql-server-2008-r2 sql-server-2012