我的一个存储过程中,需要将一个固定的表名改为变量,请问如何编写呢?
例如 
if  object_id('mytemp') is not null
    drop table mytemp以上的mytemp表名要改为可赋值的变量,存储过程有参数,参数之一就是表名.谢谢

解决方案 »

  1.   

    对SQL2005不太熟,请各位帮帮忙.
    本人是想在此存储过程中实现不同的用户调用时产生不同的数据表,这样好跟踪等等.
    if  object_id('mytemp') is not null 
        drop table mytemp 
    select * into mytemp from ddtt
      

  2.   

    exec ' drop table '+ @mytemp 
      

  3.   


    谢谢,
    select * into mytemp from ddtt 
    又怎改?
      

  4.   

    if  object_id('mytemp') is not null 这里的表名呢?
      

  5.   


    if object_id(@TableName) is not null 
      

  6.   

    EXEC 'select * into mytemp from '+@mytemp 
      

  7.   

    用exec来执行一个sql的字串
    EXEC 'select * from '+@mytemp
      

  8.   


    create proc p1(@tableName nvarchar(128)) as
    begin
    declare @cmd nvarchar(4000);  if object_id(@TableName) is not null 
      begin
        set @cmd = ' drop table '+@tableName;
        exec (@cmd);
      endset @cmd = 'select * into  '+@tableName + ' from 数据表';
    exec (@cmd);
    end