Results 1 to 9 of 9

Thread: [RESOLVED] SQL Help

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Resolved [RESOLVED] SQL Help

    I'm trying to write an SQL expression so that I can pull all the data from the TEST table into the MAIN table where the Product number (PROD) of Test is not in the ALTER table. Basically I'm trying to do an INSERT INTO nested query. Here is the code I have so far and it's not working:

    VB Code:
    1. INSERT INTO [MAIN] ([PICKSL], [SLOT], [RES],[PROD],[DESC],[PACK],[MANU ID],[HITIX],[PALLET ID],[PALLET QTY],[DFP]) ;
    2. SELECT t.PICKSL, t.SLOT, t.RES, t.PROD, t.DESC, t.PACK, t.[MANU ID], t.HITIX, t.[PALLET ID], t.[PALLET QTY], t.DFP
    3. FROM Test AS t;
    4. WHERE (((Exists (SELECT *
    5.   FROM [ALTER] a
    6.   WHERE  a.PROD = t.PROD
    7.    ))=False));

    Any help would be greatly appreciated.

    Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: SQL Help

    DESC is a resevre word in SQL so you should put it in square brackets as well. And why do you have semi-colos at the end. If it is a one query then there shouldn't be any semi-colons in between.

    Try this
    Code:
    INSERT INTO 
    	[MAIN] ([PICKSL], [SLOT], [RES],[PROD],[DESC],[PACK],
    	[MANU ID],[HITIX],[PALLET ID],[PALLET QTY],[DFP])	
    	SELECT 
    		t.PICKSL, t.SLOT, t.RES, t.PROD, t.[DESC], t.PACK, t.[MANU ID], 
    		t.HITIX, t.[PALLET ID], t.[PALLET QTY], t.DFP
    	FROM Test AS t WHERE Not Exists (SELECT *  FROM [ALTER] a WHERE  a.PROD = t.PROD)
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: SQL Help

    Why are there semi-colons inside the SQL statement? (before the Select and Where)

    Your Where clause could be written a bit more cleanly:
    Code:
    WHERE Not Exists (SELECT * 
      FROM [ALTER] a 
      WHERE  a.PROD = t.PROD 
       );
    However, whether this format is the correct method for Insert/Select depends on which DBMS you are using.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: SQL Help

    I am using MS Access, I have made the changes per Shuja Ali and si the geek; however, the data in the TEST table is still not being inserted into the MAIN table. Any other suggestions?

  5. #5
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: SQL Help

    It all depends on whether this condition is evavluating to true or false.
    Code:
    WHERE Not Exists (SELECT *  FROM [ALTER] a WHERE  a.PROD = t.PROD)
    Try running just this part and see if it returns any rows
    Code:
    SELECT 
    		t.PICKSL, t.SLOT, t.RES, t.PROD, t.[DESC], t.PACK, t.[MANU ID], 
    		t.HITIX, t.[PALLET ID], t.[PALLET QTY], t.DFP
    	FROM Test AS t WHERE Not Exists (SELECT *  FROM [ALTER] a WHERE  a.PROD = t.PROD)
    And are you getting any error message or is it just that data is bnot getting inserted.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: SQL Help

    No I'm not getting any error message; it appears to work; however, nothing is being inserted into the MAIN table. I did try just the select statement and if retrieved information. Any other suggestions? Thanks for your time.

  7. #7
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: SQL Help

    Quote Originally Posted by manofsteel00
    No I'm not getting any error message; it appears to work; however, nothing is being inserted into the MAIN table. I did try just the select statement and if retrieved information. Any other suggestions? Thanks for your time.
    Does it show 0 Rows Inserted or any other messagebox?

    Can you post what are you using again? Make sure there are no semicolons in the query, just one semi-colon at the end or else it will not insert anything.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: SQL Help

    There is no messagebox statement. I am testing it in Access itself as a query. It generates information as a query but never inserts the information into the MAIN table. The code I am using is
    INSERT INTO
    [MAIN] ([PICKSL], [SLOT], [RES],[PROD],[DESC],[PACK],
    [MANU ID],[HITIX],[PALLET ID],[PALLET QTY],[DFP])
    SELECT
    t.PICKSL, t.SLOT, t.RES, t.PROD, t.[DESC], t.PACK, t.[MANU ID],
    t.HITIX, t.[PALLET ID], t.[PALLET QTY], t.DFP
    FROM Test AS t WHERE Not Exists (SELECT * FROM [ALTER] a WHERE a.PROD = t.PROD)

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: SQL Help

    Found the problem: I saved the query and it worked fine. My bad. Thanks for all your help though.

    Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width