动态的把你的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)
---
以上,希望对你有所帮助。
温馨提示:答案为网友推荐,仅供参考