Solved

Excel VBA RENAME SQL COLUMN

Posted on 2014-09-15
9
414 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
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.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 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