Solved

Excel VBA RENAME SQL COLUMN

Posted on 2014-09-15
9
393 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
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40325465
Let's hope this is the beginning of the developing....
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

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.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

896 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

11 Experts available now in Live!

Get 1:1 Help Now