Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2002-06-26
11
Medium Priority
?
2,476 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
[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
  • 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
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

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Files go missing when using DFS (Distributed File System) Replication and how to recover them and fix it.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

705 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