简体   繁体   English

如何为数据库中的所有表更新列值的特定字符

[英]How to update a specific characters of column value for all tables in a database

I am trying to update a specific characters of column value for all tables in a database in Sql server 2008. I have _WORKGROUPNAME column in my all tables and I want to change some characters in _WORKGROUPNAME in all tables of database. 我正在尝试为Sql Server 2008中的数据库中的所有表更新列值的特定字符。我的所有表中都有_WORKGROUPNAME列,并且我想更改数据库的所有表中的_WORKGROUPNAME中的某些字符。

sample data for _WORKGROUPNAME column _WORKGROUPNAME列的样本数据

COWAI1
COWAI2
PMWAI1
PMWAI2

need results 需要结果

COXXX1
COXXX2
PMXXX1
PMXXX2

I want to change WAI to XXX. 我想将WAI更改为XXX。 and I am tying below SQL. 而且我在SQL下绑定。

SELECT 'UPDATE ' + T.TABLE_SCHEMA + '.' + T.TABLE_NAME + ' 
SET _WORKGROUPNAME = N''XXX'  
FROM INFORMATION_SCHEMA.TABLES T 
INNER JOIN INFORMATION_SCHEMA.COLUMNS C ON 
T.TABLE_NAME = C.TABLE_NAME AND c.COLUMN_NAME ='_WORKGROUPNAME' 
WHERE T.TABLE_TYPE = 'BASE TABLE' ORDER BY T.TABLE_SCHEMA, T.TABLE_NAME

but the problem is above statement is updating all column value to XXX , but I want to change only WAI to XXX. 但是问题是上面的语句将所有列值都更新为XXX ,但是我只想将WAI更改为XXX。 How to achieve this. 如何实现这一目标。

Edit : 编辑:

In all tables _WORKGROUPNAME name contains data and WAI will comes after 2 characters. 在所有表中,_WORKGROUPNAME名称包含数据,并且WAI将在2个字符之后。 and after WAI it is a number that can be any number. 在WAI之后,该数字可以是任意数字。

For Example 例如

ASWAI1500
WEWAI900000
ASWAI96
DDWAI11111

只需将AND _WORKGROUPNAME = N'WAI'添加到您的WHERE子句中。

Try this 尝试这个

SELECT 'UPDATE ' + T.TABLE_SCHEMA + '.' + T.TABLE_NAME + ' 
SET _WORKGROUPNAME = replace(_WORKGROUPNAME,''WAI'',''XXX'')'  
FROM INFORMATION_SCHEMA.TABLES T 
INNER JOIN INFORMATION_SCHEMA.COLUMNS C ON 
T.TABLE_NAME = C.TABLE_NAME AND c.COLUMN_NAME ='_WORKGROUPNAME' 
WHERE T.TABLE_TYPE = 'BASE TABLE' ORDER BY T.TABLE_SCHEMA, T.TABLE_NAME

暂无
暂无

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

相关问题 我在多个表中有相同的列,并希望将所有表中的列更新为特定值。我怎样才能做到这一点? - I have the same column in multiple tables, and want to update that column in all tables to a specific value. How can I do this? 如何遍历具有特定名称的所有表,然后更新每个表中的特定列值 - How can i traverse all the tables with specific name and then update a particular column value from each table 如何在数据库 Teradata 中查找具有特定列名的所有表? - How to find all the tables in database Teradata with specific column names in them? 在数据库的所有列和所有表中搜索一个值 - Search for a value in all column and all tables of a database 搜索数据库中包含特定列的所有表 - Searching all tables in database containing specific column 如何计算在具有特定列的所有表中出现的值? - How to count occurrences of value in all tables that have a specific column? 更新整个数据库中所有表中的值 - Update a value in all tables in the entire database Oracle:列出特定日期范围内具有 column_value = '%value%' 的数据库中的所有表? - Oracle:List all tables in the database with a column_value = '%value%' for a specific date range? 如何从oracle架构中包含该列名的所有表表中获取特定列的最大值? - How to get the max value for a specific column from all tables tables which contains that column name in an oracle schema? 更新整个数据库中特定列的所有出现 - Update all occurence of a specific column in entire database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM