Solved

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

Posted on 2002-06-26
11
2,465 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A Short Story about the Best File Recovery Software – Acronis True Image 2017
Is your company's data protection keeping pace with virtualization? Here are 7 dynamic ways to adapt to rapid breakthroughs in technology.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

10 Experts available now in Live!

Get 1:1 Help Now