Solved

Using a TSQL cursor to import data

Posted on 2011-03-08
2
192 Views
Last Modified: 2012-11-17
I have written an export routine in TSQL which imports data from a flat file database into a new database which contains different tables but links all the fields to the main data table.

Having been able to successfully import one data record and all of its linked fields (USING SELECT AND INSERT INTO statements, I need to automate the process using a cursor and import thousands of records. I would like to achieve the following:

Import from a select statement all of the main table data, which I believe would be loaded into the cursor. This holds the foreign key for linking the other tables

I then want to pass the record IDs from the above results and somehow link this with the next batch of inserts in the cursor so that all of the data is linked to the correct tables when running the cursor?

How would I go about this in TSQL?
0
Comment
Question by:mbs2000
  • 2
2 Comments
 
LVL 15

Expert Comment

by:derekkromm
ID: 35069055
If you're able to link between the 2 tables, then a cursor may not be necessary.

First, you can do your insert into the main table.

Then, join your second table to the new main table based on the FK relationship. This will get you the ID information you require for the insert statement. Then you should have the necessary information to run the insert.
0
 
LVL 15

Accepted Solution

by:
derekkromm earned 500 total points
ID: 35069066
Slightly more detailed, say you have Q1 and Q2 as the 2 queries you're trying to populate.

First, you can just insert into table1 select * from Q1

Then, you would do something like

insert into table2 select table1.ID, Q2.* from Q2 inner join table1 on Q2.FKField = table1.FKField

Since there should be some sort of natural relationship already present
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

14 Experts available now in Live!

Get 1:1 Help Now