Solved

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

Posted on 2002-06-26
11
2,466 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Update query 9 359
sybase optimizer statistics 2 38
SIMPLE QUESTION BACKUP AND RESTORE ORACLE DATABASE 13 86
sybase output only middle rows 4 11
Fine Tune your automatic Updates for Ubuntu / Debian
Note: This is the third blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   We’ve been talki…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

20 Experts available now in Live!

Get 1:1 Help Now