I need to rename a column in all tables in my database. so a column 'OldColumn' has to be renamed to 'NewColumn' in all the tables
I could get list of tables that has this column using this query:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME= <Column Name>
But how can i rename it in all tables as simple as possible and don't have to write a cursor?
Of course you don't need a cursor for this. You can use sys.columns and sys.objects to generate dynamic sql. Then simply execute it. Once you are satisfied the dynamic sql is what you want feel free to uncomment the last line.
----BE WARNED!!!!---- If you change column names your views, stored procedures, functions etc will all be broken.
declare @CurrentColumnName sysname = 'asdf'
, @NewColumnName sysname = 'qwer'
, @SQL nvarchar(MAX) = ''
select @SQL = @SQL + 'EXEC sp_rename ''' + o.name + '.' + c.name + ''', ''' + @NewColumnName + ''', ''COLUMN'';'
from sys.columns c
join sys.objects o on o.object_id = c.object_id
where c.name = @CurrentColumnName
select @SQL
--exec sp_executesql @sql
Please try this, this will generate the script
select 'EXEC sp_rename ' + tableName + ''' OldColumn''' + ' ' + '''NewColumn'''
from
(select distinct OBJECT_NAME(OBJECT_ID) tableName from sys.columns where name like '%DUNS%')a
You can generate scripts like this and then execute it in one go:
declare @oldColumn nvarchar(max) = 'department_id'
declare @newColumn varchar(max) = 'dept_id'
declare @query nvarchar(max)
select 'exec sp_rename '+ char(39) + table_name + '.' + @oldColumn + char(39)
+', '+ char(39) + @newColumn + char(39) + ','+ char(39) + 'COLUMN' +Char(39) + ' GO ' from INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME = 'department_id'
Generated script for your reference:
exec sp_rename 'table5.department_id', 'dept_id','COLUMN'
exec sp_rename 'table6.department_id', 'dept_id','COLUMN'
exec sp_rename 'table1.department_id', 'dept_id','COLUMN'
The below dynamically takes all of the table names in the DB that contain a given column name, in this case 'COL_A', and inserts those names into a table, in this case 'DBO.QUEUE'. That table then acts as a queue for the cursor below. Dynamic SQL was used to iterate through the queue and call sp_RENAME to rename the column in every applicable table.
--The below two tables will be found and update
CREATE TABLE DBO.TEST1 (COL_A INT)
CREATE TABLE DBO.TEST2 (COL_A INT)
--The below will NOT update
CREATE TABLE DBO.TEST3 (COL_B INT)
--Create a table to act as a queue
CREATE TABLE DBO.QUEUE (TABLENAME VARCHAR(255))
--Insert into the queue based upon your criteria
INSERT INTO QUEUE
SELECT DISTINCT(TABLE_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME= 'COL_A'
--Run the cursor to update
DECLARE
@TABLENAME VARCHAR(255)
DECLARE Cursor_Name CURSOR LOCAL FAST_FORWARD FOR
SELECT
TABLENAME
FROM
DBO.QUEUE AS LIST;
OPEN Cursor_Name
FETCH NEXT FROM Cursor_Name INTO @TABLENAME
WHILE @@FETCH_STATUS = 0
BEGIN
--Dynamic SQL is used to execute the sp_Rename procedure with the applicable table name.
Declare @colupdate_ varchar(MAX)
set @colupdate_=
'EXEC sp_RENAME ' + '"' + @TABLENAME + '.' + 'COL_A' + '"' + ',' + '"' + 'NEW_COLUMN_NAME' + '"' + ',' + '"' +'COLUMN' + '"'
exec(@colupdate_)
FETCH NEXT FROM Cursor_Name INTO @TABLENAME
END
CLOSE Cursor_Name
DEALLOCATE Cursor_Name
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.