Hi all, here’s my task:
From one database I have to find all the tables with server name. I want to be able to do exec sp_chgname with the parameter being the new name. But right now I just need to get my cursor right!
DECLARE @TableName TABLE
(New_Name varchar(255),
Column_Name varchar(255))
--DECLARE @TableName varchar(255)
--DECLARE @column _Name varchar(255)
DECLARE TableCursor CURSOR FOR
select table_name, column_name from information_schema.columns
where table_catalog = 'mgmt_tester'
and column_name like '%sname%' or column_name like '%server_name%' or
column_name like '%servername%'
OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableName
WHILE @@Fetch _STATUS = 0
BEGIN
update @TableName set Column_Name = 'TEST'
FETCH NEXT FROM TableCursor INTO @TableName
END
CLOSE TableCursor
DEALLOCATE TableCursor
The goal is to go and find every table that has the server name. Most tables have sname for that column. But there are a few tables that have the column as servername or server_name.
So I figured iterate through all the tables with those columns. Then update them to ‘test’…for right now I just want to see if I can update the values to ‘test’.
I hope this makes sense because it’s getting foggy for me!