declare @sSql nvarchar(4000) create table #a (xxx char(7)) set @sSql = 'insert into #a select convert(char(7),dateadd(month,id,'''+@BeginDate +'''),121) as xxx from 
                    (select top '+cast(datediff(month,@BeginDate,@FinishDate)+1 as char(3)) + '(select sum(1) from sysobjects where name <= a.name) - 1 as id from sysobjects a) bb' exec (@sSql) select IDENTITY(int, 1,1) as id, 'select * from his..logasset_' + substring(xxx, 1, 4) + '_' + substring(xxx, 6, 2) + ' where orgid in (' + @orgid + ') union all ' as sSqlString into #b from #a 
update #b set sSqlString = substring(sSqlString, 1, len(sSqlString) - 10) where id = (select max(id) from #b) 
set @sSql = ''select @sSql = @sSql + sSqlString from #b 
set @sSql = 'insert into #c select * from (' + @sSql + ') a'SELECT * INTO #c FROM his..logasset_2008_09 WHERE 1<> 1exec(@sSql)DROP TABLE #a
DROP TABLE #b  exec(select a.*, isnull(b.成交金额, 0) as 成交金额, isnull(b.净手续费, 0) as 净手续费, isnull(b.手续费, 0) as 手续费, isnull(b.印花税, 0) as 印花税, isnull(b.过户费, 0) as 过户费, isnull(b.清算费, 0) as 清算费, isnull(b.交易规费, 0) as 交易规费, isnull(b.经手费, 0) as 经手费, isnull(b.证管费, 0) as 证管费, isnull(b.其他费, 0) as 其他费, isnull(b.前台费用, 0) as 前台费用 into #a from aaa a left join bbb b on (a.资金帐号 = b.资金帐号) and (a.币种 = b.币种) where (a.机构代码 in (' + @orgid + ')) order by a.经理帐号, a.资金帐号')为何已经执行了DROP TABLE #a 还会报There is already an object named '#a' in the database. 

解决方案 »

  1.   

    declare @sSql nvarchar(4000) create table #a (xxx char(7)) 
    IF OBJECT_ID('#a') IS NOT NULL 
       DROP TABLE #A
    set @sSql = 'insert into #a select convert(char(7),dateadd(month,id,'''+@BeginDate +'''),121) as xxx from 
                        (select top '+cast(datediff(month,@BeginDate,@FinishDate)+1 as char(3)) + '(select sum(1) from sysobjects where name <= a.name) - 1 as id from sysobjects a) bb' 
    ......
      

  2.   

    create table #a (xxx nvarchar(20))
    insert into #a (xxx)
           VALUES ('aaaaaaaaaa')drop table #acreate table #a (xxx nvarchar(20))
    insert into #a (xxx)
           VALUES ('aaaaaaaaaa')There is already an object named '#a' in the database.