本帖最后由 liyongf 于 2011-12-04 07:41:21 编辑

解决方案 »

  1.   

    改成如下面所示就行了:
    create table t1(code varchar(10))
    insert into t1 select 'aaa'
    insert into t1 select 'bbb'
    create table t2(code varchar(10))
    insert into t2 select 'bbb'
    go
    go
    create procedure [dbo].[p_CopyData]
    @SrcTableName nvarchar(50),
    @DestTableName nvarchar(50)
    as
    begin
    declare @num int,@i int
    exec('insert into #t_code select '+@DestTableName+'.code from '+@SrcTableName+' join '+@DestTableName+'
    on '+@SrcTableName+'.code='+@DestTableName+'.code')
    end
    go
    create table #t_code(code varchar(10))
    exec p_copyData 't1','t2'
    select * from #t_code
    /*
    code
    ----------
    bbb(1 行受影响)
    */
    go
    drop table t1,t2,#t_code
    drop procedure p_copydata在存储过程内部生成的临时表在存储过程外部无法调用,但在调用存储过程前生成的临时表,可以在存储过程中向其中插入数据.
      

  2.   

    #t_code--改为 ##t_code 全局临时表也可接收
    建议
    用临时表或表变量接收