Results 1 to 13 of 13

Thread: [RESOLVED] sql server 2000 - help with insert from one table to another

Hybrid View

  1. #1
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: sql server 2000 - help with insert from one table to another

    Maybe something like this

    Code:
    set nocount on
    
    declare @zip table (zip char(5), city varchar(100), zip_ik int)
    insert into @zip(zip,city,zip_ik) values('49862','Munising',0)
    insert into @zip(zip,city,zip_ik) values('49862','Christmas',1)
    insert into @zip(zip,city,zip_ik) values('49862','Forest Lake',2)
    
    declare @ldc_zip table (ldc_zip_ik int, ldc_ik int, zip char(5), zip_ik int)
    
    insert into @ldc_zip (ldc_zip_ik,ldc_ik, zip, zip_ik) Values(0,0,'49862',null)
    
    --update existing ldc_zip
    update ldc
    set zip_ik = zip.zip_ik
    from @ldc_zip ldc
    inner join @zip zip on ldc.zip = zip.zip
    
    --insert missing cities
    insert into @ldc_zip (ldc_zip_ik,ldc_ik, zip, zip_ik)
    select	ldc.ldc_zip_ik,
    		ldc.ldc_ik,
    		ldc.zip,
    		zip.zip_ik
    from @ldc_zip ldc
    inner join @zip zip on ldc.zip = zip.zip
    where zip.zip_ik not in (select zip_ik from @ldc_zip)
    
    select *
    from @ldc_zip
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  2. #2

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: sql server 2000 - help with insert from one table to another

    Quote Originally Posted by wild_bill View Post
    Maybe something like this...
    Thanks, I may use some of this, but some of it I can't. I don't want to rebuild/modify the zip table except to add the PK (that's done)...but probably the insert of the missing cities I can use.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

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