Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
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
 
Old October 6th, 2005, 01:07 PM
Authorized User
 
Join Date: Jun 2004
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default INSERT INTO WHERE NOT IN

I doing something wrong, just I cannot see what it is. I have three columns in one table (tbl_product) I need to insert them into another table if they dont exist already.

scat1, scat2, distributor into columns of the same name in tbl_catergory

INSERT INTO tbl_catergory (scat1,scat2,distributor)
SELECT (scat1,scat2,distributor)
FROM tbl_product
WHERE (SELECT scat1 NOT IN
              (SELECT scat1
                      FROM tbl_product) AND scat2 NOT IN
              (SELECT scat2
                      FROM tbl_product) AND distributor NOT IN
              (SELECT distributor
                      FROM tbl_product)

I know the categories are not in the tbl_catergory, and are in the product table so... i'm doing something wrong.... right?

Again, and again, as always your help is valued

Stuart
 
Old October 6th, 2005, 01:15 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there..

and your error is???

Code:
WHERE (SELECT scat1 NOT IN <----- this select here dont look right..
              (SELECT scat1 
                      FROM tbl_product) AND scat2 NOT IN
              (SELECT scat2
                      FROM tbl_product) AND distributor NOT IN
              (SELECT distributor 
                      FROM tbl_product)


HTH

Gonzalo
 
Old October 6th, 2005, 01:39 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by Stuart Stalker
 I doing something wrong, just I cannot see what it is. I have three columns in one table (tbl_product) I need to insert them into another table if they dont exist already.
What do you mean by the words them and they in the last sentence above?

Remember that an INSERT statement inserts rows into a table, not columns. You seem to be thinking of things in terms of columns and not rows.

Do you mean you want to insert a row into your tbl_catergory (is that really it's name?) table if the corresponding row does not exist in your tbl_product table?

Why would you want to make duplicates like this? Duplicate data is a no-no. See normalization.

That having been said, I think you'll need something like:
Code:
INSERT INTO tbl_catergory (scat1, scat2, distributor)
    SELECT scat1, scat2, distributor
    FROM tbl_product T1
    WHERE NOT EXISTS
    (SELECT scat1, scat2, distributor
        FROM tbl_catergory T2
        WHERE T1.scat1 = T2.scat1
        AND T1.scat2 = T2.scat2
        AND T1.distributor = T2.distributor)
But I have a bad feeling about this. I vaguely recall some issues I had some some time back trying to "see" the contents of a table while I was trying to insert rows into it. The problem was something like the rows being inserted don't come into existence until the INSERT statement has fully completed. Thus, correlating a query against the newly inserted rows won't work because you can't see them until it's too late - until the statement finishes execution.

Try my suggested query to see if it does what you want. If it doesn't, you may have to try something like insert the "missing" rows into a temporary table then load the rows from that.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old October 6th, 2005, 02:53 PM
Authorized User
 
Join Date: Jun 2004
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You are indeed correct rows not columns, sometimes its really hard to explain what your thinking with these things. The Insert statement works great. Basically this is creating a reference point for products and there future categorisation. I think thats three times you have helped me now, thank you so much for your help.
 
Old October 12th, 2005, 12:16 AM
Registered User
 
Join Date: Aug 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

INSERT INTO tbl_catergory (scat1,scat2,distributor)
SELECT (scat1,scat2,distributor)
FROM tbl_product
WHERE (scat1 NOT IN
              (SELECT scat1
                      FROM tbl_product) AND scat2 NOT IN
              (SELECT scat2
                      FROM tbl_product) AND distributor NOT IN
              (SELECT distributor
                      FROM tbl_product)


 
Old October 12th, 2005, 02:32 AM
Authorized User
 
Join Date: Jun 2004
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Didnt we start with this?





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to insert " ". Somesh C# 1 March 6th, 2007 08:45 AM
insert into U.N.C.L.E. SQL Language 3 June 6th, 2006 06:35 PM
trigger to insert current date on insert kev_79 SQL Server 2000 3 January 23rd, 2006 05:58 PM
Insert into jemacc SQL Server 2000 2 March 31st, 2004 08:31 AM
"INSERT INTO" kaz SQL Language 7 December 15th, 2003 07:40 PM





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