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
476 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
  • 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sql Query Datatype 2 20
sql query questions 2 26
SSRS troubles 4 23
How do I Start SQL Management Studio 2012 17 21
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

863 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

23 Experts available now in Live!

Get 1:1 Help Now