没有为 C1 的第 1 列指定列

No column was specified for Column 1 of C1

我需要帮助,我已经坚持了 3 天了,我一直在谷歌上搜索,但我似乎无法解决这个问题,有人可以帮我解决这个问题并解释我做错了什么吗?

我不断收到 No Column was specified for C1 错误,当我尝试执行它时,我不断收到

消息 4112,级别 15,状态 1,过程 XXX,第 27 行 [批处理开始第 7 行] 函数 'ROW_NUMBER' 必须有一个带有 ORDER BY 的 OVER 子句。

SELECT TOP 1 @DataIndex = DataIndex,
                 @ChannelDescription = Description,
                 @UserConfigurationData = UserConfigurationData
    FROM DevicesCW WHERE Id = @DTSourceRecId
    
        
    SELECT @DataIndex DataIndex,Column_Id ChannelID, value ChannelName INTO #CName FROM dbo.[fn_split_string_to_column] (@ChannelDescription,@delimiter)
    SELECT @DataIndex DataIndex,Column_Id ChannelID, value ChannelNumber INTO #CNumber FROM dbo.[fn_split_string_to_column] (@UserConfigurationData,@delimiter);
    
    --Test Code--
    --Channel 1
    With C1 AS (SELECT  @ReoDeviceId,CN.ChannelNumber,C.ChannelID,D.Totaliser1Units TotaliserUnits, C.ChannelName, 'CW', @DTSourceRecId,'B', C.ChannelName,
            RN = ROW_NUMBER() OVER(PARTITION BY @DTSourceRecId)
            FROM DevicesCW D
                JOIN #CName C
                    ON D.DataIndex = C.DataIndex AND C.ChannelID = 1
                JOIN #CNumber CN
                    ON D.DataIndex = C.DataIndex AND C.ChannelID = 1
                LEFT JOIN ReoDeviceMeter RDM
                    ON D.DataIndex = RDM.DeviceIndex AND RDM.ChannelNumber = 1
            WHERE D.Id = @DTSourceRecId AND RDM.ReoMeterId IS NULL)
    INSERT INTO ReoMeter(ReoDeviceId, MeterNumber, ChannelNumber, ChanUnitOfMeasure, Description, DtSource, DtSourceRecId, MeterType, SourceDescription)
    SELECT @ReoDeviceId,ChannelNumber,ChannelID,TotaliserUnits,ChannelName,'CW',@DTSourceRecId,'B',ChannelName

    FROM C1
    WHERE RN = 1
    Group BY @ReoDeviceId,ChannelNumber,ChannelID,TotaliserUnits,ChannelName,'CW',@DTSourceRecId,'B',ChannelName


    -- Test Code---

我正在尝试采用上述方法来解决打击代码为每个频道复制数据库中数据的问题。如果我有 2 个通道,它会为 CH1 和 CH2 插入数据 2 次,而不是只为每个通道插入数据

--Channel1
    INSERT INTO ReoMeter(ReoDeviceId, MeterNumber, ChannelNumber, ChanUnitOfMeasure, Description, DtSource, DtSourceRecId, MeterType, SourceDescription)
    SELECT  @ReoDeviceId,CN.ChannelNumber,C.ChannelID,D.Totaliser1Units, C.ChannelName, 'CW', @DTSourceRecId,'B', C.ChannelName
    FROM DevicesCW D
        JOIN #CName C
            ON D.DataIndex = C.DataIndex AND C.ChannelID = 1
        JOIN #CNumber CN
            ON D.DataIndex = C.DataIndex AND C.ChannelID = 1
        LEFT JOIN ReoDeviceMeter RDM
            ON D.DataIndex = RDM.DeviceIndex AND RDM.ChannelNumber = 1
    WHERE D.Id = @DTSourceRecId AND RDM.ReoMeterId IS NULL

    --Channel2
    INSERT INTO ReoMeter(ReoDeviceId, MeterNumber, ChannelNumber, ChanUnitOfMeasure, Description, DtSource, DtSourceRecId, MeterType, SourceDescription)
    SELECT  @ReoDeviceId,CN.ChannelNumber,C.ChannelID,D.Totaliser2Units, C.ChannelName, 'CW', @DTSourceRecId, 'B', C.ChannelName
    FROM DevicesCW D
        JOIN #CName C
            ON D.DataIndex = C.DataIndex AND C.ChannelID = 2
        JOIN #CNumber CN
            ON D.DataIndex = C.DataIndex AND C.ChannelID = 2
        LEFT JOIN ReoDeviceMeter RDM
            ON D.DataIndex = RDM.DeviceIndex AND RDM.ChannelNumber = 2
    WHERE D.Id = @DTSourceRecId AND RDM.ReoMeterId IS NULL

您需要为 C1 的所有列提供 Aleas 才能解决此问题。

SELECT TOP 1 @DataIndex = dataindex,
             @ChannelDescription = description,
             @UserConfigurationData = userconfigurationdata
FROM   devicescw
WHERE  id = @DTSourceRecId

SELECT @DataIndex DataIndex,
       column_id  ChannelID,
       value      ChannelName
INTO   #cname
FROM   dbo.[Fn_split_string_to_column] (@ChannelDescription, @delimiter)

SELECT @DataIndex DataIndex,
       column_id  ChannelID,
       value      ChannelNumber
INTO   #cnumber
FROM   dbo.[Fn_split_string_to_column] (@UserConfigurationData, @delimiter);

--Test Code--
--Channel 1
WITH c1
     AS (SELECT @ReoDeviceId      AS ReoDeviceId,
                CN.channelnumber,
                C.channelid,
                D.totaliser1units TotaliserUnits,
                C.channelname,
                'CW'              AS CW,
                @DTSourceRecId    AS DTSourceRecId,
                'B'               AS B,
                C.channelname,
                RN = Row_number()
                       OVER(
                         partition BY @DTSourceRecId)
         FROM   devicescw D
                JOIN #cname C
                  ON D.dataindex = C.dataindex
                     AND C.channelid = 1
                JOIN #cnumber CN
                  ON D.dataindex = C.dataindex
                     AND C.channelid = 1
                LEFT JOIN reodevicemeter RDM
                       ON D.dataindex = RDM.deviceindex
                          AND RDM.channelnumber = 1
         WHERE  D.id = @DTSourceRecId
                AND RDM.reometerid IS NULL)
INSERT INTO reometer
            (reodeviceid,
             meternumber,
             channelnumber,
             chanunitofmeasure,
             description,
             dtsource,
             dtsourcerecid,
             metertype,
             sourcedescription)
SELECT reodeviceid,
       channelnumber,
       channelid,
       totaliserunits,
       channelname,
       cw,
       dtsourcerecid,
       b,
       channelname
FROM   c1
WHERE  rn = 1
GROUP  BY reodeviceid,
          channelnumber,
          channelid,
          totaliserunits,
          channelname,
          cw,
          dtsourcerecid,
          b,
          channelname