Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8088673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:09:35+00:00 2026-06-05T19:09:35+00:00

When I try to run the following SQL snippet inside a cursor loop, set

  • 0

When I try to run the following SQL snippet inside a cursor loop,

set @cmd = N'exec sp_rename ' + @test + N',' +
           RIGHT(@test,LEN(@test)-3) + '_Pct' + N',''COLUMN'''

I get the following message,

Msg 15248, Level 11, State 1, Procedure sp_rename, Line 213
Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong.

What is wrong and how do I fix it ? I tried wrapping the column name in brackets [], and double quotes "" like some of the search results suggested.

Edit 1 –

Here is the entire script. How do I pass the table name to the rename sp ? I’m not sure how to do that since the column names are in one of many tables.

BEGIN TRANSACTION

declare @cnt int
declare @test nvarchar(128)
declare @cmd nvarchar(500) 
declare Tests cursor for
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE 'pct%' AND TABLE_NAME LIKE 'TestData%'

open Tests
fetch next from Tests into @test
while @@fetch_status = 0
BEGIN
  set @cmd = N'exec sp_rename ' + @test + N',' + RIGHT(@test,LEN(@test)-3) + '_Pct' + N', column' 

  print @cmd

  EXEC sp_executeSQL @cmd

  fetch next from Tests into @test
END

close Tests 
deallocate Tests


ROLLBACK TRANSACTION
--COMMIT TRANSACTION

Edit 2 –
The script is designed to rename columns whose names match a pattern, in this case with a “pct” prefix. The columns occur in a variety of tables within the database. All table names are prefixed with “TestData”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:09:36+00:00Added an answer on June 5, 2026 at 7:09 pm

    Here is slightly modified version. Changes are noted as code commentary.

    BEGIN TRANSACTION
    
    declare @cnt int
    declare @test nvarchar(128)
    -- variable to hold table name
    declare @tableName nvarchar(255)
    declare @cmd nvarchar(500) 
    -- local means the cursor name is private to this code
    -- fast_forward enables some speed optimizations
    declare Tests cursor local fast_forward for
     SELECT COLUMN_NAME, TABLE_NAME
       FROM INFORMATION_SCHEMA.COLUMNS 
      WHERE COLUMN_NAME LIKE 'pct%' 
        AND TABLE_NAME LIKE 'TestData%'
    
    open Tests
    -- Instead of fetching twice, I rather set up no-exit loop
    while 1 = 1
    BEGIN
      -- And then fetch
      fetch next from Tests into @test, @tableName
      -- And then, if no row is fetched, exit the loop
      if @@fetch_status <> 0
      begin
         break
      end
      -- Quotename is needed if you ever use special characters
      -- in table/column names. Spaces, reserved words etc.
      -- Other changes add apostrophes at right places.
      set @cmd = N'exec sp_rename ''' 
               + quotename(@tableName) 
               + '.' 
               + quotename(@test) 
               + N''',''' 
               + RIGHT(@test,LEN(@test)-3) 
               + '_Pct''' 
               + N', ''column''' 
    
      print @cmd
    
      EXEC sp_executeSQL @cmd
    END
    
    close Tests 
    deallocate Tests
    
    ROLLBACK TRANSACTION
    --COMMIT TRANSACTION
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On Mysql (Amazon RDS), when I try to run the following SQL query UPDATE
if i try to run following code in sql server 2005 i get error
I try to run the following code: public void Init(Url rootUrl) { var web
When i try to run the following expect script, it just finishes running instead
When I try to run the following code in the MySQL console it gives
When I try to run the following, I get an error back from ActiveRecord
I try to run jsf application in myeclipse using jboss web server and following
I have the following code: public void run() { try { logger.info(Looking for new
I get the following error when I try to run sample example of Google
When I try to run a routine I get the following error: Error Code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.