Solved

Excel VBA RENAME SQL COLUMN

Posted on 2014-09-15
9
402 Views
Last Modified: 2014-09-21
Hi

I am trying to rename a SQL Column in Excel VBA using the following statement but it says incorrect syntax

 sSQL = "ALTER TABLE [BANK] RENAME COLUMN [ACOUNT] to [ACCOUNT]"
0
Comment
Question by:murbro
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40324716
The syntax seems to be correct. Try
sSQL = "ALTER TABLE BANK RENAME COLUMN ACOUNT to ACCOUNT;"
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40324732
believe you need to use sp_rename

EXEC sp_rename
    @objname = 'BANK.ACOUNT ',
    @newname = 'ACCOUNT',
    @objtype = 'COLUMN'
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40325445
Playing with fire, we are...
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 27

Expert Comment

by:MacroShadow
ID: 40325465
Let's hope this is the beginning of the developing....
0
 

Author Comment

by:murbro
ID: 40325955
Hi MacroShadow I get the error Incorrect syntax near 'RENAME'.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40326880
I am NOT aware of RENAME being available in TSQL. Refer to the documentation page for ALTER TABLE
Try using the browser's search (ctrl f) for "rename"

The syntax is not correct for SQL Server
(it might be for MySQL for example)

Consider the following which you can try yourself here: http://sqlfiddle.com/#!3/414b0/1
CREATE TABLE BANK
     ([ID] int identity primary key
     
     , [ACOUNT] int                  --<< incorrect spelling
    );

INSERT INTO BANK 	([ACOUNT])
VALUES 	(10),	(20),	(30);

                                      --<< fix the spelling!

EXEC sp_rename @objname = 'BANK.ACOUNT ', @newname = 'ACCOUNT', @objtype = 'COLUMN';

Open in new window

-- note the column heading
SELECT * FROM BANK;


-- now try this syntax:
ALTER TABLE BANK RENAME COLUMN ACCOUNT to ACOUNT;

Open in new window

| ID | ACCOUNT |
|----|---------|
|  1 |      10 |
|  2 |      20 |
|  3 |      30 |
		
Incorrect syntax near 'RENAME'.: -- now try this syntax:
ALTER TABLE BANK RENAME COLUMN ACCOUNT to ACOUNT

Open in new window

0
 
LVL 27

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 40327165
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40327175
That reference is for "generic SQL" NOT T-SQL (which  is the MS SQL Server dialect of SQL)

Microsoft do not own the term "SQL" although I do know many folk will just use that term when referring to "Microsoft SQL Server" so it's easy to get this wrong when out of context.

The definitive reference for T-SQL is "Books On-Line" (BOL)
0
 

Author Closing Comment

by:murbro
ID: 40335303
Thanks very much
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
remove dups 10 37
Excel Formula 16 45
Unhide very hidden sheets with password 22 45
Increment column based of a FK 8 23
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question