0

ブローテーブルを見つけてください。

テーブル

ClientID    Balance1    Balance2    Balance3    Balance4    Balance5    Balance6    Balance7    Balance8    Balance9
A           NULL        NULL        NULL        3           Null        Null        Null        Null        Null
B           10          null        Null        Null        20          Null        Null        Null        NULL
C           Null        8           Null        10          Null        Null        1           Null        NULL
D           Null        19          Null        Null        Null        Null        Null        Null        NULL
E           Null        Null        50          Null        Null        Null        Null        Null        NULL
F           NULL        NULL        NULL        30          NULL        NULL        NULL        NULL        NULL    
G           NULL        NULL        NULL        NULL        80          NULL        NULL        NULL        NULL      

クエリを使用して、上記のテーブルを以下のように変更するにはどうすればよいですか。助けてください。

ClientID    Balance1    Balance2    Balance3    Balance4    Balance5    Balance6    Balance7    Balance8    Balance9
A           NULL        NULL        NULL        3           3           3           3           3           3   
B           10          10          10          10          20          20          20          20          20
C           Null        8           8           10          10          10          1           1           1
D           Null        19          19          19          19          19          19          19          19
E           Null        Null        50          50          50          50          50          50          50          
F           NULL        NULL        NULL        30          30          30          30          30          30          
G           NULL        NULL        NULL        NULL        80          80          80          80          80          
4

3 に答える 3

2

次のように、各列を個別に順番に更新する必要があります。

UPDATE YourTable
SET Balance2 = Balance1 
WHERE Balance2 IS NULL AND Balance1 IS NOT NULL

UPDATE YourTable
SET Balance3 = Balance2
WHERE Balance3 IS NULL AND Balance2 IS NOT NULL

すべての更新が行われるか、まったく行われないようにするために、すべてのステートメントをトランザクションでラップすることができます。

于 2012-11-05T19:37:21.623 に答える
2

解決:

update myTable
set balance9 = coalesce(balance9 ,balance8 ,balance7 ,balance6 ,balance5 ,balance4 ,balance3 ,balance2 ,balance1)
,   balance8 = coalesce(balance8 ,balance7 ,balance6 ,balance5 ,balance4 ,balance3 ,balance2 ,balance1)
,   balance7 = coalesce(balance7 ,balance6 ,balance5 ,balance4 ,balance3 ,balance2 ,balance1)
,   balance6 = coalesce(balance6 ,balance5 ,balance4 ,balance3 ,balance2 ,balance1)
,   balance5 = coalesce(balance5 ,balance4 ,balance3 ,balance2 ,balance1)
,   balance4 = coalesce(balance4 ,balance3 ,balance2 ,balance1)
,   balance3 = coalesce(balance3 ,balance2 ,balance1)
,   balance2 = coalesce(balance2 ,balance1)

作業例:

declare @myTable table 
(
    clientId nchar(1) not null primary key clustered
    , balance1 int null
    , balance2 int null
    , balance3 int null
    , balance4 int null
    , balance5 int null
    , balance6 int null
    , balance7 int null
    , balance8 int null
    , balance9 int null
)

insert into @myTable (clientId, balance3, balance6)
values ('X',8,12)

update @myTable
set balance9 = coalesce(balance9 ,balance8 ,balance7 ,balance6 ,balance5 ,balance4 ,balance3 ,balance2 ,balance1)
,   balance8 = coalesce(balance8 ,balance7 ,balance6 ,balance5 ,balance4 ,balance3 ,balance2 ,balance1)
,   balance7 = coalesce(balance7 ,balance6 ,balance5 ,balance4 ,balance3 ,balance2 ,balance1)
,   balance6 = coalesce(balance6 ,balance5 ,balance4 ,balance3 ,balance2 ,balance1)
,   balance5 = coalesce(balance5 ,balance4 ,balance3 ,balance2 ,balance1)
,   balance4 = coalesce(balance4 ,balance3 ,balance2 ,balance1)
,   balance3 = coalesce(balance3 ,balance2 ,balance1)
,   balance2 = coalesce(balance2 ,balance1)

select * from @myTable
于 2012-11-05T19:42:08.023 に答える
0

UPDATEステートメントについて学習します。

次のようないくつかのステートメントが必要です。

UPDATE
    [YourTableName]
SET
    [Balance5] = '3',
    [Balance6] = '3',
    [Balance7] = '3',
    [Balance8] = '3',
    [Balance9] = '3'
WHERE
    [ClientID] = 'A'

適切なWHERE句があることを確認してください。そうしないと、意図したよりも多くのデータを更新する可能性があります。最初にロールバックする更新クエリをラップしTRANて、予想される行数を取得していることを確認することをお勧めします。

于 2012-11-05T19:35:49.787 に答える