tsql更新列

vc9ivgsu  于 2021-08-09  发布在  Java
关注(0)|答案(3)|浏览(233)

我在下面的表格里有数据,

SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO

    SET ANSI_PADDING ON
    GO

    CREATE TABLE [dbo].[tbl_Test](
        [Id1] [int] NOT NULL,
        [Id2] [int] NOT NULL,
        [id3] [int] NOT NULL,
        [D_OldValue] [varchar](100) NOT NULL,
        [R_Value] [varchar](100) NOT NULL,

     CONSTRAINT [PK_tbl_Test] PRIMARY KEY CLUSTERED 
    (
        [Id1] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]

    GO

    SET ANSI_PADDING OFF
    GO

    USE [temporary_databases];
SET NOCOUNT ON;
SET XACT_ABORT ON;
GO

BEGIN TRANSACTION;
INSERT INTO [dbo].[tbl_Test]([Id1], [Id2], [id3], [D_OldValue], [R_Value])
SELECT 437, 163, 163, N'BW', N'Ford' UNION ALL
SELECT 493, 163, 163, N'BW', N'Stock' UNION ALL
SELECT 1224, 163, 163, N'BW', N'Wood' UNION ALL
SELECT 1225, 163, 163, N'BW', N'FRD' UNION ALL
SELECT 1232, 163, 163, N'BW', N'COW' UNION ALL
SELECT 1240, 163, 163, N'BW', N'FRD1' UNION ALL
SELECT 2160, 163, 163, N'BW', N'Log'
COMMIT;
RAISERROR (N'[dbo].[tbl_Test]: Insert Batch: 1.....Done!', 10, 1) WITH NOWAIT;
GO

我的excel里有如下要求
+------+-----+-----+

wi3ka0sx

wi3ka0sx2#

--+

| Id1  | Id2 | id3 | D_OldValue | R_Value | D_NewValue |

+------+-----+-----+------------+---------+------------+

|  437 | 163 | 163 | BW         | Ford    | BF         |

|  493 | 163 | 163 | BW         | Stock   | WS         |

| 1224 | 163 | 163 | BW         | Wood    | WS         |

| 1225 | 163 | 163 | BW         | FRD     | BF         |

| 1232 | 163 | 163 | BW         | COW     | WS         |

| 1240 | 163 | 163 | BW         | FRD1    | BF         |

| 2160 | 163 | 163 | BW         | Log     | WS         |

+------+-----+-----+------------+---------+------------+

现在我需要用d\u newvalue(excel表)更新(tbl\u test)中的d\u oldvalue列,我已经尝试过像update tbl\u test set d\u oldvalue='bf'这样的简单更新查询,其中d\u oldvalue='bw',但它会将所有bw更新为bf最终输出应该是

+------+-----+-----+------------+---------+
| Id1  | Id2 | id3 | D_OldValue | R_Value |
+------+-----+-----+------------+---------+
|  437 | 163 | 163 | BF         | Ford    |
|  493 | 163 | 163 | WS         | Stock   |
| 1224 | 163 | 163 | WS         | Wood    |
| 1225 | 163 | 163 | BF         | FRD     |
| 1232 | 163 | 163 | WS         | COW     |
| 1240 | 163 | 163 | BF         | FRD1    |
| 2160 | 163 | 163 | WS         | Log     |
+------+-----+-----+------------+---------+

Thanks
flvlnr44

flvlnr443#

通过使用下面的查询,所有行都将被更新。如果要更新特定行,请添加where条件

Update tbl_Test set D_OldValue = D_NewValue

相关问题