Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 cerate table as an old table

Author  Topic 

xpandre
Posting Yak Master

212 Posts

Posted - 2002-01-31 : 11:04:10
Hi guys,
How do i create a new table similar to an existing one in the database?

eg:
i have t1 as
id int
idref int

i want to create a new table t2 at run time exactly similar to t1 assuming i dont know the construct of t1..
Thank you
sam

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-01-31 : 11:25:12
SELECT * INTO t2 FROM t1

Be careful of doing this with large tables, it can use up a lot of resources and may cause performance issues. Also, this won't carry over indexes or constraints from the t1 table.

Go to Top of Page

xpandre
Posting Yak Master

212 Posts

Posted - 2002-01-31 : 11:27:40
THAT WAS TOOO FAST AND TOOO KEWL robvolk....
THANX A MILLIONSAM

Go to Top of Page

xpandre
Posting Yak Master

212 Posts

Posted - 2002-01-31 : 11:32:15
i used this

SELECT TOP 1 * INTO TEST3 FROM TEST1

and it worked :d

Thanx again robvolk

Sam

Go to Top of Page

xpandre
Posting Yak Master

212 Posts

Posted - 2002-01-31 : 12:11:47
CAN I USE THIS IN a procedure?
thank you
sam

Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-01-31 : 12:37:40
Absolutely, just put a CREATE PROCEDURE in front of it:

CREATE PROCEDURE Top1 AS
SELECT TOP 1 * INTO TEST3 FROM TEST1
SELECT * FROM Test3
DROP TABLE Test3


The 2nd line will return the results from the newly created table. The DROP TABLE is optional, but if this new table is not meant to be a permanent one you should keep it in there.

Go to Top of Page

jbkayne
Posting Yak Master

100 Posts

Posted - 2002-01-31 : 12:54:08
You could improve on this:
SELECT * INTO TEST3 FROM TEST1 WHERE 1=2

this would result in the table structure and 0 rows


quote:

i used this

SELECT TOP 1 * INTO TEST3 FROM TEST1

and it worked :d

Thanx again robvolk

Sam





Go to Top of Page
   

- Advertisement -