Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
| Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language 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
  #1 (permalink)  
Old November 3rd, 2003, 09:55 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default Inserting Returned Records

Hello Everyone and thanks for your help in advance. I have written a query similar to the following:

SELECT *
FROM tblTemp
WHERE ID NOT IN (
   SELECT ID FROM tblMain)

However, what I want to do is, once the recordset is returned, insert them into another table (tblNew). However, I am not sure if this can be done in one SQL statement (or even if it should be). Any help would be greatly appreciated. Thanks.

  #2 (permalink)  
Old November 4th, 2003, 05:33 AM
Authorized User
 
Join Date: Jun 2003
Location: Edinburgh, , United Kingdom.
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

To insert the data into tblNew use the following :-

INSERT tblNew
(col1, col2, col3)
SELECT col1, col2, col3
FROM tblTemp
WHERE ID NOT IN (
   SELECT ID FROM tblMain)

I would explicitly give the column names you will be inserting as select * selects all in the table and is generaly bad practice you never know what columns will be added etc.

Nickie
  #3 (permalink)  
Old November 4th, 2003, 01:05 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Cardiff, , United Kingdom.
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you want to return the recordset as well as insert the returned rows into another table then you will have to write a batch or script to accomplish this. In SQL Server you could do it like this:

Code:
INSERT INTO tblNew
(col1, col2, col3)
SELECT col1, col2, col3
FROM tblTemp
WHERE ID NOT IN (
   SELECT ID FROM tblMain)
GO

SELECT * FROM tblNew
I hope that helps

Regards
Owain Williams


Similar Threads
Thread Thread Starter Forum Replies Last Post
no records returned when trying to insert data ronny Classic ASP Databases 1 February 2nd, 2006 12:22 PM
Records not returned with where clause Trojan_uk SQL Server 2000 2 December 4th, 2003 10:03 AM
Limited 1000 records returned from Active Directoy LPetras .NET Web Services 0 July 30th, 2003 09:45 AM





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