Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

add new column in the middle of a table - sybase 11.9.2

Posted on 2002-06-26
11
Medium Priority
?
2,478 Views
Last Modified: 2012-05-04
Is it possible to add a new column where ever we want in a existing table. If so How ?

thanks,

hmk
0
Comment
Question by:hmk
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 5

Expert Comment

by:amitpagarwal
ID: 7112757
i can think of the following approach :

suppose the table has the following struction

Create table OLD ( a int , b int , c int )

Now you want to insert a new column after b - say it is d int datatype.

I would say select a , b , d = 0, c into #x from OLD

drop table OLD
select * into OLD from #x
drop table #x

Thanks.
0
 

Author Comment

by:hmk
ID: 7117175
Yes if you are using temp tables that's ok but I am talking about permanent tables which I can't drop and recreate. I just want to modify existing table. Can we use alter table ? if so how ?

hmk
0
 
LVL 5

Expert Comment

by:amitpagarwal
ID: 7119591
The problem with alter table is that it will add a column at the end of the table - which I guess may not fulfill your requirement ( you want to add a column anywhere you like ? ) - so i guess you need to create a new table and drop the old one ..

Thanks.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 10

Expert Comment

by:bret
ID: 7122001
In 11.9.2, you would have to create a new table with the
desired column layout and move the data to the new table
(as in the SELECT INTO technique discussed earlier [note that using temp tables like that is dangerous, where would you be if there was a power failure right after "drop table", while the data thus only existed in the temp table?]).


However, 11.9.2 is an obsolete, end-of-lifed version of ASE.  Current versions support more options with ALTER TABLE, such that you could do something like this:

create table mytable (a int, b int)
go
alter table mytable add newcol int default 2, b2 int default 3)
go

-- At this point you have a table (a,b,newcol,b2)

update mytable set b2 = b
go
alter table mytable drop b
go
sp_rename "mytable.b2", "b"
go

-- At this point you have a table with the columns in the desired default order of (a,newcol,b)


 


 
0
 

Author Comment

by:hmk
ID: 7138137
It's excellent idea. But i have a problem while doing this.
I was testing this in tempdb  by creating a table and I am getting error "incorrect syntax near the keyword DROP"

My original table structure is like this
OldTable (a int,b int, c char(12))

And I want oldtable to look like this
NewTable (a int,b int,newcolumn int, c char(12))

I created the table  tempdb..NewTable same as OldTable structure. executed following statements to change the table structure.

alter tempdb..NewTable
add newcolumn decimal(13,8) null,c1 char(12) null

update tempdb..NewTable
set c1 = c

untill now it works fine but while executing following statement i am getting error. I don't find any syntax errors in the statement. And I have a question for you , what if the field(c) has index on it. Do we need to drop index first and then column(c).And also do we need to recreate the index on the field(c1) before rename it.

alter tempdb..NewTable drop c

Please reply.
0
 
LVL 10

Expert Comment

by:bret
ID: 7138157
try "alter table tempdb..NewTable drop c"

-bret
0
 

Author Comment

by:hmk
ID: 7138387
I tried Still same problem.


hmk
0
 

Author Comment

by:hmk
ID: 7138414
bret,

I found this on sybase site.
alter table drop column is an unsupported command in release 11.9.2 and earlier, with possible dangerous outcomes.
Hers is the link.

http://www.sybase.com/detail/1,6904,671,00.html



hmk
0
 

Author Comment

by:hmk
ID: 7138511
I tried Still same problem.


hmk
0
 

Author Comment

by:hmk
ID: 7143940
The only way is to use insert data into a #table and load from it into new table.
This I know it So I am going to delete this question.

Thanks for sharing your ideas.

hmk.
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 8240697
PAQ'd and 100 points refunded

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

885 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