Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 24th, 2004, 08:06 PM
Authorized User
 
Join Date: Mar 2004
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to gmoney060 Send a message via MSN to gmoney060
Default Select Into Statement I think, Basic question

I need to take recordsets from one table and transfer some of them to another table in a diffrent database. I know it is possible and I think i use the select into statement... I saw this on another post:

SELECT select_list
[INTO new_table_]
FROM table_source
[WHERE search_condition]
[GROUP BY group_by_expression]
[HAVING search_condition]
[ORDER BY order_expression [ASC | DESC] ]

So would it be Select * From Players INTO where something

But how do i actually execute that.... Like how do i open the databases and such?

 
Old December 25th, 2004, 08:18 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

You can use it like this by creating a store procedure then executing it

Code:
create store procedure cr_sp_selectinto
as
select * into <destination> col1, col2, col3 
from <source>
where <criteria>
When referencing another database use four part naming
Code:
database.dbo.table.object
or
Code:
database..table
Code:
execute cr_sp_selectinto
Jaime E. Maccou
 
Old December 27th, 2004, 07:14 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Anantsharma Send a message via Yahoo to Anantsharma
Default

HI,

Yes its possible but be carefull while using this. This may cause loss of existing data.

Better u use this:-
-----------------

INSERT INTO DB1.DBO.TABLE1 (col1,col2) VALUES (SELECT COL1,col2 FROM DB2.DBO.TABLE2......)


THIS IS SAFE AND MUCH LOGICAL. For table names in different databases, use 4 part database name.






B. Anant
 
Old December 29th, 2004, 08:26 AM
Registered User
 
Join Date: Dec 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to abhishek_255 Send a message via Yahoo to abhishek_255
Default

IF the two databases on different servers then use this code:

INSERT INTO OPENDATASOUCE('SQLOLEDB', 'Data Source=Datasoucename;User ID=userid;Password=password')DB1.DBO.TABLENAME SELECT * FROM OPENDATASOURCE('SQLOLEDB', 'Data Source=Datasoucename;User ID=userid;Password=password' ).DB2.DBO.TABLENAME

Note : The First Datasouce contains the connection string where you want to append records.
The Second Datasource contains the connection string from where you want to fetch records.
IF They are on same server then use this code:

INSERT INTO DB1.DBO.TABLENAME SELECT * FROM DB2.DBO.TABLENAME

May be this help you

Regards,

Abhishek Kumar
Software Developer
 
Old December 31st, 2004, 02:52 AM
Registered User
 
Join Date: Dec 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

this is to remind that after use SELECT INTO, check your index, constraint and default value. new talble might lost them.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Select from another select statement to a repeater simsen ASP.NET 2.0 Professional 0 May 2nd, 2007 04:34 PM
Question on select statement RinoDM SQL Server 2000 3 January 19th, 2007 10:21 PM
Select statement Sarju Mehta SQL Server 2000 1 March 30th, 2004 03:16 PM
select statement collie MySQL 0 January 14th, 2004 04:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.