简体   繁体   中英

Unable to drop table from inside a stored procedure

I have a stored procedure that creates a temp table each time from a select into statement.

The problem is that the procedure gives the following error when it is run:

Cannot drop the table '#Temp', because it does not exist or you do not have permission.

This only happens within the stored procedure and when I am testing it in management studio it works just fine.

I have tried giving the account that runs the procedure the db_owner role but it made no difference.

*I have removed the alter procedure section and this is the code I am using in

   USE [DBName]
GO
/****** Object:  StoredProcedure [dbo].[PL_Vehicles_Search]    Script Date: 25/06/2014 14:52:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Stored Procedure
-- Description:
-- Version:
-- ModifiedBy:
-- Date Modified:
-- =============================================

ALTER PROCEDURE [dbo].[PL_Vehicles_Search]
    @PageSize INT = 10 ,
    @CurrentPage INT = 1 ,
    @SortExpression NVARCHAR(MAX) = 'RegNumber' ,
    @SearchText NVARCHAR(250) = '' ,
    @SearchSite NVARCHAR(1) = '1'
AS
    BEGIN


        SET @PageSize = 10
    SET @CurrentPage = 1
    SET @SortExpression = 'RegNumber'
    SET @SearchText = ''
    SET @SearchSite = '1'


            DECLARE @PageID INT
        SET @PageID = 245
        DECLARE @Cols VARCHAR(max)

        DECLARE @sqlstring NVARCHAR(MAX)
        DECLARE @UpperBand INT
        DECLARE @LowerBand INT
        DECLARE @RealCol NVARCHAR(MAX)

        SET @LowerBand = ( @CurrentPage - 1 ) * @PageSize
        SET @UpperBand = ( @CurrentPage * @PageSize ) + 1



        IF EXISTS ( SELECT  [name]
                    FROM    tempdb.sys.tables
                    WHERE   [name] LIKE '#Temp%' )
            BEGIN
                DROP TABLE #Temp;
            END



            --DATA


        SELECT  V.VehID,
                V.RegNumber,
                V.FleetNumber,
                VT.VehTypeCode,
                VT.[Description] AS 'VehTypeDesc',
                V.Driver,
                V.VehicleConfirmed
        ,ROW_NUMBER() OVER (Order By @SortExpression ) As RowNumber
        INTO    #Temp
        FROM    dbo.Vehicles V
        INNER JOIN dbo.VehicleTypes VT
        ON V.VehicleTypeID = VT.ID
        WHERE (@SearchText = '' OR V.RegNumber LIKE '%' + @SearchText + '%')

              --  SELECT  *
        --FROM    #Temp

        EXEC dbo.PL_GetColumns @PageID, @Cols OUTPUT


        --END


        --DATA


    SET @sqlstring = 'SELECT ' + @Cols + ' FROM #Temp WHERE ROWNUMBER > '
                + CONVERT(VARCHAR, @LowerBand) + ' AND ROWNUMBER < '
                + CONVERT(VARCHAR, @UpperBand) + ' ORDER BY #Temp.'
                + @SortExpression + ''

            EXEC sp_executesql @sqlstring


        SELECT  [Text]
        FROM    dbo.CustomerLocalizationColumns CLC
                INNER JOIN Portal.dbo.[Columns] C ON CLC.ColumnID = C.ID
        WHERE   C.PageID = @PageID



    END

If you are creating a local temporary table(Single #) within the a stored procedure, then what is happening is after the stored procedure finishes executing, it drops the local temporary tables. So, if you try to drop the table after executing the stored procedure, that table will no longer exist, as it has already been dropped.

Consider using a Global temporary tables (start with ##) if you need to access the table in multiple locations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM