Solved

Excel VBA RENAME SQL COLUMN

Posted on 2014-09-15
9
385 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 26

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
 
LVL 26

Expert Comment

by:MacroShadow
ID: 40325465
Let's hope this is the beginning of the developing....
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 26

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now