how to use select into without adding duplicate records

  • I have a query with 2 inner joins and an IN.

    But unfortunately i can't use the query because the application needs a normal table i.e select a,b,c from d

    So i was thinking of using a select into newtable from OldTable against the query which worked.

    But how can i setup this process to insert to the table new data

    Thanks in advance

  • Patrick Ige (6/1/2009)


    I have a query with 2 inner joins and an IN.

    But unfortunately i can't use the query because the application needs a normal table i.e select a,b,c from d

    So i was thinking of using a select into newtable from OldTable against the query which worked.

    But how can i setup this process to insert to the table new data

    Thanks in advance

    Well, not much to work on, so here goes the requests:

    Please provide the DDL (create table statements) for alll tables involved, sample data (in a readily consummable format - just cut, paste, and execute in SSMS to load the tables), expected results based on the sample data, and the code you currently have written to solve the problem.

    Need help with the above? Read the first article I reference below in my signature block. The more work you do up front, the better answers you will get in return.

  • Lynn..

    I don't know what else to write.

    I have a query that has 3 tables joined that result in a resultset.

    I can't use this query because it won't work with this particular application.

    So i need a flat table as i explained earlier.

    i can use a select into to wrap the query and insert into this new table.

    But what can i do to update/add to this new table?

    As this is the table i will be using.

  • To avoid adding duplicate records, you might want to look at using "where not exists ()" or "where not IN ()"

    You might also be able to use the EXCEPT operator.

    To get further help, you will really need to provide table structures (including constraints) (DDL), and test data for these tables (DML). As a benefit, you then get TESTED code.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • Patrick Ige (6/1/2009)


    Lynn..

    I don't know what else to write.

    I have a query that has 3 tables joined that result in a resultset.

    I can't use this query because it won't work with this particular application.

    So i need a flat table as i explained earlier.

    i can use a select into to wrap the query and insert into this new table.

    But what can i do to update/add to this new table?

    As this is the table i will be using.

    Provide us with the DDL (create table statements for the tables involved in your query), sample data (in a consummable format to load the tables you provide, should be able to cut, paste, and execute in SSMS), the expected results you are looking for (not what you are currently getting), and the code you have written so far that isn't working.

    Also, make sure the sample data is sufficient to show the problem(s) you are experiencing as well.

    Just explaining things isn't always enough. Sometimes you have to show us what you are trying to accomplish. For me seeing something visually helps me understand the problem better.

    If you have any problems with this, please read the first article I reference in my signature block below. It provides you with step by step instruction on what you need to do to get better answers to your questions.

  • Is there any reason you can't drop the flat table, and then recreate it using the select into everytime you want to update it?

  • Maybe you can create a view based on your query and then select data from it?

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply