SQL Server Join 2 Tables Into One

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • marcellus7
    New Member
    • Oct 2008
    • 33

    SQL Server Join 2 Tables Into One

    Hey guys, im having trouble joining two tables into one. Both have different columns, and a matching id. When I try:

    SELECT *
    INTO FinalTable
    FROM Table 1
    Full Join Table 2
    ON Table1.id = Table2.id

    I get an error saying that both tables contain ID. How could I do this without having to specify every column? Each of these tables has over 100 columns.

    Thanks
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Any use of "*" will give you an error.

    1. Make sure your query analyzer output results to delimited-text (not file).

    2. Do a

    Code:
    select top 5 * from Table1
    
    select top 5 * from Table2
    You'll have a formatted-text based result.
    3. Copy the header to an editor, replace your delimiter with comma.
    4. Find the matching key and use alias on those keys.
    5. Paste it back to your query.

    OR......

    Retrieve the column name from system views.

    OR......

    Build your query dynamically...


    Good Luck!!!

    ~~ CK

    Comment

    • marcellus7
      New Member
      • Oct 2008
      • 33

      #3
      Thanks a lot for the response. I was able to get it done by using the Query Editor to do a Select All from one of the tables, then highlight the query, right click, and edit in query designer again, and it generated the list of columns for me.

      Comment

      • ck9663
        Recognized Expert Specialist
        • Jun 2007
        • 2878

        #4
        Yes....that, too :)

        Happy Coding!!!

        ~~ CK

        Comment

        Working...