SQLServer 创建表 时候 循环插入n 个字段?

在 sql 语句 的create table 时 插入 n个不知道 的字段,,怎么插入????
回复下面哥们:
declare @str varchar(1000);

select @str='create table ttt(id1 int,id2 int,id3 int.....);'

exec(@str);

你这个
select @str='create table ttt(id1 int,id2 int,id3 int.....);'
依然是有限的啊。。。我说的是ttt()括号里如果 有10000个字段呢??难道写10000个吗??
非常感谢您的回答,如果有办法,请再回答一下吧谢谢!

动态的把你的sql拼出来,然后用exec()调用。

例如:
declare @str varchar(1000);
select @str='create table ttt(id1 int,id2 int,id3 int.....);'
exec(@str);

************
补充:
************
1、建一个表,保存你要追加的字段名和字段类型
如:create table t_tmp(id int IDENTITY(1,1) not null,col_name varchar(100),col_type varchar(100));

2、把你想要动态追加的字段和类型全写入表中
如:
insert into t_tmp(col_name,col_type) values('t1','int');
insert into t_tmp(col_name,col_type) values('t2','date');
insert into t_tmp(col_name,col_type) values('t3','varchar(100)');
insert into t_tmp(col_name,col_type) values('t4','char(100)');

3、生成动态建表语句,并创建表
declare @str varchar(2000)
set @str='create table ttt('
select @str=@str + col_name+' '+col_type+',' from t_tmp
set @str=substring(@str,1,len(@str)-1)+')'
select @str
exec(@str)

---
以上,希望对你有所帮助。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-10-01
l don know
第2个回答  2009-09-28
mark
相似回答