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

  • Home
  • SEARCH
  • 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 6947389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:41:13+00:00 2026-05-27T13:41:13+00:00

Im trying to execute a SQL query that will rename the columns of a

  • 0

Im trying to execute a SQL query that will rename the columns of a table with the text from the first recordset in a table.

My table looks like this:

 COL1  |  COL2  |  COL3  |  COL4  |  COL5  | COL6

 REASON |ITEMDATE|ITEMTIME|SITENAME|  EVENT | RPM  
 tstamp |12-11-07|  24:12 | Spain1 |Shutdwn | 1000  
 tstamp |13-11-07|  02:22 | Spain1 |Startup | 1050

And I would like to rename the columns like this:

 REASON |ITEMDATE|ITEMTIME|SITENAME|  EVENT | RPM

 tstamp |12-11-07|  24:12 | Spain1 |Shutdwn | 1000  
 tstamp |13-11-07|  02:22 | Spain1 |Startup | 1050 
  • 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-05-27T13:41:14+00:00Added an answer on May 27, 2026 at 1:41 pm

    This procedure will do what you need. You can run it as follows:

        exec p_rename_columns N'<mytable>'
    

    Note that the procedure assumes that the “first” row is the physical first row on disk. Since this can change depending on which field the clustered index on the table uses it is not 100% guaranteed.

    The code for the procedure:

    create proc p_rename_columns (@table sysname)
    AS
    
    declare @name sysname, 
            @col sysname,
            @sql nvarchar(max)
    
    declare cur cursor 
    local read_only 
    for select name 
          from sys.columns 
         where object_id = object_id(@table)
    
    open cur
    fetch next from cur into @name
    
    while @@fetch_status = 0 
      begin
    
        select @sql = N'select top (1) @col = ' + quotename(@name) + N' from ' + quotename(@table)
        exec sp_executesql @sql, N'@col sysname output', @col output
    
        select @sql = N'exec sp_rename ''' + quotename(@table) + N'.' + quotename(@name) + N''', ''' + @col + N''''
        exec (@sql)
    
        fetch next from cur into @name
      end 
    close cur
    deallocate cur
    
    select @sql = N'DELETE TOP (1) from ' + quotename(@table)
    exec (@sql)
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first question. I'm trying to execute a SQL query in django
I am trying to execute a query on a table in my SQL Server
I am trying to execute this SQL query prior to restoring a .BAK file
I'm getting a timeout error when trying to execute a LINQ (-to-SQL) query System.Data.SqlClient.SqlException:
I am trying to execute this SQL command: SELECT page.page_namespace, pagelinks.pl_namespace, COUNT(*) FROM page,
I'm trying to programmatically add an Execute SQL task from within a script task
I'm trying to insert text which happens to contain a sql query into a
I'm new to SQLite3 with iOS I'm trying to create an SQL Query that
I am trying to execute two sql statements in a row. EDIT:Only the first
I'm trying to execute a query that currently works in phpMyAdmin but it does

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.