Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Update a Table from a text file by running a store procedure......

Posted on 2010-11-16
2
609 Views
Last Modified: 2013-11-30
I have a table: TableTest in Mircosoft SQL Server that I would like to get updated on a regular basis when the Update.txt file get updated. The txt file reside on the SQL server

All the content of the txt file need to replace the content of the table once the text file get updated.

Should I write a store procedure for this and if so? How do I do this?
Text File: Update.Txt

V04510;5/28/2009;00729783;Q10070849I;9/29-30/07 RMB INV#22738;156;SHORT, JUDY

Table TableTest

SELECT TOP 1000 [Vendor Number]
      ,[Date]
      ,[Check Number]
      ,[Reference Number]
      ,[Description]
      ,[Payment Amount]
      ,[payee]
  FROM [AdvancePayroll].[dbo].[TableTest]
0
Comment
Question by:yguyon28
2 Comments
 
LVL 10

Accepted Solution

by:
wls3 earned 166 total points
ID: 34146912
I tend to try and do this in two steps:

1) run a batch file as a scheduled task on whatever interval you need.  The body works well like this (assuming sqlcmd):

sqlcmd -S Yourserver\instancename -U username -P password -i "C:\script.sql"

alternately, you can use a trusted connection

sqlcmd -S yourserver\instancename -E -i "C:\script.sql"

If you do not specify the instance name, you may run into what appear to be funky connectivity errors.

2) store the body of the script in a file with a .sql extension (referenced above as C:\script.sql).  This would contain something to the effect of:

BULK INSERT [AdvancePayroll].[dbo].[TableTest]
    FROM 'c:\update.txt'
    WITH
    (
        FIELDTERMINATOR = ';',
        ROWTERMINATOR = '\n'
    )

Refer to this article for BCP examples:

http://sqlserver2000.databases.aspfaq.com/how-do-i-load-text-or-csv-file-data-into-sql-server.html
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question