Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2003
    South Africa

    Unanswered: Importing and merging csv using sql

    Hi Gurus.

    My client drops for me many files like this on a shared drive M: daily

    I would like you to advice on how to write a SQL code (that can include DTS if possible) that will take this files, merge them into one (since they have same columns) and send them to another client as one file. But it must only take the files of the same date and must not resend files i have send already. I need this to be an automated process since the files are dumped into M:drive midnite and I need this code so that I can schedule it as a job and run around 4h00am.

  2. #2
    Join Date
    Nov 2002

    I would do all (and where is that code) in a stored procedure.

    I would

    1. us xp_cmdshell to interogate the directory and store all of the contents in a temp table
    2. I would then parse it out and look for the files I want
    3. I would then load these file to a temp table and audit them 1 at a time using bcp
    4. When everything looked good, I would insert from the temp table to the final destination
    5. I would then use xp_cmdshell again to vreate an datetime stamped sub archive folder, and would move EVERYTHING to that archive (i'm a neat freak...well...)
    6. Thoughout the process I would echo out messages as to what;s going on to a file, and on the way out I would bcp that file to a log table.

    That's what I would do.

    If you want I can dig up some code.

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  3. #3
    Join Date
    Sep 2004
    Helsinki, Finland
    I would also go the route that Brett suggested, there might be some "features" that you could skip, but on the other hand you get quite flexible code if you follow his suggestions!

    The only thing that I would do differently is that I would create one folder for the archived files and store them there (with dates as filenames), but this is just a matter of personal preference.

    Why would you like to include DTS to this?
    ...I know that DTS is good for a lot of things, but personally I find scritping much more portable (this might have something to do with that I haven't used DTS that much, so my opinion could change in the future).

    Check out this page from Nigel Rivett: should at least get an idea of how to aproach the problem.


  4. #4
    Join Date
    Feb 2002
    Assam, India


    I would do like Brett Kaiser and query the directory where these files are kept. And store all the contents in a #temp table. What I would do differently is that I would use a directory structure. I would for example keep the files in C:\CSV and once the file has been processed I would move the file to another directory say C:\CSV\sent by using a stored procedure.

    In the database say TEST, I would create a table test_csv with the same fields as the .csv file.

    I will then use a procedure like this one, I have written it specially for you. The input parameter will come from the #temp table well which means this procedure will be called from another procedure that will have loop for the filenames field and the procedure csv_test will be called from inside the loop.

    -- csv_test 'C:\CSV\Book2.csv'
    create proc csv_test
    @filename varchar(70)

    declare @cmd as varchar(100)
    declare @cmd_insert as varchar(300)

    SET @filename = replace(@filename, '''', '''''')
    set @cmd_insert = 'BULK INSERT TEST.dbo.test_csv FROM ''' + @filename + ''' WITH ( FIELDTERMINATOR = '','', ROWTERMINATOR = ''\n'' )'
    exec (@cmd_insert)

    set @cmd = 'xp_cmdshell ''move ' + @filename + ' C:\CSV\SENT'''
    exec (@cmd)



    And then at last transfer the table to the destination table and truncate csv_test. Remember you can do everything using a procedure and schedule a DTS to run at a specified time every day.

    Roshmi Choudhury
    Last edited by Roshmi Choudhury; 09-23-04 at 01:09.

Posting Permissions

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