繁体   English   中英

SQL Server中的临时表替代

[英]Temporary table subsitute in 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已经存在,但是还有其他方法可以执行此代码。 我希望将选择查询的输出更新到更新表中给出的表中

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

        ;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

暂无
暂无

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

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