Solved

create procedure to insert/update a table every night

Posted on 2008-06-12
3
335 Views
Last Modified: 2008-09-14
Hi,

I'm fairly new to using SQL server but I'm looking to insert new records into a table every night  or else update to current records.

As an example I've created a table with the following fields: id,FirstName,Age and City.

I've created a text file with the following:

      123a      Garry      30      Leeds
      23fr      Mike      33      London
      a29k      Tony      32      New York
      a34f      John      27      Manchester
      asdr      Claire      18      Liverpoool
      f56g      James      14      Dublin

and updated my table using the following SQL:

BULK insert customer
        FROM 'c:\cust.dat'
            WITH
          (
                FIELDTERMINATOR = '|',
                ROWTERMINATOR = '\n'
              )
GO

SELECT * FROM customer

GO

Every night a text file will be created with records as shown above, and I want to insert new records or update existing records.

How do I create an SQL proc(I assume) to do this?

I would prefer if you could give me the actual SQL which I can test rather than links to other tutorial sites.

Thanks,

0
Comment
Question by:gaz1000
[X]
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
3 Comments
 
LVL 19

Accepted Solution

by:
frankytee earned 80 total points
ID: 21768308
first create a stored proc that does all this and then create an sql job to execute the stored proc every night at whatever time.
2nd, bulk insert into a "staging" table, ie a temporary table, a table identical in structure to your real table, and then compare that table to your real table (customer) to do the comparison as shown below.
assuming your id field is actually unique,

--your current code edited to insert into staging table
bulk insert customerStaging
... etc

--then add this code to update and insert
--update existing
update customer
set
c.FirstName = s.FirstName
c.Age = s.Age
c.City  = s.City  
from customer c join customerStaging s
on c.id = s.id

--insert new
insert customer
select s.*
from customer c right join customerStaging s
on c.id = s.id
where c.id is null
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.

732 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