如何在Temp表上应用过滤器,并在从同一临时表中删除旧数据后将结果存储到同一Temp表中

时间:2016-09-01 13:25:37

标签: sql-server-2008 sql-server-2012

ALTER PROCEDURE InLiceneseAdmRoute 
    -- Add the parameters for the stored procedure here
@Region nvarchar(500)=NULL,
@Country nvarchar(500)=NULL
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    IF @Region IS NOT NULL AND @Region!=''
    BEGIN

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

        CREATE TABLE #TempAdmRoute(
                Country nvarchar(250),
                API1 nvarchar(250),
                API2 nvarchar(250),
                API3 nvarchar(250),
                AdmRoute nvarchar(250))

        INSERT INTO #TempAdmRoute(Country,API1,API2,API3,AdmRoute)
        SELECT Country,API1,API2,API3,AdmRoute
        FROM dbo.Product
        WHERE 
        AdmRoute is not null and AdmRoute !='' and 
        ScrapePipeline ='S' and 
        Region in (SELECT items FROM [dbo].[Split] (@Region, ',')) and
        IsActive = 1


        IF @Country IS NOT NULL AND @Country !='' 
        BEGIN
            INSERT INTO #TempAdmRoute
            SELECT Country,API1,API2,API3,AdmRoute 
            FROM #TempAdmRoute
            WHERE Country =@Country

            IF @API1IS NOT NULL AND @API1!='' 
            BEGIN
            INSERT INTO #TempAdmRoute
            SELECT Country,API1,API2,API3,AdmRoute 
            FROM #TempAdmRoute
            WHERE API1=@API1
            END
        END

            select * from #TempAdmRoute

    END 
END
GO

我已经使用上面的代码从10,00,000个reocrds中检索数据,所以我使用了临时表。是否可以从#TempAdmRoute中检索记录,然后将记录存入#TempAdmRoute清除所有以前的记录?

2 个答案:

答案 0 :(得分:1)

我不清楚@API1来自哪里。您没有在存储过程中定义它。

您可以删除表格中您想要的所有记录,而不是将表格中的选定记录复制回自身。这可能是最简单的代码。

事实上,如果您在复制记录之前知道@API1,则可以向INSERT添加一个额外的过滤器#TempAdmRoute

或者,因为(从外观)您最终返回#TempAdmRoute的内容,您可以过滤返回#TempAdmRoute内容的查询。

但是,如果这些都不起作用,您可以将#TempAdmRoute的内容复制到表变量或第二个临时表中,删除#TempAdmRoute的内容,然后插入{{1从您的表变量或第二个临时表。

#TempAdmRoute

无法删除表格内容,然后查询刚刚删除的内容。这些是单独的步骤,一旦删除记录,它们将被删除。

答案 1 :(得分:0)

我在 #TempInLiceneseAPI 表中应用了过滤器,并从表中删除了与搜索条件不匹配的记录

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

        CREATE TABLE #TempInLiceneseAPI(
                Country nvarchar(500),
                API1 nvarchar(250),
                API2 nvarchar(250),
                API3 nvarchar(250))

        INSERT INTO #TempInLiceneseAPI(Country,API1,API2,API3)
        SELECT Country,API1,API2,API3
        FROM dbo.Product
        WHERE 
        API1 is not null and 
        API1 !='' and 
        ScrapePipeline ='S' and
        Region in (SELECT items FROM [dbo].[Split] (@Region, ',')) and
        IsActive =1

        IF @Country IS NOT NULL AND @Country !='' 
        BEGIN
            DELETE FROM #TempInLiceneseAPI
            WHERE Country NOT IN(SELECT items FROM [dbo].[Split] (@Country, ','))
        END

        IF @API1 IS NOT NULL AND @API1 !='' AND @API1 !='0'
        BEGIN
            DELETE FROM #TempInLiceneseAPI WHERE API1 !=@API1 OR API1 IS NULL

            IF @API2 IS NOT NULL AND @API2 !='' AND @API2 !='0'
            BEGIN
                DELETE FROM #TempInLiceneseAPI WHERE API2 !=@API2 OR API2 IS NULL

                IF @API3 IS NOT NULL AND @API3 !='' AND @API3 !='0'
                BEGIN
                    DELETE FROM #TempInLiceneseAPI WHERE API3 !=@API3 OR API3 IS NULL
                END
            END
        END