0
  DECLARE @outerCounter INT = 1, @rowCount INT,
@query NVARCHAR(MAX), @load numeric(18,3), @batcolno NVARCHAR(MAX), @inCounter INT = 1,@innerrowsCount INT
SELECT @rowCount = COUNT(*) FROM tempA

WHILE(@outercounter <= @rowCount)
BEGIN

    SELECT @innerrowsCount = COUNT(*) 
    FROM 
    tempB INNER JOIN tempA 
    ON  tempB.batteryid = tempA.batteryid
    AND 
    tempB.uniquerowid = @outercounter 

    WHILE(@inCounter <= @innnerrowCounter)
    BEGIN
        SELECT  @laod = laod FROM tempB INNER JOIN tempA 
        ON  tempB.batteryid = tempA.batteryid 
        AND 
        tempB.uniquerowid = @inCounter

        SET @testcolno = 'batt' + REPLACE(STR(@inCounter,3),' ','0')

         SET @query = ' UPDATE tempA
                    SET '+ @testcolno + ' = '+ @load +'
                    WHERE tempA.rowid = '+ @outercounter + '    '       
        EXEC(@query)
        SET @inCounter = @inCounter + 1         
    END
    SET @outercounter = @outercounter + 1
END

我有 2 张表 tempA 和 tempB。

温度A:

batteryid | batname |  batt01 | batt02 |  batt03 | batt04
----------+---------+---------+--------+---------+--------
01        | trixon  |  null   | null   |  null   | null
03        | jaguarv |  null   | null   |  null   | null

温度B:

batteryid  |  load
-----------+---------
01         |  14.58
01         |  58.12
01         |  16.89
03         |  25.47
03         |  87.65

tempA 中的最终输出应该是这样的:

batteryid | batname  | batt01   | batt02 | batt03  | batt04
----------|----------|----------|--------|---------|--------------
01        | trixon   |    14.58 | 58.12  | 16.89   | null
03        | jaguarv  |    25.47 | 87.65  | null    | null

上面的代码使用 while 循环通过将电池 ID 与 tempB 表连接来更新 tempA 表。

谢谢

4

1 回答 1

0

您可以PIVOT在 SQL Server 中使用 a,并且可以取消循环和更新语句。

SELECT  SourceA.[batteryid], 
        SourceA.[batname],
        SourcePivot.[1] AS 'Battery1',
        SourcePivot.[2] AS 'Battery2',
        SourcePivot.[3] AS 'Battery3',
        SourcePivot.[4] AS 'Battery4'
FROM @tempA AS SourceA
    INNER JOIN (
        SELECT *
        FROM
        (
          SELECT batteryId, 
                 loadval,
                 row_number() OVER (PARTITION BY batteryid ORDER BY loadval) rn
          FROM @tempB
        ) s
        PIVOT (MIN(loadval) FOR rn IN ([1], [2], [3], [4])) pvt
    ) AS SourcePivot ON SourcePivot.batteryid = SourceA.batteryid

这是我用来测试查询的代码

DECLARE @tempA TABLE (
    batteryid VARCHAR(2),
    batname VARCHAR(50), 
    batt01 DECIMAL(18, 2),
    batt02 DECIMAL(18, 2),
    batt03 DECIMAL(18, 2),
    batt04 DECIMAL(18, 2)
)

DECLARE @tempB TABLE (
    batteryid VARCHAR(2), 
    loadval DECIMAL(18, 2)
)

INSERT INTO @tempA (batteryid, batname) VALUES ('01', 'trixon')
INSERT INTO @tempA (batteryid, batname) VALUES ('03', 'jaguarv')
INSERT INTO @tempB VALUES ('01', '14.58')
INSERT INTO @tempB VALUES ('01', '58.12')
INSERT INTO @tempB VALUES ('01', '16.89')
INSERT INTO @tempB VALUES ('03', '25.47')
INSERT INTO @tempB VALUES ('03', '87.65')
于 2013-07-14T11:27:50.277 回答