Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using a TSQL cursor to import data

Posted on 2011-03-08
2
Medium Priority
?
203 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
[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
  • 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 1500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

There are some very powerful Dynamic Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a di…
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

618 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