简体   繁体   English

无法从存储过程中删除表

[英]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. 我有一个存储过程,每次从select into语句创建一个临时表。

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. 无法删除表“ #Temp”,因为该表不存在或您没有权限。

This only happens within the stored procedure and when I am testing it in management studio it works just fine. 这仅在存储过程中发生,当我在Management Studio中对其进行测试时,它工作正常。

I have tried giving the account that runs the procedure the db_owner role but it made no difference. 我尝试给运行该过程的帐户指定db_owner角色,但没有任何区别。

*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. 如果需要在多个位置访问表,请考虑使用全局临时表(以##开头)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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