简体   繁体   English

SQL更新1个表中的多行

[英]SQL updating multiple rows in 1 table

I have a question. 我有个问题。 I am using MS SQL Server Management Studio by the way. 我正在使用MS SQL Server Management Studio。

I have a Dictionary table with a lot of translations. 我有一个包含很多翻译的词典表。 I need to copy a complete description from a languageID to another languageID. 我需要将完整的描述从一个languageID复制到另一个languageID。

Example below. 下面的例子。

LanguageID | Description
    2      | Some text
    2      | More text
    2      | Some more text
   10      | *needs to be replaced
   10      | *needs to be replaced
   10      | *needs to be replaced

The result must be like this: 结果必须是这样的:

LanguageID | Description
    2      | Some text
    2      | More text
    2      | Some more text
   10      | Some text
   10      | More text
   10      | Some more text

The description of LanguageID 2 and 10 must be exactly the same. LanguageID 2和10的描述必须完全相同。

My current Query runs into an error: 我当前的查询遇到错误:

update tblDictionary
set Description = (Select Description from tblDictionary where 
tblDictionary.LanguageID = 2)
where LanguageID = 10

Msg 512, Level 16, State 1, Line 1 Subquery returned more than 1 value. 消息512,级别16,状态1,行1子查询返回的值超过1。 This is not permitted when the subquery follows =, !=, <, <= , 当子查询遵循=,!=,<,<=时,这是不允许的

, >= or when the subquery is used as an expression. ,> =或将子查询用作表达式时。 The statement has been terminated. 该语句已终止。

If all translations for LanguageID 10 must be exact the same as for languageID 2 then its easier to delete all translations for ID 10 and then insert them back again. 如果LanguageID 10的所有翻译必须与languageID 2的翻译完全相同,则可以轻松删除ID 10的所有翻译,然后重新插入。
Something like this 像这样

delete from tblDictionary where LanguageID = 10;

insert into tblDictionary (LanguageID, Description)
select 10, d.Description
from   tblDictionary d
where  d.LanguageID = 2

This method also has the advantage that if there are less records with LanguageID = 10 then there are for LanguageID = 2 this will be corrected in the same process. 该方法还具有以下优势:如果较少的记录具有LanguageID = 10 ,则对于LanguageID = 2将在同一过程中对其进行更正。

If you have more columns in tblDictionary than you will need to modify the insert statement off course 如果tblDictionary中的列多于需要修改的插入语句

DECLARE @temp varchar(50)
DECLARE language_cursor CURSOR FOR  
SELECT Description FROM tblDictionary  
WHERE LanguageID = 2  
ORDER BY Description;  

OPEN language_cursor;  

-- Perform the first fetch.  
FETCH NEXT FROM language_cursor
into @temp;  

-- Check @@FETCH_STATUS to see if there are any more rows to fetch.  
WHILE @@FETCH_STATUS = 0  
BEGIN  
   update TOP (1) tblDictionary
   set Description = @temp
   where Description = ''
   and LanguageID = 10;  
   FETCH NEXT FROM language_cursor
   into @temp;
END  

CLOSE language_cursor;  
DEALLOCATE language_cursor;  

Set all languageID 10 to empty first, then loop all description from languageID 2 to update into languageID 10 one by one until all empty description from languageID10 is filled. 首先将所有languageID 10设置为空,然后循环遍历languageID 2的所有描述以逐一更新为languageID 10,直到填充languageID10中的所有空白描述。

Now if you really want an update, something like this should work, even though I think the structure of the table needs to be improved. 现在,如果您真的想要更新,即使我认为表的结构需要改进,类似的事情也应该起作用。

WITH l2 AS 
(SELECT *,  
 ROW_NUMBER() OVER(PARTITION BY LanguageId ORDER BY Description ASC) AS No FROM tblDictionary WHERE LanguageId=2),
l10 AS 
(SELECT *,  
 ROW_NUMBER() OVER(PARTITION BY LanguageId ORDER BY Description ASC) AS No FROM tblDictionary WHERE LanguageId=10)

UPDATE l10 SET Description = l2.Description 
FROM l10
INNER JOIN l2 ON l10.No = l2.No

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM