提供个思路declare @aa int
if (@aa=1)
begin
create .....
end 
else
begin
create .....
end

解决方案 »

  1.   

    create proc p_createtable 
    @列数 int=1
    as
    declare @s varchar(8000)
    set @s=''
    while @i>0
    select @s=',列'+cast(@i as varchar)+' nvarchar(500)'+@s
    ,@i=@i-1
    set @s=substring(@s,2,8000)
    exec('create table 表('+@s+')')
      

  2.   

    --写错变量,改一下:create proc p_createtable 
    @列数 int=1
    as
    declare @s varchar(8000)
    set @s=''
    while @列数>0
    select @s=',列'+cast(@列数 as varchar)+' nvarchar(500)'+@s
    ,@列数=@列数-1
    set @s=substring(@s,2,8000)
    exec('create table 表('+@s+')')
    go
      

  3.   

    --测试
    create proc p_createtable 
    @列数 int=1
    as
    declare @s varchar(8000)
    set @s=''
    while @列数>0
    select @s=',列'+cast(@列数 as varchar)+' nvarchar(500)'+@s
    ,@列数=@列数-1
    set @s=substring(@s,2,8000)
    exec('create table 表('+@s+')')
    go--调用
    exec p_createtable 3
    go--显示创建的表
    select * from 表
    go--删除测试
    drop table 表
    drop proc p_createtable/*--测试结果
    列1    列2    列3    
    ----- ----- ----- (所影响的行数为 0 行)
    --*/
      

  4.   

    --更加灵活一点的,可以指定要创建的表名,及字段的前缀create proc p_createtable 
    @列数 int=1,
    @表名 sysname='表',
    @字段前缀 sysname='列'
    as
    declare @s varchar(8000)
    set @s=''
    while @列数>0
    select @s=',['+@字段前缀+cast(@列数 as varchar)+'] nvarchar(500)'+@s
    ,@列数=@列数-1
    set @s=substring(@s,2,8000)
    exec('create table ['+@表名+']('+@s+')')
    go
      

  5.   

    帅呆了~  高手就是不一样!
    可是我看了您写的N遍,也还不是太明白是如何实现的.特别是下面这句不知是什么意思?
    select @s=',['+@字段前缀+cast(@列数 as varchar)+'] nvarchar(500)'+@s
    ,@列数=@列数-1
      

  6.   


    declare @sql varchar(1000)
    declare @int int
    set @int=5
    set @sql=''set @sql='create table tabNew( ' 
    while @int>0 
    begin
    select @sql=@sql+ '列'+cast(@int as nvarchar)+' nvarchar(500),'
    set @int=@int-1
    end
    set @sql=left(@sql,len(@sql)-1)
    set @sql=@sql+')'
    exec (@sql)
      

  7.   

    --这两句配合,实现循环while @列数>0 
    select @s=',['+@字段前缀+cast(@列数 as varchar)+'] nvarchar(500)'+@s
    ,@列数=@列数-1