使用三个with子句插入表SQL Server

时间:2018-06-26 21:14:22

标签: sql-server

所以我在语句中使用三个with子句插入表中,代码如下:

WITH dataforIDs AS
(
    SELECT 
        value, ERSBusinessLogic_InputDataSeries
    FROM 
        [AnimalProductsCoSD].[CoSD].[ERSBusinessLogic]
    CROSS APPLY 
        STRING_SPLIT(ERSBusinessLogic_InputDataSeries, ',')
    WHERE
        ERSBusinessLogic_InputGeographyDimensionID = 7493
        AND ERSBusinessLogic_InputTimeDimensionValue = 'all months'
        AND ERSBusinessLogic_Type = 'HS10 aggregation'
), datafromdatavalues (Total, ERSBusinessLogic_InputDataSeries, Date) AS 
(
    SELECT
        SUM(ERSDataValues_AttributeValue) AS Total,
        dataforIDs.ERSBusinessLogic_InputDataSeries,
        FORMAT(b.ERSTimeDimension_Date, 'yyyy-MM-dd') AS Date
    FROM 
        cosd.ERSDataValues a, cosd.ERSTimeDimension_LU b, dataforIDs
    WHERE
        a.ERSDataValues_ERSCommodity_ID = dataforIDs.value   
        AND a.ERSDataValues_ERSTimeDimension_ID = b.ERSTimeDimension_ID
        AND b.ERSTimeDimension_Year IN (SELECT b.ERSTimeDimension_Year FROM cosd.ERSTimeDimension_LU) 
    GROUP BY 
        b.ERSTimeDimension_Year, dataforIDs.ERSBusinessLogic_InputDataSeries, b.ERSTimeDimension_Date
), dataFromBussiness (ERSBusinessLogic_ID, ERSBusinessLogic_OutputDestination, ERSBusinessLogic_LongDesc,
                      ERSBusinessLogic_OutputUnitID, ERSBusinessLogic_PrivacyID, ERSBusinessLogic_InputSources,
                      ERSBusinessLogic_InputSourceID, ERSBusinessLogic_OutputTimeDimensionValue, 
                      ERSBusinessLogic_OutputTimeDimensionTypeID, ERSBusinessLogic_OutputName,
                      ERSBusinessLogic_OutputGeographyDimensionID, ERSBusinessLogic_InputTimeDimensionValue) AS 
(
    SELECT 
        ERSBusinessLogic_ID, ERSBusinessLogic_OutputDestination, ERSBusinessLogic_LongDesc,
        ERSBusinessLogic_OutputUnitID, ERSBusinessLogic_PrivacyID, 
        ERSBusinessLogic_InputSources, ERSBusinessLogic_InputSourceID,
        ERSBusinessLogic_OutputTimeDimensionValue, ERSBusinessLogic_OutputTimeDimensionTypeID,
        ERSBusinessLogic_OutputName, ERSBusinessLogic_OutputGeographyDimensionID,
        ERSBusinessLogic_InputTimeDimensionValue AS '1'
    FROM 
        cosd.ERSBusinessLogic   
    WHERE
        ERSBusinessLogic_InputDataSeries IN (SELECT ERSBusinessLogic_InputDataSeries
                                             FROM [AnimalProductsCoSD].[CoSD].[ERSBusinessLogic]   
                                             WHERE ERSBusinessLogic_InputGeographyDimensionID = 7493
                                               AND ERSBusinessLogic_InputTimeDimensionValue = 'all months'
                                               AND ERSBusinessLogic_Type = 'HS10 aggregation')
)
INSERT INTO [CoSD].[ERSConstructedVariablesOutcomes]
            ([ERSConstructedVariable_BusinessLogicID], [ERSConstructedVariable_OutputValue],
             [ERSConstructedVariable_OutputDestination], [ERSConstructedVariable_LongDescription],
             [ERSConstructedVariable_ExecutionDate], [ERSConstructedVariable_OutputUnitID],
             [ERSConstructedVariable_DataRowPrivacyID], [ERSConstructedVariable_InputSources],
             [ERSConstructedVariable_InputSourceID], [ERSConstructedVariable_OutputTimeDimensionValue],
             [ERSConstructedVariable_OutputTimeDimensionTypeID], [ERSConstructedVariable_OutputName],
             [ERSConstructedVariable_TimeDimensionDate], [ERSConstructedVariable_NewDataSeriesID],
             [ERSConstructedVariable_DataRowLifecyclePhaseID], [ERSConstructedVariable_OutputGeographyDimensionID],
             [ERSConstructedVariable_TimeDimensionID])
    SELECT  
        ERSBusinessLogic_ID, Total,
        ERSBusinessLogic_OutputDestination, ERSBusinessLogic_LongDesc, 
        GETDATE(), ERSBusinessLogic_OutputUnitID,
        ERSBusinessLogic_InputTimeDimensionValue, ERSBusinessLogic_InputSources,    
        ERSBusinessLogic_InputSourceID, ERSBusinessLogic_OutputTimeDimensionValue,
        ERSBusinessLogic_OutputTimeDimensionTypeID, ERSBusinessLogic_OutputName,Date,
        ERSBusinessLogic_InputTimeDimensionValue, ERSBusinessLogic_OutputGeographyDimensionID,
        ERSBusinessLogic_InputDataSeries, ERSBusinessLogic_OutputGeographyDimensionID 
    FROM
        dataFromBussiness, datafromdatavalues

但是我似乎出错了

  

将varchar值“所有月份”转换为数据类型int时转换失败

如果我正在运行两个选择查询,则运行平稳,但是对于合并查询,似乎有问题。我检查了with子句的语法是否正确。有什么想法吗?

0 个答案:

没有答案