Solved

Need to insert CSV file into multiple tables using bulk insert and stored procedure and relate data in tables

Posted on 2006-10-29
6
2,057 Views
Last Modified: 2008-01-09
I want to import a CSV file into 3 SQL tables - pat_06, sam_06, result_06. The CSV file will be uploaded regularly by users at ad hoc times so I have set up an ASP bulk insert command which triggers the bulk insert when the page is visited.
I have also created a Stored Procedure that bulk inserts the CSV file into a temporary table (temp_pat) and then inserts the data into the different tables. The problem I have is that I need the IDENTITY (patno) of each row of data after it is inserted into the first table, which is then to be inserted along with the other data into tables 2 and 3 so that each row remains related (patno = patno_sam and patno_res respectively). However, with the way I have the Stored Procedure set up at present the @@IDENTITY only copies across, of all the rows of data, the last row's IDENTITY and consequently I have no association between the tabled data.
I am sure I need to do some kind of loop but am not sure how. Can anyone help me out and show me sample code as I am a newbie and as a first stored procedure this has been a steep learning curve.

Copy of my stored procedure -

CREATE PROCEDURE ps_pat_Import
@PathFileName varchar(100),
@patno integer,
@FileType tinyint
AS

DECLARE @SQL varchar(2000)
IF @FileType = 1
 BEGIN
   SET @SQL = "BULK INSERT temp_pat FROM '"+@PathFileName+"' WITH (FIRSTROW = 2, FIELDTERMINATOR = '"",""') "
 END
ELSE
 BEGIN
  SET @SQL = "BULK INSERT temp_pat FROM '"+@PathFileName+"' WITH (FIRSTROW = 2, FIELDTERMINATOR = ',') "
 END

--Step 2: Execute BULK INSERT statement

EXEC (@SQL)

INSERT pat_06 (sname, sampleday, samplemonth, sampleyear)
SELECT sname, sampleday, samplemonth, sampleyear
FROM temp_pat
SET @patno = @@IDENTITY       --ID of the last inserted record
INSERT sam_06 (sampletime, timeu, localid, patno_sam)
SELECT sampletime, timeu, localid, @patno
FROM temp_pat
SET @patno = @@IDENTITY       --ID of the last inserted record
INSERT result_06 (acode, lim, conc, units, patno_res)
SELECT acode, lim, conc, units, @patno
FROM temp_pat

TRUNCATE TABLE temp_pat

Thanks!
0
Comment
Question by:herschellrd
6 Comments
 
LVL 3

Expert Comment

by:mahe2000
Comment Utility
may be you can use dts to do that and configure a job to run the dts.
0
 
LVL 11

Accepted Solution

by:
regbes earned 500 total points
Comment Utility
Hi herschellrd,

Couple of thinsgs to try here

1. add an identity col to the table temp_pat so when the bulk insert populates it it is populated and you can use that as patno in each of your 3 tables
2. the 3 tables look like they have a one to one relationship? if so why not just combine them into one table?


HTH

R.
0
 
LVL 30

Expert Comment

by:nmcdermaid
Comment Utility
What is the identity field in pat_06? If it is patno_pat then do this:

INSERT pat_06 (sname, sampleday, samplemonth, sampleyear)
SELECT sname, sampleday, samplemonth, sampleyear
FROM temp_pat

INSERT sam_06 (partno,sampletime, timeu, localid, patno_sam)
SELECT sampletime, timeu, localid, patno_pat

... ensuring that partno_pat IS an IDENTITY column and patno_sam IS NOT.


But think about what regbes says... why do you have three tables with idetical data?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 30

Expert Comment

by:nmcdermaid
Comment Utility
Sorry, last line should have been:

INSERT sam_06 (partno,sampletime, timeu, localid, patno_sam)
SELECT sampletime, timeu, localid, patno_pat
FROM pat_06


or even better, follow all of regbes instructions and assign the identity in the staging table.
0
 
LVL 42

Expert Comment

by:EugeneZ
Comment Utility
please clarify:
the code will make all inserted recoreds to have same "patno_res" - is it what you want?
--
INSERT result_06 (acode, lim, conc, units, patno_res)
SELECT acode, lim, conc, units, @patno
FROM temp_pat
---
if 'yes'
<--ID of the last inserted record>
do you mean last 'patno_res' or MAximum #?
if 'yes'
try:
select  @patno = max(patno_res) from result_06      --ID of the last inserted record
INSERT result_06 (acode, lim, conc, units, patno_res)
SELECT acode, lim, conc, units, @patno
FROM temp_pat
0
 
LVL 50

Expert Comment

by:Lowfatspread
Comment Utility
consider having a trigger on the intial table do the work of inserting the rows to the other tables...
which shouldn't have the patno as an identity column...

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now