如何在 SQL Server 中使用替换更新多个列?
如何更新表中的不同列和行? 我想做一些类似于 替换字符串在 SQL Server 中
我想这样做,但该值存在于同一类型的多个列中。 这些值是员工表的外键 varchar。 每列代表一个任务,因此同一员工可能会被分配到记录中的多个任务,并且这些任务在记录之间会有所不同。 我怎样才能有效地做到这一点? 基本上是替换整个表中不同列的所有内容。
感谢您的任何帮助或建议。
干杯, ~ck 在圣地亚哥
How do I update different columns and rows across a table? I want to do something similiar to replace a string in SQL server
I want to do this but the value exists in multiple columns of the same type. The values are foreign keys varchars to an employee table. Each column represents a task, so the same employee may be assigned to several tasks in a record and those tasks will vary between records. How can I do this effectively? Basically something of a replace all accross varying columns throughout a table.
Thanks for any help or advice.
Cheers,
~ck in San Diego
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这应该可以解决问题:
等等...
This should do the trick:
etc...
主要思想是创建一个SQL Update语句,无论表有多少个字段。 它是在 SQL Server 2012 上创建的,但我认为它也可以在 2008 上运行。
示例表:
仅获取 varchar 和 nvarchar 字段。 根据您的要求更改OLD_TEXT和NEW_TEXT。 如果您不仅需要匹配 varchar 和 nvarchar 字段,请更改 system_type_id 值。
最后一次查询的结果是:
只需复制结果并在SSMS中执行即可。 这个片段可以为您在编写更新语句时节省一些时间。
希望能帮助到你。
The main idea is to create a SQL Update sentence, no matter how many fields has the table. It was created on SQL Server 2012, however I think it works on 2008 too.
Sample table:
Get only varchar and nvarchar fields. Change OLD_TEXT and NEW_TEXT accord to your requirement. Change system_type_id values if you need match not only varchar and nvarchar fields.
The result of the last query is:
just copy the result and execute in SSMS. This snippet saves you a little time when writing the update sentence.
Hope it helps.
回答发帖者关于如何标准化此数据结构的补充问题。 这样做的方法如下:
您当前的结构(项目表中有一堆 Task1、Task2 等...列)显然不是由了解关系数据库的人设计的。
在解雇该人的过程中,您可能会解释说他的设计违反了第一范式,同时将他定向到该链接文章的“跨列重复组”部分。
In answer to the poster's supplementary question about how to normalize this data structure. Here's how you'd do it:
Your current structure, where you have a bunch of Task1, Task2, etc... columns in the Project table, was clearly not designed by somebody that understands relational databases.
During the process of firing that individual, you might explain that his design violates the First Normal Form, while directing him to the "Repeating groups across columns" section of that linked article.
我知道已经过去 12 年了,但我最近有一个类似的需求,这就是我解决它的方法(在没有运气在任何地方找到合适的完整解决方案之后)。 不同之处在于,我必须更改包含特定字符串值的所有数据库表中的值。 以下过程更为通用,也可用于一张表。
作为使用示例:
参数:
说明:
NULL
替换该值。EXEC (@SQL)
) 并使用 PRINT 取消注释这些行,只是为了了解该过程的作用以及最终语句的外观。NULL
值(即让@TestValue 为NULL
),这不会工作(很可能)。 不过,也可以轻松更改它来实现此目的,方法是用IS NULL
替换WHERE
子句(在动态查询中)中的等号,并删除其余的当 @TestValueIS NULL
时,该行。希望这最终能帮助某人。
I know it has been 12 years, but I recently had a similar requirement and this is how I solved it (after having no luck in finding a proper full solution anywhere). The difference was that I had to change the values in ALL of the database tables that contained a certain string value. The following procedure is more general, and can be used for one table too.
As a usage example:
Parameters:
LIKE
operator to find all the tables from your database whose names that match this value;Explanation:
NULL
.EXEC (@SQL)
) and uncomment the lines with PRINT, just to get an idea for what the procedure does and how the final statements look like.NULL
value (i.e. to have @TestValue asNULL
). Nevertheless, it can be easily changed to accomplish this as well, by replacing the equal sign from theWHERE
clause (in the dynamic query) withIS NULL
and removing the rest of the line, when @TestValueIS NULL
.Hope this helps someone eventually.