I am looking for a way to search or delete a certain string in a certain column of all tables of all databases of a server. The name of the column in which to search for the specified string starts as CNUM (i.e. columns CNUML, CNUMX, CNUM...)
In detail: a procedure that has two input parameters, one is the searched string and the other is a 1/0 parameter, where 1 means displaying a table containing the names of all tables that have the searched string in the specified column. The resulting table has the structure - example:
| database | table | column |
| ------------ | ------------ | ------------ |
| Database1 | Table1 | CNUMX |
and 0 means that the procedure does not display the table but deletes the specified string from the specified column of all tables where the string occurs
Thank you for any help, as I am not able to handle this problem myself
2条答案
按热度按时间vbopmzt11#
According to your comments, you want to delete all rows matching this string in all such columns in the database. So just build up a string of all that using
STRING_AGG
and the system tables.If the data types or lengths are different then you might want a cast as well.
If you want this to work on all databases then you need dynamic-over-dynamic.
Place all the above code into a
@sql
variable, escaping all'
with''
. Then run a cursor over all databases, executing for each oneEXEC database.sys.sp_executesql @sql
u3r8eeie2#
You can use dynamic SQL in this case if you want to search for or delete a specific string in a specified column across all tables in all databases on your server.