我要在存储过程中创建一个表,表名有参数给出
但这样有错误,请问该怎么实现?

解决方案 »

  1.   

    declare @tname varchar(20),@sql varchar(4000)
    set @tname = 'test'
    set @sql='create table '+@tname+'(id int,code varchar(10))'
    exec(@sql)
      

  2.   

    exec('create table ' + @tablename + '(……)')
      

  3.   

    有抢分嫌疑:
    declare @tbname nvarchar(50)
    set @tbname ='mytest'
    if exists (select * from sysobjects where id = object_id(N'+@tbname +') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    begin
    print '表已经存在'
    return
    end
    else
    exec sp_executesql 'create table @mytable (id int,a varchar(50))',N'@mytable nvarchar(50)',@tbname