Forum Discussion
Jagdeep_1012
Mar 05, 2024Copper Contributor
Updating columns names of multiple tables dynamically
Hi all I have 10 tables and the columns names have same prefix names but different suffix after underscore - my requirement is I want to update the column names with the prefix only means I want ...
frugecn
Mar 06, 2024Copper Contributor
If you have a https://learn.microsoft.com/en-us/sql/ssdt/project-oriented-offline-database-development?view=sql-server-ver16, you could just update them there and do a sync. Another way is to create a cursor for the tables and columns sys views filtered to what you tables and columns you wanted and do https://learn.microsoft.com/en-us/sql/relational-databases/tables/rename-columns-database-engine?view=sql-server-ver16 on the columns with the new generated from LEFT(column_name, CHARINDEX('_',column_name)-1) assuming that the first '_' is where you want to split from. I'm sure there are much more elegant ways as well.