Solved

How do I bulk insert only a partial record into a MS SQL db with a stored proc.?

Posted on 2010-08-16
4
480 Views
Last Modified: 2012-06-27
Dear experts,

I need to bulk insert 450 student profiles into a MS SQL db. The student profile table contains 48 fields. The school has provided a tab delimited file but only the first 12 fields have data. Mapping the insert for all 48 fields is tedious and is complicated as 1 error can throw the entire insert off.  For a simple test of the bulk insert I created a table and text (tab delimited) file. The db is on a unix system. The code below is the code that worked for inserting 3 fields. How would I modify it to add a record including the other 9 fields...
             [phone_number]  nchar(25),
             [student_id]    int,
             [ethnicity]     nchar(25),
             [gender]        nchar(10),
             [date_of_birth] smalldatetime,
             [grade]         smallint,
             [he_she]        nchar(10),
             [him_her]       nchar(10),
             [his_hers]      nchar(10),  

...without having to map all 48?

Code that works for bulk insert 3 fields, unix system:
ALTER PROCEDURE [dbo].[uspImportStudentInfoNoDuplicates]
      -- Add the parameters for the stored procedure here
--      @p1 int = 0,
--      @p2 int = 0
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;


CREATE TABLE ImportStudentTempTable
(last_name varchar(50),
first_name varchar(50),
middle_name varchar(50)
)

DECLARE @bulk_cmd varchar(1000)
SET @bulk_cmd = 'BULK INSERT ImportStudentTempTable
FROM ''\\*********Location of txt file'
WITH (ROWTERMINATOR = '''+CHAR(10)+''')'
EXEC(@bulk_cmd)


-- Insert the new student records
INSERT INTO ImportDataTest
   SELECT last_name, first_name, middle_name
   FROM ImportStudentTempTable


DROP TABLE ImportStudentTempTable

END
0
Comment
Question by:bobbellows
[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
4 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33445300

ALTER PROCEDURE [dbo].[uspImportStudentInfoNoDuplicates]
      -- Add the parameters for the stored procedure here
--      @p1 int = 0,
--      @p2 int = 0
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;


CREATE TABLE #ImportStudentTempTable
(last_name varchar(50),
first_name varchar(50),
middle_name varchar(50),
[phone_number]  nchar(25),
[student_id]    int,
[ethnicity]     nchar(25),
[gender]        nchar(10),
[date_of_birth] smalldatetime,
[grade]         smallint,
[he_she]        nchar(10),
[him_her]       nchar(10),
[his_hers]      nchar(10)
)

DECLARE @bulk_cmd varchar(1000)
SET @bulk_cmd = 'BULK INSERT ImportStudentTempTable
FROM ''\\*********Location of txt file'
WITH (ROWTERMINATOR = '''+CHAR(10)+''')'
EXEC(@bulk_cmd)


-- Insert the new student records
INSERT INTO tblName
(last_name,first_name,middle_name,[phone_number],[student_id],
 [ethnicity],[gender],[date_of_birth],[grade],[he_she],[him_her],[his_hers])
   SELECT * -- all 12 fields
   FROM ImportStudentTempTable


DROP TABLE #ImportStudentTempTable

END

Open in new window

0
 

Author Comment

by:bobbellows
ID: 33451190
Your solution worked perfectly and I'll give you the points. I'm still learning about stored procedures having only written a few. Can you tell me why you used the "#" in front of the temp table name? Thanks. Bob
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33451203
Tables created with a name of # are temporary and live only while the session is active.
If they are created in a SP, you don't even need to drop them - they automatically disappear.
Other options are ## (these stay until the server is rebooted and live in tempdb) and @ - these are variables that work like a table.
It is not good practice to create a [real] table within an SP, even if you drop it, just for the sake of some temporary work.

Regards
0
 

Author Closing Comment

by:bobbellows
ID: 33451377
Did exactly what I needed to do and even answered a question to help me learn about Stored Procedures! Awesome
0

Featured Post

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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 INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

695 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