declare @chvname varchar(50), @intmin int , @intmax int create table #test_tmp(months char(4))//创建一个表,该表是目标表,它的字段是 源表WWW字段eqtype 的值 create table #aaa(id smallint identity(1,1),names varchar(20))//创建一个表,是临时表,用于存 //储源表WWW字段eqtype 的值 insert #aaa(names)select eqtype from www//存储表A记录 select @intmin=min(id),@intmax=max(id) from #aaa//取得最大(小)值 while ((@intmin+1)<=@intmax) begin select @chvname=names from #aaa where id=@intmin exec('alter table #test_tmp add '+@chvname +' varchar(10) null') delete from #aaa where id=@intmin//之所以不用id++,是因为id可能不连续 select @intmin=min(id) from #aaa end//至此完成目标表。 -- select * from #aaa select * from #test_tmp drop table #aaa drop table #test_tmp GO
关注……
能否给一个具体的例子啊,谢谢啦
declare @chvname varchar(50),
@intmin int ,
@intmax int
create table #test_tmp(months char(4))//创建一个表,该表是目标表,它的字段是 源表WWW字段eqtype 的值
create table #aaa(id smallint identity(1,1),names varchar(20))//创建一个表,是临时表,用于存
//储源表WWW字段eqtype 的值
insert #aaa(names)select eqtype from www//存储表A记录
select @intmin=min(id),@intmax=max(id) from #aaa//取得最大(小)值 while ((@intmin+1)<=@intmax)
begin
select @chvname=names from #aaa where id=@intmin exec('alter table #test_tmp add '+@chvname +' varchar(10) null') delete from #aaa where id=@intmin//之所以不用id++,是因为id可能不连续
select @intmin=min(id) from #aaa
end//至此完成目标表。
-- select * from #aaa
select * from #test_tmp
drop table #aaa
drop table #test_tmp
GO