SQL Server临时表已经存在?

时间:2014-05-23 14:44:03

标签: sql-server tsql stored-procedures

所以这很奇怪,我在这个SQL中声明了一个临时表,但我只是基于if else逻辑声明它。我在运行以下查询之前删除临时表,但仍然会得到相同的行为。

但是,当我尝试在There is already an object named '#ManifestTrackingBranches' in the database. 设置为2的情况下运行查询时,SQL Server会对ReportType抱怨。

我在这里错过了什么吗?

T-SQL

declare @ReportType int 
declare @CustomerNumber int 
declare @StartDate datetime
declare @EndDate datetime 

set @ReportType = 2
set @CustomerNumber = 81 
set @StartDate = '2014-04-27'
set @EndDate = '2014-05-04'

if @CustomerNumber = 81
begin
    if @ReportType = 1 -- roll up by location
    begin 
        select  InboundData.Tracking,
                InboundData.NegotiatedRate 
        into    #ManifestTrackingBranches
        from    InboundData
        where   Injected >= @StartDate and Injected <= @EndDate

        -- Match tracking numbers against Ebill Data 
        select      #ManifestTrackingBranches.Tracking,
                    SUM(isnull(cast(#ManifestTrackingBranches.NegotiatedRate as decimal(18,2)),0)) as ManifestAmount
        from        EBillData 
        group by    #ManifestTrackingBranches.Branch
    end

    else if  @ReportType = 2 -- Line Item Reports
    begin 
        select  InboundData.Tracking,
                InboundData.NegotiatedRate 
        into    #ManifestTrackingBranches
        from    InboundData
        where   Injected >= @StartDate and Injected <= @EndDate

        -- Match tracking numbers against Ebill Data 
        select      #ManifestTrackingBranches.Tracking,
                    SUM(isnull(cast(#ManifestTrackingBranches.NegotiatedRate as decimal(18,2)),0)) as ManifestAmount
        from        EBillData 
    end
end

如果将ReportType设置为2,则会在第二个错误发生,并且我尝试选择相同的临时表。

2 个答案:

答案 0 :(得分:6)

在任何变量声明之前添加此行代码。

IF OBJECT_ID('tempdb..#ManifestTrackingBranches') IS NOT NULL 
DROP TABLE #ManifestTrackingBranches
GO

只有在使用GO关键字将此声明置于单独的批处理中时才会生效。当你实际编写程序并再次执行n再次测试代码时,这已经足够了。

在您的过程中,您无法添加关键字GO,并且在从应用程序调用此过程时也不需要删除表。对此过程的每次调用都将拥有自己的连接,并将创建一个仅限于该连接范围的临时表。

答案 1 :(得分:1)

SQL将保留每个连接的临时表,除非您删除它们。因此,一旦完成使用它,放弃临时表是个好主意。

添加DROP TABLE语句

declare @ReportType int 
declare @CustomerNumber int 
declare @StartDate datetime
declare @EndDate datetime 

set @ReportType = 2
set @CustomerNumber = 81 
set @StartDate = '2014-04-27'
set @EndDate = '2014-05-04'

if @CustomerNumber = 81
begin
  if @ReportType = 1 -- roll up by location
  begin 
    select  InboundData.Tracking,
            InboundData.NegotiatedRate 
    into    #ManifestTrackingBranches
    from    InboundData
    where   Injected >= @StartDate and Injected <= @EndDate

    -- Match tracking numbers against Ebill Data 
    select      #ManifestTrackingBranches.Tracking,
                SUM(isnull(cast(#ManifestTrackingBranches.NegotiatedRate as decimal(18,2)),0)) as ManifestAmount
    from        EBillData 
    group by    #ManifestTrackingBranches.Branch;

    --clean up after yourself
    drop table #ManifestTrackingBranches
  end

  else if  @ReportType = 2 -- Line Item Reports
  begin 
    select  InboundData.Tracking,
            InboundData.NegotiatedRate 
    into    #ManifestTrackingBranches
    from    InboundData
    where   Injected >= @StartDate and Injected <= @EndDate

    -- Match tracking numbers against Ebill Data 
    select      #ManifestTrackingBranches.Tracking,
                SUM(isnull(cast(#ManifestTrackingBranches.NegotiatedRate as decimal(18,2)),0)) as ManifestAmount
    from        EBillData 

    --clean up after yourself
    drop table #ManifestTrackingBranches
  end
end