Solved

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

Posted on 2002-06-26
11
2,470 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sybase SQL Syntax 2 289
How do we check sybase license in ASE 1 3,107
StorageCraft ShadowProtect Sybase VSS? 3 598
Reading ASE Optimizer trace flag / switch output 4 602
Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

756 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