SQL Server中的临时表替代

时间:2018-07-31 22:16:42

标签: sql-server

伙计们,这是我的临时表代码

            SELECT
                cosd.erscommoditydataseries.erscommodity_id AS
                datatobeupdated,ERSBusinessLogic_ID
                INTO temptable1
            FROM cosd.erscommoditydataseries INNER JOIN cosd.ersconstructedvariablesoutcomes
                ON 
                SUBSTRING(
                cosd.erscommoditydataseries.erscommodity_sourceseriesid,
                CHARINDEX('(', cosd.erscommoditydataseries.erscommodity_sourceseriesid)
                + 1,
                CHARINDEX(')',
                cosd.erscommoditydataseries.erscommodity_sourceseriesid)
                -
                CHARINDEX('(',
                cosd.erscommoditydataseries.erscommodity_sourceseriesid) - 1) =
                SUBSTRING(
                cosd.ersconstructedvariablesoutcomes.ersconstructedvariable_outputdestination,
                CHARINDEX('(', cosd.ersconstructedvariablesoutcomes.ersconstructedvariable_outputdestination)
                + 1, CHARINDEX(')',
                cosd.ersconstructedvariablesoutcomes.ersconstructedvariable_outputdestination) -
                CHARINDEX('(',
                cosd.ersconstructedvariablesoutcomes.ersconstructedvariable_outputdestination)
                - 1)
                INNER JOIN cosd.ERSBusinessLogic   

                ON
                cosd.ERSBusinessLogic.ERSBusinessLogic_ID=ERSConstructedVariable_BusinessLogicID
            where erscommodity_sourceseriesid LIKE '%(N%'
            AND cosd.ersconstructedvariablesoutcomes.ersconstructedvariable_outputdestination
            LIKE '%CV(N%'
            AND cosd.ersconstructedvariablesoutcomes.ersconstructedvariable_newdataseriesid
            IS
            NULL


            update cosd.ERSConstructedVariablesOutcomes set ERSConstructedVariable_NewDataSeriesID=_dummy.datatobeupdated
            from cosd.ERSConstructedVariablesOutcomes dummy  JOIN temptable1 _dummy
            on dummy.ERSConstructedVariable_BusinessLogicID= _dummy.ERSBusinessLogic_ID

但是我尝试执行两次,它当然说temptable1已经存在,但是还有其他方法可以执行此代码。我希望将选择查询的输出更新到更新表中给出的表中

1 个答案:

答案 0 :(得分:0)

不要使用临时表(或本地表):

        ;WITH _dummy as (
        SELECT
            cosd.erscommoditydataseries.erscommodity_id AS
            datatobeupdated,ERSBusinessLogic_ID
        FROM cosd.erscommoditydataseries INNER JOIN cosd.ersconstructedvariablesoutcomes
            ON 
            SUBSTRING(
            cosd.erscommoditydataseries.erscommodity_sourceseriesid,
            CHARINDEX('(', cosd.erscommoditydataseries.erscommodity_sourceseriesid)
            + 1,
            CHARINDEX(')',
            cosd.erscommoditydataseries.erscommodity_sourceseriesid)
            -
            CHARINDEX('(',
            cosd.erscommoditydataseries.erscommodity_sourceseriesid) - 1) =
            SUBSTRING(
            cosd.ersconstructedvariablesoutcomes.ersconstructedvariable_outputdestination,
            CHARINDEX('(', cosd.ersconstructedvariablesoutcomes.ersconstructedvariable_outputdestination)
            + 1, CHARINDEX(')',
            cosd.ersconstructedvariablesoutcomes.ersconstructedvariable_outputdestination) -
            CHARINDEX('(',
            cosd.ersconstructedvariablesoutcomes.ersconstructedvariable_outputdestination)
            - 1)
            INNER JOIN cosd.ERSBusinessLogic   

            ON
            cosd.ERSBusinessLogic.ERSBusinessLogic_ID=ERSConstructedVariable_BusinessLogicID
        where erscommodity_sourceseriesid LIKE '%(N%'
        AND cosd.ersconstructedvariablesoutcomes.ersconstructedvariable_outputdestination
        LIKE '%CV(N%'
        AND cosd.ersconstructedvariablesoutcomes.ersconstructedvariable_newdataseriesid
        IS
        NULL
        )
        update cosd.ERSConstructedVariablesOutcomes set ERSConstructedVariable_NewDataSeriesID=_dummy.datatobeupdated
        from cosd.ERSConstructedVariablesOutcomes dummy  
        JOIN _dummy
        on dummy.ERSConstructedVariable_BusinessLogicID= _dummy.ERSBusinessLogic_ID