关于#sqlsever#的问题,同一字段下关键词如何批量对调?

数据库:sqlsever2008
续接前个提问,问题是:在找出需要对调的sht码后,也就是字段内对特定关键词两两对调

初始数据如下:

 sht     pcs     index route
1111a    20231a    1    41
1111a    20232a    2    41
1111a    20233a    3    41
2222a    20234a    1    41
2222a    20235a    2    41
2222a    20236a    3    41
3333a    20237a    1    41
3333a    20238a    2    41
3333a    20239a    3    41
4444a    20240a    1    41
4444a    20241a    2    41
4444a    20242a    3    41

在处理后得到需要对调的数据:

sht       pcs   index  route      sht       pcs    index route
1111a    20243a    1    41        2222a    20249a    1    41
1111a    20244a    2    41        2222a    20250a    2    41
1111a    20245a    3    41        2222a    20251a    3    41
3333a    20246a    1    41        4444a    20252a    1    41
3333a    20247a    2    41        4444a    20253a    2    41
3333a    20248a    3    41        4444a    20254a    3    41

我尝试过:
1.replace关键字,但这样替换过后另一半就消失了;如果新建两个临时对象A/B再替换那一次只能对调一张,实际有好多
2.case when then,这东西如果是对调一个sht内的内容比如index可以
(when index 1 than 2
when index 2 than 1 end)这是可以同时对调的,但是现在是对调sht指定不了一个固定内容,不加括号也是另一半没了

目标:将这个表格内sht下的左侧与右侧两两对调,也就是1111a与2222a、3333a与4444a对调以此类推,且需要限定route为41 进行批量更新!

@轻松仁——极简生活

望采纳!!!点击回答右侧采纳即可!!
题主你这个需求可以使用update语句进行对调

update table_name
set sht = (case when sht = '1111a' then '2222a' else '1111a' end),
pcs = (case when pcs = '20243a' then '20249a' else '20243a' end),
index = (case when index = '1' then '1' else '1' end),
route = (case when route = '41' then '41' else '41' end)
where sht in ('1111a', '2222a')
and pcs in ('20243a', '20249a')

这个语句是在table_name表中,将sht字段值为'1111a'的记录更新为'2222a',将pcs字段值为'20243a'的记录更新为'20249a',index和route字段没有更新,只是做了一个比对而已。

在SQL Server中实现批量对调字段内关键词的方法有多种,下面是一种可行的解决方案:

  1. 创建临时表,将原表数据插入临时表中,这样就可以保证原表数据不会被破坏。

  2. 使用ROW_NUMBER()函数为临时表中的数据添加行号。

  3. 使用CASE语句根据行号对sht进行对调,并对route为41的数据进行更新。

WITH temp_table AS (
    SELECT ROW_NUMBER() OVER (PARTITION BY sht ORDER BY sht) AS row_num,
           sht, pcs, index, route
    FROM original_table
    WHERE route = 41
)
UPDATE temp_table
SET sht = CASE 
            WHEN row_num % 2 = 1 THEN (SELECT sht FROM temp_table WHERE row_num = tt.row_num + 1)
            ELSE (SELECT sht FROM temp_table WHERE row_num = tt.row_num - 1)
           END
FROM temp_table tt


请注意,此示例代码仅供参考,可能需要根据您的具体需求进行修改。
4. 将更新后的数据从临时表中导入到原表中,完成批量对调操作。

UPDATE original_table
SET sht = temp_table.sht, pcs = temp_table.pcs, [index] = temp_table.[index], route = temp_table.route
FROM original_table JOIN temp_table ON original_table.sht = temp_table.sht


5.如果需要,可以删除临时表。

DROP TABLE temp_table


在这个过程中,使用了CASE语句和ROW_NUMBER()函数来对sht字段进行对调。使用了WITH语句来创建临时表并给行号,并使用UPDATE语句来更新原表。

请务必在对生产数据库进行任何更改之前进行充分测试和备份,以避免数据损坏或丢失。

1、还是感觉题主描述不太清楚:
①、题主是想将表 1 的原始数据,经过表 2 的匹配交换更新后,输出表 1 一样格式的表格么?还是输出表 2 格式的表格?
②、题主只给出了表 1 和表 2 ,但表 1 如何转换为表 2 的规则则没有说,仅仅是sht字段的除最后一位字母部分的值相邻吗?且是奇数到偶数的相邻(如1与2、3与4或者是2与3、4与5等等……)吗?还是有其他的规则?或者说题主已经自己通过转换生成了表 2 的内容呢?
③、是一个sht必然有另一个sht可交换呢?还是有可能存在sht没有与之对应的另一个sht呢?
④、题主给出的表 1 和 表 2 的数据中,sht可认为来源是一样的,但pcs又完全不同,更不清楚表 1 如何转换为表 2了
⑤、题主说的“1111a与2222a、3333a与4444a对调以此类推”,而数据中1111a就存在3个index,根据表 2 ,是111a的index:1 与 2222a的index:1对调么?

在SQL Server中,可以使用UPDATE语句来批量对调同一字段下的关键词。

例如,假设有一个表名为"mytable",有一个字段名为"keywords",要将该字段中的关键词"keyword1"和"keyword2"进行对调。

可以使用以下代码实现:


UPDATE mytable
SET keywords = REPLACE(keywords, 'keyword1', 'temp')
WHERE keywords LIKE '%keyword1%'

UPDATE mytable
SET keywords = REPLACE(keywords, 'keyword2', 'keyword1')
WHERE keywords LIKE '%keyword2%'

UPDATE mytable
SET keywords = REPLACE(keywords, 'temp', 'keyword2')
WHERE keywords LIKE '%temp%'

上面这段代码,会在mytable表中,把keywords列中所有包含'keyword1'的地方替换成'temp',然后再把包含'keyword2'的地方替换成'keyword1',最后把包含'temp'的地方替换成'keyword2'。

这样就实现了对调keyword1和keyword2的目的。

需要注意的是,如果你的数据量很大,这样的更新可能会很耗时,建议在更新之前先备份数据。

另外如果你的数据量很大,建议在更新之前先创建索引,这样可以提高更新的效率。

您可以使用临时表来完成对调操作。首先,将需要对调的数据插入临时表中,然后使用UPDATE语句将临时表中的数据更新到正式表中。

具体操作步骤如下:

创建临时表:

```sql
CREATE TABLE #temp (sht varchar(50), pcs varchar(50), [index] int, route int)
将需要对调的数据插入临时表中:

```sql
INSERT INTO #temp (sht, pcs, [index], route)
SELECT sht, pcs, [index], route
FROM mytable
WHERE route = 41

使用UPDATE语句将临时表中的数据更新到正式表中:


```sql
UPDATE mytable
SET sht = t.sht, pcs = t.pcs
FROM #temp t
WHERE mytable.sht = t.sht AND mytable.route = 41


注意:上述语句假设您的表名为mytable,请根据实际情况修改。

如果您想批量对调两个不同的关键词(例如sht为1111a2222a),您可以按照以下步骤操作:

创建临时表:

```sql
CREATE TABLE #temp (sht varchar(50), pcs varchar(50), [index] int, route int)



将需要对调的数据插入临时表中:


INSERT INTO #temp (sht, pcs, [index], route)
SELECT sht, pcs, [index], route
FROM mytable
WHERE route = 41 AND (sht = '1111a' OR sht = '2222a')




使用UPDATE语句将临时表中的数据更新到正式表中:


```sql
UPDATE mytable
SET sht = 
    CASE 
    WHEN t.sht = '1111a' THEN '2222a'
    WHEN t.sht = '2222a' THEN '1111a'
    END
FROM #temp t
WHERE mytable.sht = t.sht AND mytable.route = 41


```
此外,在完成更新操作后,请不要忘记删除临时表。

SQL参考如下:

WITH temp_table AS (
    select sht,CONVERT(varchar(50),(row_number() over(partition by [route] order by pcs)+(select top 1 REPLACE(pcs,'a','') from [table] order by REPLACE(pcs,'a','') desc)))+'a' as pcs,[index],[route] from [table] where sht='1111a' or sht='3333a'
)
select (case when sht = '1111a' then REPLACE(sht,'1111a','2222a') else REPLACE(sht,'3333a','4444a') end) as sht,CONVERT(varchar(50),(row_number() over(partition by [route] order by pcs)+(select top 1 REPLACE(pcs,'a','') from temp_table where sht='1111a' or sht='3333a' order by REPLACE(pcs,'a','') desc)))+'a' as pcs,[index],[route] from temp_table

结果:

img

原表数据:

img