[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 377
  • Last Modified:

Need to Raname a field using SQL statement

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
familine
Asked:
familine
  • 5
  • 3
  • 2
  • +7
1 Solution
 
PeterGrCommented:
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
 
nigelrivettCommented:
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
 
nigelrivettCommented:
or you can use sp_rename :-).
0
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
familineAuthor Commented:
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
 
appariCommented:
try the following

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


Rao
0
 
JonathanReadCommented:
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
 
JonathanReadCommented:
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
 
nigelrivettCommented:
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
 
JonathanReadCommented:
To NigelRivett:

Re-think in which way?
0
 
nigelrivettCommented:
the

I meant Answer - Not Comment
0
 
aavictorCommented:
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
 
nigelrivettCommented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
A friendly reminder (for familine)
0
 
mironCommented:
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
 
avbsqlCommented:
learning...
0
 
MoondancerCommented:
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
 
MoondancerCommented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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

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.

  • 5
  • 3
  • 2
  • +7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now