在更新该选择的字段时插入选择

时间:2018-06-12 08:07:24

标签: sql sql-server

我需要在数据库中重新插入数据,只有1个字段不同。有没有办法让我用一个查询来做到这一点。所以从更新的选择中插入。

这是我需要的选择enter image description here

我需要再次在我的数据库中插入这个select,但是将Seasoncode从82更改为91.所以我的数据库中的数据加倍,1行不同。有没有办法让我用一个查询来做到这一点?

这是我目前的查询。但我不知道如何在此添加更新。

  INSERT INTO [OrdForm].[dbo].[SeasonDepSexGroup]
SELECT * from [SeasonDepSexGroup]
  where DepartmentCode = 'UD' and KlasseCode = 'U'

我尝试在线搜索,但是找不到这样的查询。

3 个答案:

答案 0 :(得分:4)

你走在正确的轨道上。只需选择您需要的列+' 91'为您的季节代码。

.parent {
    #SYNTAX#:selector-one {
        color:green;
    }

    #SYNTAX#:selector-two {
        &.child {
            color:red;
        }
    }

    &.second-child {
        color: blue;
    }
}

答案 1 :(得分:3)

您需要在select上清楚地填写列名称,并且可以尝试使用CASE WHEN来满足您的期望。

INSERT INTO [OrdForm].[dbo].[SeasonDepSexGroup]
      (Code,Sex10Code,LastModified,SeasoneCode ....)
SELECT t.Code,
       t.Sex10Code,
       t.DepartmentCode
       t.LastModified
       (CASE WHEN t.SeasoneCode = 82 then 91 else t.SeasoneCode end) SeasoneCode 
       ...
from [SeasonDepSexGroup] t
where DepartmentCode = 'UD' and KlasseCode = 'U'

答案 2 :(得分:3)

@Carra和@ D-Shih的答案很棒

你有anthor anwswer :-)

INSERT INTO [SeasonDepSexGroup]
  SELECT [Code], [Sexl Code], [DepartmentCode], [Last Modified], 92, [KlasseCode], [CompanyCode], [Kenmerk2] from [SeasonDepSexGroup]
  where DepartmentCode = 'UD' and KlasseCode = 'U'
  and SeasonCode = 82
  union all
  SELECT * from [SeasonDepSexGroup]
  where DepartmentCode = 'UD' and KlasseCode = 'U'
  and SeasonCode <> 82;

<强>结果:

| Code | Sexl Code | DepartmentCode |            Last Modified | SeasonCode | KlasseCode | CompanyCode | Kenmerk2 |
|------|-----------|----------------|--------------------------|------------|------------|-------------|----------|
| TSHI |         2 |             UD |  2018-02-22T13:37:50.46Z |         82 |          U |         GED |   (null) |
| ACCE |         1 |             UD |  2018-02-22T13:37:50.46Z |         82 |          U |         GED |   (null) |
| ACWI |         1 |             UD | 2018-02-22T13:37:50.463Z |         82 |          U |         GED |   (null) |
| BERM |         1 |             UD | 2018-02-22T13:37:50.463Z |         82 |          U |         GED |   (null) |
| BODW |         1 |             UD | 2018-02-22T13:37:50.463Z |         82 |          U |         GED |   (null) |
| BROE |         1 |             UD | 2018-02-22T13:37:50.467Z |         82 |          U |         GED |   (null) |
|  DAS |         1 |             UD | 2018-02-22T13:37:50.467Z |         82 |          U |         GED |   (null) |
| DENI |         1 |             UD | 2018-02-22T13:37:50.467Z |         82 |          U |         GED |   (null) |
| GILE |         1 |             UD | 2018-02-22T13:37:50.467Z |         82 |          U |         GED |   (null) |
| HEML |         1 |             UD |  2018-02-22T13:37:50.47Z |         82 |          U |         GED |   (null) |
| LEDE |         1 |             UD |  2018-02-22T13:37:50.47Z |         82 |          U |         GED |   (null) |
| LING |         1 |             UD |  2018-02-22T13:37:50.47Z |         82 |          U |         GED |   (null) |
| MANT |         1 |             UD | 2018-02-22T13:37:50.473Z |         82 |          U |         GED |   (null) |
|  MTO |         1 |             UD | 2018-02-22T13:37:50.473Z |         82 |          U |         GED |   (null) |
| TSHI |         2 |             UD |  2018-02-22T13:37:50.46Z |         92 |          U |         GED |   (null) |
| ACCE |         1 |             UD |  2018-02-22T13:37:50.46Z |         92 |          U |         GED |   (null) |
| ACWI |         1 |             UD | 2018-02-22T13:37:50.463Z |         92 |          U |         GED |   (null) |
| BERM |         1 |             UD | 2018-02-22T13:37:50.463Z |         92 |          U |         GED |   (null) |
| BODW |         1 |             UD | 2018-02-22T13:37:50.463Z |         92 |          U |         GED |   (null) |
| BROE |         1 |             UD | 2018-02-22T13:37:50.467Z |         92 |          U |         GED |   (null) |
|  DAS |         1 |             UD | 2018-02-22T13:37:50.467Z |         92 |          U |         GED |   (null) |
| DENI |         1 |             UD | 2018-02-22T13:37:50.467Z |         92 |          U |         GED |   (null) |
| GILE |         1 |             UD | 2018-02-22T13:37:50.467Z |         92 |          U |         GED |   (null) |
| HEML |         1 |             UD |  2018-02-22T13:37:50.47Z |         92 |          U |         GED |   (null) |
| LEDE |         1 |             UD |  2018-02-22T13:37:50.47Z |         92 |          U |         GED |   (null) |
| LING |         1 |             UD |  2018-02-22T13:37:50.47Z |         92 |          U |         GED |   (null) |
| MANT |         1 |             UD | 2018-02-22T13:37:50.473Z |         92 |          U |         GED |   (null) |
|  MTO |         1 |             UD | 2018-02-22T13:37:50.473Z |         92 |          U |         GED |   (null) |

db<>fiddle Test Demo Link