Solved

Using a TSQL cursor to import data

Posted on 2011-03-08
2
194 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query - which index being used? 2 61
SQL Server 2012 express 24 40
CONVERT date time to a different time zone. 2 61
Need help with a query 6 77
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

830 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