?
Solved

Need to Raname a field using SQL statement

Posted on 2001-06-25
18
Medium Priority
?
370 Views
Last Modified: 2007-12-19
We are having problems with a FIELD name using VB and therefore we need to rename it... We have tried many SQL statements we found out in the internet but were unsuccessful...
These are some options we have tried:

ALTER TABLE Presupuestos_detalle ALTER COLUMN [pvp_descto-pre-det] RENAME pvp_descto_det_pre

        Incorrect systax near pvp_descto_det_pre

ALTER TABLE Presupuestos_detalle RENAME [pvp_descto-pre-det] TO pvp_descto_det_pre

        Incorrect syntax near pvp_descto-det-pre

ALTER TABLE Presupuestos_detalle RENAME COLUMN [pvp_descto-pre-det] TO pvp_descto_det_pre

        Incorrect syntax near keyword 'COLUMN'

We cannot use the SQL Server to rename it... It must be done in our VB programme using a SQL statement, but we run out of options... Any other option? Thank U very much

0
Comment
Question by:familine
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +7
18 Comments
 

Expert Comment

by:PeterGr
ID: 6223768
Correct syntax (refer to BOL) is:
ALTER TABLE [Table1] RENAME COLUMN [Name1] TO [Name2]
but it doesn't work!
error : Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'COLUMN'.
It seems strange indeed.
But anyway you can use sp_rename stored procedure:
EXEC sp_rename 'Table1.Name1', 'Table1.Name2'
0
 
LVL 18

Expert Comment

by:nigelrivett
ID: 6223795
There is no alter table option to rename a column (I think).

If you are brave you can try updating the system table.
You should be able to do all this from VB if you have permission.



exec sp_configure 'allow updates',1
go
reconfigure with override
go
update syscolumns
set name = 'newname'
where id = object_id('tableName')
and name = 'oldname'
exec sp_configure 'allow updates',0
go
reconfigure with override
go




0
 
LVL 18

Expert Comment

by:nigelrivett
ID: 6223798
or you can use sp_rename :-).
0
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 

Author Comment

by:familine
ID: 6223809
We have tried to use that stored procedure in the following example...

EXEC sp_rename Pedidos_cli_detalle.[pvp_descto-det-ped], Pedidos_cli_detalle.[pvp_descto_det_ped]

     Incorrect syntax near '.'

We need to rename a field whose name has got hyphens...

Thank you anyway
0
 
LVL 39

Accepted Solution

by:
appari earned 300 total points
ID: 6223836
try the following

sp_rename 'Pedidos_cli_detalle.[pvp_descto-det-ped]' , 'pvp_descto_det_ped', 'COLUMN'


Rao
0
 

Expert Comment

by:JonathanRead
ID: 6224172
Not all that slick but if none of the other solutions worked try this :

assume the original table is called 'Familine'

1) exec sp_rename 'Familine', 'Familine_old', 'OBJECT'
2) create a new table using a similar definition that would create the original table - replace the FieldName you do not want with the new name

CREATE TABLE Familine
(column1 ...........,
 column2NEWNAME ...........,
 column3 ...........
)

3) Populate this new table :

INSERT INTO Familine
SELECT * FROM Familine_old

4) Check it has been populated ok

5) drop table Familine_old

6) add permissions, indexes etc


hope this helps
0
 

Expert Comment

by:JonathanRead
ID: 6224189
I meant Answer - Not Comment
-----------------------------

Not all that slick but if none of the other solutions worked try this :

assume the original table is called 'Familine'

1) exec sp_rename 'Familine', 'Familine_old', 'OBJECT'
2) create a new table using a similar definition that would create the original table - replace the
FieldName you do not want with the new name

CREATE TABLE Familine
(column1 ...........,
column2NEWNAME ...........,
column3 ...........
)

3) Populate this new table :

INSERT INTO Familine
SELECT * FROM Familine_old

4) Check it has been populated ok

5) drop table Familine_old

6) add permissions, indexes etc


hope this helps
0
 
LVL 18

Expert Comment

by:nigelrivett
ID: 6224452
Proposed Answer

I meant Answer - Not Comment

Not all that slick but if none of the other solutions worked try this :

Maybe you should rethink that.
0
 

Expert Comment

by:JonathanRead
ID: 6227606
To NigelRivett:

Re-think in which way?
0
 
LVL 18

Expert Comment

by:nigelrivett
ID: 6230943
the

I meant Answer - Not Comment
0
 
LVL 4

Expert Comment

by:aavictor
ID: 6248725
Answer if and only if you are DBA
First.
=====
select * from sysobjects A inner join syscolumns B
on A.id = B.id
where a.name = 'TableName';

sp_configure 'allow updates', 1
reconfigure with override

These commands set the system tables in database as an opencity

Second,
=======
update syscolumns
set name = 'Newcolumn'
 from sysobjects A inner join syscolumns B
on A.id = B.id
where a.name = 'TableName'
and B.name = 'OldColumn';

Finally, very very important
============================
sp_configure 'allow updates', 0
reconfigure with override


Or you can use SQL server enterprise manager to manually rename the column
0
 
LVL 18

Expert Comment

by:nigelrivett
ID: 6249803
Or as I said earlier

exec sp_configure 'allow updates',1
go
reconfigure with override
go
update syscolumns
set name = 'newname'
where id = object_id('tableName')
and name = 'oldname'
exec sp_configure 'allow updates',0
go
reconfigure with override
go




0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6378867
A friendly reminder (for familine)
0
 
LVL 9

Expert Comment

by:miron
ID: 6468134
Well, under the hood sp_rename is doing this:

-- Use DBCC to check for column in use by check-constraint, computed-column, etc
-- THIS IS NOT A DOCUMENTED DBCC: DO NOT USE DIRECTLY!
DBCC RENAMECOLUMN ( @OwnAndObjName, @UnqualOldName, @newname )

Why not make an exxception when all else are failing. And with all dew respect, RECONFIGURE WITH OVERRIDE is somewhat emerigency exit, lets stick with what usual ways offer. If the column name will again upset the DBCC wrapped into sp_rename, which a failry possible, I propose to use the following, more general SQL syntax.
---- pceudo code
ALTER TABLE __table ADD <new_column_name old_column_type>

update table __table set <old_coumn> = <new_column>

ALTER TABLE __table DROP COLUMN <old_column_name>

---sample code:

create table t_test
(
     number int
)

insert into t_test select id from sysobjects
GO

alter table t_test ADD number_test int
GO

update t_test set number_test = number
alter table t_test DROP COLUMN number
select * from t_test

drop table t_test

----------- end of sample code

angeIIII, please no reminders.
0
 
LVL 3

Expert Comment

by:avbsql
ID: 6662408
learning...
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6934966
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process for further information, if needed.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE KnowledgePro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20188991.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20141014.html
http://www.experts-exchange.com/questions/Q.20269057.html

**** PLEASE DO NOT AWARD THE POINTS TO ME. *****
 
------------>  EXPERTS:  Please leave your closing recommendations if this item remains inactive another seven (7) days.  If you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed here -> http://www.experts-exchange.com/commspt/Q.20277028.html
 

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 7116365
Asker chose not to close this.

EXPERTS PLEASE GUIDE ME WITH CLOSING RECOMMENDATIONS, SINCE IT HAS BEEN LEFT IN OUR HANDS TO COMPLETE.

Thanks,
Moondancer - EE Moderator
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7121188
In my opinion, appari has given the correct answer...

sp_rename 'Pedidos_cli_detalle.[pvp_descto-det-ped]' , 'pvp_descto_det_ped', 'COLUMN'

As the quotes have to be used...
CHeers



0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

765 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