473,387 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Script for adding fields to table

How can I create a script that updates a table with new fields without
losing the data in the table (i.e., without dropping and recreating the
table)?

Thanks.
Jul 23 '05 #1
5 8803
Use an ALTER TABLE statement:

ALTER TABLE your_table_name ADD new_column_name INTEGER NULL

See Books Online for the full syntax.

--
David Portas
SQL Server MVP
--

Jul 23 '05 #2
Thanks. It would be nice if EM provided a way to automatically script that
for you, the way it does for adding entire tables. But I guess some things
need to be done by hand.

"David Portas" <RE****************************@acm.org> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Use an ALTER TABLE statement:

ALTER TABLE your_table_name ADD new_column_name INTEGER NULL

See Books Online for the full syntax.

--
David Portas
SQL Server MVP
--

Jul 23 '05 #3
EM will generate the script for you. In the Design Table screen, add a
column and then click the "Save change script" button (third on the
toolbar). If you insert a column in the middle of the structure
however, it will generate a script to recreate the entire table,
because there is no way to fix absolute column order with an ALTER
TABLE statement.

It pays to make table mods through Query Analyzer rather than the EM
interface. TSQL is more powerful and gives you more control.

--
David Portas
SQL Server MVP
--

Jul 23 '05 #4
Great! Thanks for the info.

"David Portas" <RE****************************@acm.org> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
EM will generate the script for you. In the Design Table screen, add a
column and then click the "Save change script" button (third on the
toolbar). If you insert a column in the middle of the structure
however, it will generate a script to recreate the entire table,
because there is no way to fix absolute column order with an ALTER
TABLE statement.

It pays to make table mods through Query Analyzer rather than the EM
interface. TSQL is more powerful and gives you more control.

--
David Portas
SQL Server MVP
--

Jul 23 '05 #5
Neil (no****@nospam.net) writes:
Thanks. It would be nice if EM provided a way to automatically script that
for you, the way it does for adding entire tables. But I guess some things
need to be done by hand.


Count yourself lucky for not having found it, and damn the day David
told you that you could do it.

To wit, yes, there is a function for this in Enterprise Manager, but, no,
you should not use it. There are several serious flaws with it, and you
could end up messing up your database.

As David mentioned, EM will recreate the table if you insert a column in
the middle. What he didn't say is that EM will do this also if you change
or drop a column, despite there is an ALTER TABLE syntax for this.
Basically, EM assumes that it's talking to an 6.5 database.

Now, recreating table is necessarily not bad. In fact, this can be
preferable over an ALTER TABLE in some situations, even if ALTER TABLE
is available. But recreating the table, requires you to have some sort
of recovery strategy, if the operation fails half-way. EM applies a
transaction scope, and as long as you run the thing directly from EM,
it's safe, but if you save the script, the script is not safe. Furthermore,
EM's transaction scope is wacko.

Another horror story with EM is that you can start doing a change to
table, then abandon that table and close it. Yet, you may find this
change performed anyway when you save, because this table was referred
by some other table you saved!

So don't use Enterprise Manager to modify tables. The same applies to
the Modify Table function in the new SQL Server Management Studio.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jamie Fryatt | last post by:
Hi everyone, here's what id like to do. I have a table with 2 fields, name and value I need to be able to add multiple records quickly, for example I need to add name value abc 1...
5
by: Tim Morrison | last post by:
Is there any easy way to create a change script as illustrated below for all tables within a database? Right now I would have to create a seperate script for each table. I would like to be able...
2
by: Viorel | last post by:
Adding new row with default values. In order to insert programmatically a new row into a database table, without direct "INSERT INTO" SQL statement, I use the well-known DataTable.NewRow,...
7
by: mattrapoport | last post by:
Hello - I am kinda new to the HTML DOM so I apologize in advance for my ignorance. I have a table made from divs. I am trying to write a script that appends a new row to the table (by cloning...
2
by: Muzzy | last post by:
Hi, I've used information on these newsgroups to build many pages. So I thought that now that I have my script working (something that I've been working on for about a week), I should post it so...
5
by: Kosmos | last post by:
I have traveled the world and the seven seas and I have yet to come up with an answer to this question.... So I'm adding an attachment to an email from access... The following is the code: ...
2
by: Kosmos | last post by:
Alright so I've got this Outlook code written in VBA in Access. The first part, which works, records information about appointment times based on the required days before notification of certain...
2
by: Steve | last post by:
I have zero experience with ODBC. If I have an Access frontend connected to a SQL Database using ODBC, are the tables connected like a frontend/backend Access database where the the tables you see...
82
by: happyse27 | last post by:
Hi All, I modified the user registration script, but not sure how to make it check for each variable in terms of preventing junk registration and invalid characters? Two codes below : a)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.