Help With Cursors

cipher7836 86 Reputation points
2021-11-08T20:52:22.593+00:00

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!

Developer technologies Transact-SQL
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Camila de Macena Ribeiro 1 Reputation point Microsoft Employee
    2021-11-08T21:08:51.08+00:00

    Please try use the parenthesis to separate the OR conditions:

    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%' )

    0 comments No comments

  2. Tom Phillips 17,771 Reputation points
    2021-11-08T21:36:36.037+00:00

    The table name in an UPDATE statement cannot be a variable.

    You must use "dynamic sql" to do this kind of operation.

    DELCARE @sqlcmd VARCHAR(max);
    SET @sqlcmd = 'UPDATE ' + @tablename + ' SET Column_Name = ''TEST'''
    
    EXEC (@sqlcmd)
    
    0 comments No comments

  3. Viorel 122.6K Reputation points
    2021-11-08T22:04:42.127+00:00

    Try replacing the UPDATE line with this statements:

    declare @c varchar(max) = concat( quotename(@TableName), '.', quotename(@ColumnName))
    exec sp_rename @c, 'test', 'COLUMN'
    

    where @ColumnName is the current name of the column to be renamed (extracted from INFORMATION_SCHEMA.COLUMNS).

    0 comments No comments

  4. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2021-11-08T22:44:31.307+00:00

    I think we have discussed this before. Just have one UPDATE statement per table. That will be a lot easier to maintain. Not the least when the column name is not consistent between tables.

    0 comments No comments

  5. EchoLiu-MSFT 14,621 Reputation points
    2021-11-09T07:23:17.16+00:00

    Hi @cipher7836 ,

    Please refer to the suggestions of other experts to modify your cursor.If there are still problems, please provide error information.

    In addition, the cursor is only used as a backup method. It is used when WHILE loops, subqueries, temporary tables, table variables, custom functions or other methods cannot implement certain queries.

    Regards,
    Echo


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.