+ Reply to Thread
Results 1 to 3 of 3

Thread: Bulk Insert with other requirements..

  1. #1
    Barn Newbie yangski is an unknown quantity at this point yangski's Avatar
    Join Date
    Jul 2009
    Posts
    26
    Rep Power
    3

    Bulk Insert with other requirements..

    Hi guys, I have a code here that inserts a .csv file to an sql server 2005 database.

    Code:
    Bulk insert Sampletb
    from 'C:\Sample\Sample.csv'
    with
    (
    FIELDTERMINATOR = ',',
    ROWTERMINATOR = '\n'
    )
    
    But it directly inserts all the contents of the file to the database.. Want I want to do is to call the Ids from a different table and insert it instead of the name in the a certain field.

    Example:
    Code:
    -Sample.csv-
    Name        City     Type
    ====       ====    ====
    Name1     City1    Type1
    Name2     City2    Type2
    
    
    -Typetb (in the database)-
    TypeId   Type
    =====   ====
    1          Type1
    2          Type2
    3          Type3
    
    -Sampletb(When inserted to the database)-
    
    Id       Name    City     Type
    ====  ====    ====    ====
    1       Name1   City1       1
    2       Name2   City2       2
    
    Any ideas?? Thanks.

  2. #2
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Probably the easiest way I can think of is to do the Bulk Insert into a temp table and then update your Sampletb from that.

    A more complicated method would be to INSERT with an OPENROWSET. For more info on that look here Importing Bulk Data by Using BULK INSERT or OPENROWSET(BULK...). Something like the following might work:
    Code:
    insert into SampletB(name, city, type)
    SELECT    t1.name, t1.city,  t2.typeID
    FROM    OPENROWSET
        ( 
            BULK 'd:\work\test.txt', 
            FORMATFILE = 'd:\work\testImport-f-n.Fmt'
        ) AS t1(name, city, type)
    Join Typetb t2
     on t1.type = t2.type
    
    Info on the FormatFile is here: http://msdn.microsoft.com/en-us/library/ms178129.aspx
    Last edited by Wolffy; August 11th, 2009 at 10:49 AM.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  3. #3
    Barn Newbie yangski is an unknown quantity at this point yangski's Avatar
    Join Date
    Jul 2009
    Posts
    26
    Rep Power
    3

    Quote Originally Posted by Wolffy View Post
    Probably the easiest way I can think of is to do the Bulk Insert into a temp table and then update your Sampletb from that.

    A more complicated method would be to INSERT with an OPENROWSET. For more info on that look here Importing Bulk Data by Using BULK INSERT or OPENROWSET(BULK...). Something like the following might work:
    Code:
    insert into SampletB(name, city, type)
    SELECT    t1.name, t1.city,  t2.typeID
    FROM    OPENROWSET
        ( 
            BULK 'd:\work\test.txt', 
            FORMATFILE = 'd:\work\testImport-f-n.Fmt'
        ) AS t1(name, city, type)
    Join Typetb t2
     on t1.type = t2.type
    
    Info on the FormatFile is here: Using a Format File to Bulk Import Data
    Thank you so much for the suggestion!! Will try this ASAP...

+ Reply to Thread

Similar Threads

  1. Need to send bulk sms
    By micky in forum .NET Development
    Replies: 27
    Last Post: August 30th, 2009, 07:25 AM
  2. INSERT-SELECT errors
    By moonflower333 in forum Microsoft SQL Server
    Replies: 5
    Last Post: November 4th, 2008, 06:43 PM
  3. Help with handling sql insert error
    By Rebelle in forum ASP Development
    Replies: 11
    Last Post: August 29th, 2008, 02:20 PM
  4. can not insert duplicate key
    By guddu in forum Microsoft SQL Server
    Replies: 1
    Last Post: August 20th, 2008, 10:27 AM
  5. send Bulk email
    By peebman2000 in forum .NET Development
    Replies: 2
    Last Post: June 20th, 2008, 11:06 AM

Bookmarks

Posting Permissions

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

SEO by vBSEO