Solved

Using a TSQL cursor to import data

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to format datetime as mm/dd/yyyy 6 63
Dataset not reading table data 12 43
How to print the sql query 18 41
SQL Select * from 6 30
This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 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

16 Experts available now in Live!

Get 1:1 Help Now