<i id='Y47KD'><tr id='Y47KD'><dt id='Y47KD'><q id='Y47KD'><span id='Y47KD'><b id='Y47KD'><form id='Y47KD'><ins id='Y47KD'></ins><ul id='Y47KD'></ul><sub id='Y47KD'></sub></form><legend id='Y47KD'></legend><bdo id='Y47KD'><pre id='Y47KD'><center id='Y47KD'></center></pre></bdo></b><th id='Y47KD'></th></span></q></dt></tr></i><div id='Y47KD'><tfoot id='Y47KD'></tfoot><dl id='Y47KD'><fieldset id='Y47KD'></fieldset></dl></div>

  1. <tfoot id='Y47KD'></tfoot>
  2. <legend id='Y47KD'><style id='Y47KD'><dir id='Y47KD'><q id='Y47KD'></q></dir></style></legend>

      <bdo id='Y47KD'></bdo><ul id='Y47KD'></ul>

      <small id='Y47KD'></small><noframes id='Y47KD'>

      是否可以使用单个 UPDATE SQL 语句执行多个更新?

      Is it possible to perform multiple updates with a single UPDATE SQL statement?(是否可以使用单个 UPDATE SQL 语句执行多个更新?)
        • <bdo id='P6nrn'></bdo><ul id='P6nrn'></ul>
              <tbody id='P6nrn'></tbody>

                <legend id='P6nrn'><style id='P6nrn'><dir id='P6nrn'><q id='P6nrn'></q></dir></style></legend>

                <small id='P6nrn'></small><noframes id='P6nrn'>

                <tfoot id='P6nrn'></tfoot>

                <i id='P6nrn'><tr id='P6nrn'><dt id='P6nrn'><q id='P6nrn'><span id='P6nrn'><b id='P6nrn'><form id='P6nrn'><ins id='P6nrn'></ins><ul id='P6nrn'></ul><sub id='P6nrn'></sub></form><legend id='P6nrn'></legend><bdo id='P6nrn'><pre id='P6nrn'><center id='P6nrn'></center></pre></bdo></b><th id='P6nrn'></th></span></q></dt></tr></i><div id='P6nrn'><tfoot id='P6nrn'></tfoot><dl id='P6nrn'><fieldset id='P6nrn'></fieldset></dl></div>
                本文介绍了是否可以使用单个 UPDATE SQL 语句执行多个更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                假设我有一个表 tbl,其中包含 idtitle 列.我需要更改标题列的所有值:

                Let's say I have a table tbl with columns id and title. I need to change all values of title column:

                1. 从a-1"到a1",
                2. 从a.1"到a1",
                3. 从b-1"到b1",
                4. 从b.1"到b1".

                现在,我正在执行两个 UPDATE 语句:

                Right now, I'm performing two UPDATE statements:

                UPDATE tbl SET title='a1' WHERE title IN ('a-1', 'a.1')
                UPDATE tbl SET title='b1' WHERE title IN ('b-1', 'b.1')
                

                这根本不是问题,如果表很小,并且单个语句在不到一秒的时间内完成,您只需要执行几条语句.

                This isn't at all a problem, if the table is small, and the single statement completes in less than a second and you only need a few statements to execute.

                您可能是客套话了 - 我有一个巨大的表要处理(一个语句在大约 90 秒内完成),而且我有大量的更新要执行.

                You probably guested it - I have a huge table to deal with (one statement completes in about 90 seconds), and I have a huge number of updates to perform.

                那么,是否可以合并更新以便它只扫描表一次?或者,在这种情况下,有更好的处理方法.

                So, is it possible to merge the updates so it would only scan the table once? Or perhaps, there's a better way to deal with in a situation like this.

                请注意,我正在使用的真实数据以及我必须执行的数据更改并不是那么简单 - 字符串更长并且它们不遵循任何模式(它是用户数据,所以不能做任何假设 - 它可以是任何东西).

                Note, that the real data I'm working with and the changes to the data I have to perform are not really that simple - the strings are longer and they don't follow any pattern (it is user data, so no assumptions can be made - it can be anything).

                推荐答案

                在更一般的情况下,可能有数百个映射到每个新值,您将创建一个单独的旧值和新值表,然后在 UPDATE 语句中使用它.在 SQL 的一种方言中:

                In a more general case, where there could be many hundreds of mappings to each of the new values, you would create a separate table of the old and new values, and then use that in the UPDATE statement. In one dialect of SQL:

                CREATE TEMP TABLE mapper (old_val CHAR(5) NOT NULL, new_val CHAR(5) NOT NULL);
                ...multiple inserts into mapper...
                INSERT INTO mapper(old_val, new_val) VALUES('a.1', 'a1');
                INSERT INTO mapper(old_val, new_val) VALUES('a-1', 'a1');
                INSERT INTO mapper(old_val, new_val) VALUES('b.1', 'b1');
                INSERT INTO mapper(old_val, new_val) VALUES('b-1', 'b1');
                ...etcetera...
                
                UPDATE tbl
                   SET title = (SELECT new_val FROM mapper WHERE old_val = tbl.title)
                   WHERE title IN (SELECT old_val FROM mapper);
                

                两个选择语句都至关重要.第一个是相关子查询(不一定快,但如果映射表有数千行,则比大多数替代查询快),它将新值从对应于旧值的映射表中拉出.第二个确保只有那些在映射表中有值的行被修改;这是至关重要的,否则,对于那些没有映射条目的行(以及那些在您开始之前正常的记录),标题将设置为 null.

                Both select statements are crucial. The first is a correlated sub-query (not necessarily fast, but faster than most of the alternatives if the mapper table has thousands of rows) that pulls the new value out of the mapping table that corresponds to the old value. The second ensures that only those rows which have a value in the mapping table are modified; this is crucial as otherwise, the title will be set to null for those rows without a mapping entry (and those were the records that were OK before you started out).

                对于一些替代方案,CASE 操作是可以的.但是,如果您有数百、数千或数百万个映射要执行,那么您很可能会超出 DBMS 中 SQL 语句长度的限制.

                For a few alternatives, the CASE operations are OK. But if you have hundreds or thousands or millions of mappings to perform, then you are likely to exceed the limits of the SQL statement length in your DBMS.

                这篇关于是否可以使用单个 UPDATE SQL 语句执行多个更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Building a comma separated list?(建立一个逗号分隔的列表?)
                Errors in SQL Server while importing CSV file despite varchar(MAX) being used for each column(尽管每列都使用了 varchar(MAX),但在导入 CSV 文件时 SQL Server 中出现错误)
                How can I import an Excel file into SQL Server?(如何将 Excel 文件导入 SQL Server?)
                Export table to file with column headers (column names) using the bcp utility and SQL Server 2008(使用 bcp 实用程序和 SQL Server 2008 将表导出到带有列标题(列名称)的文件)
                Concat field value to string in SQL Server(将字段值连接到 SQL Server 中的字符串)
                SQL Server Bulk insert of CSV file with inconsistent quotes(SQL Server 批量插入带有不一致引号的 CSV 文件)
                  <bdo id='3Fo2D'></bdo><ul id='3Fo2D'></ul>
                  <legend id='3Fo2D'><style id='3Fo2D'><dir id='3Fo2D'><q id='3Fo2D'></q></dir></style></legend>
                • <i id='3Fo2D'><tr id='3Fo2D'><dt id='3Fo2D'><q id='3Fo2D'><span id='3Fo2D'><b id='3Fo2D'><form id='3Fo2D'><ins id='3Fo2D'></ins><ul id='3Fo2D'></ul><sub id='3Fo2D'></sub></form><legend id='3Fo2D'></legend><bdo id='3Fo2D'><pre id='3Fo2D'><center id='3Fo2D'></center></pre></bdo></b><th id='3Fo2D'></th></span></q></dt></tr></i><div id='3Fo2D'><tfoot id='3Fo2D'></tfoot><dl id='3Fo2D'><fieldset id='3Fo2D'></fieldset></dl></div>
                        <tfoot id='3Fo2D'></tfoot>

                        <small id='3Fo2D'></small><noframes id='3Fo2D'>

                            <tbody id='3Fo2D'></tbody>