I was thinking of putting the data and then altering the table, but your solutions seem fine too.. but my question was "how" ?
bu thanks for the idea..
Main Topics
Browse All Topicshi experts.
i am using mssql 2000
i have 2 databases, named :
db1 and db1,
i need to copy some of the tables in db1 to db2 with their indexes, foreign keys, etc.
I used :
SELECT * INTO db2.dbo.test FROM db1.dbo.test
(db2.dbo.test does not exists)
it works only for structure and data
is there any way to take indexes, foreign keys, etc ?
i need a sp for this job i think...
thanks for your help...
P.S : i can't use any third-party program because i am coding a visual basic program. so i should do this with sql (i suppose).
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
>> but my question was "how" ?<<
How what? Generate the script or the Insert?
The easiest way to generate the script for the structure is to use Enterprise Manager, highlight all the appropriate tables, right click "All Tasks" | "Generate SQL Scripts..."
As far as doing the INSERT that is dependent on the structure of your tables, but something like this:
INSERT db2.dbo.test
Select *
FROM db1.dbo.test
Although it is best to explcitly name the columns.
I am writing a backup module for a program that has 150-200 tables, each have indexes, foreign keys. the tables are like xxx_10_13 , xxx_14_15. each table names means another firm, so the user should choose the ones to backup, and the script would get the name of the table from dropdown and copy it to the backup database.
so the script (i suppose it would be stored procedure) should get the constants as well as data.
- create table
- insert the data
- alter the indexes and foreign keys.
is there any sp that copies indexes, foreign keys, defaults in mssql ? or should i do it looping through sysobjects ?
yeah, it is a nightmare.. but they don't want to take full backup. cause they would use this program both for backup and giving data to other program users. and the given data sould not be the full backup but only some tables (which is a security issue i couldn't even care to understand).
but i am in a very bad situation now, and need help from experts. that's why i post this question.
"xxx_14_15. each table names means another firm"
So I suppose it's too late to change teh design and store all firms in one table with a "firmID" and not multiple tables.
Like we said above, your best bet is to probably generate the SQL Scripts with enterprise manager. Another option would be to use SQLDMO to do it on the fly (sample: http://www.sqlteam.com/ite
This just has a bad feel...
i need only hints about using sysobjects table to loop through table related items to generate them automaticly. like getting the indexes of a table and create the same indexes in another one through a stored procedure.. if i could do that, i believe i can write a full functional sp that does that job.
i tried to use sysobjects because i saw a script like :
select sysobjects.name Table_Name,
(Select rows from sysindexes
where id = sysobjects.id
and indid = 0) Rows
from
sysobjects
where
xtype = 'u'
and objectproperty(sysobjects.
which gives the tables without indexes...
but let me look at SQLDMO a bit.
if it helps anyone, i rewrote
http://www.databasejournal
this script written and it worked well.
thanks for the sql-dmo hint
and another method i found is
http://www.databasejournal
Business Accounts
Answer for Membership
by: acperkinsPosted on 2005-09-05 at 14:58:42ID: 14825579
You need to generate the script that creates the table, indexes, foreign keys, etc. and then use INSERT to copy the data instead of SELECT ... INTO