Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bulk insert using stored procedure

I have a query which is working fine:

BULK INSERT ZIPCodes  FROM  'e:\5-digit Commercial.csv'  WITH  (       FIRSTROW = 2 ,     FIELDTERMINATOR = ',',      ROWTERMINATOR = '\n'  ) 

but now I want to create a stored procedure for it.

I have written below code to make its stored procedure:

create proc dbo.InsertZipCode @filepath varchar(500)='e:\5-digit Commercial.csv' as begin BULK INSERT ZIPCodes  FROM  @filepath  WITH  (       FIRSTROW = 2 ,     FIELDTERMINATOR = ',',      ROWTERMINATOR = '\n'  ) end 

but its showing error:

Msg 102, Level 15, State 1, Procedure InsertZipCode, Line 6 Incorrect syntax near '@filepath'.

Msg 319, Level 15, State 1, Procedure InsertZipCode, Line 7 Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

Please tell me what I am doing wrong and what I can do to make it work in stored procedure.

Thanks

like image 1000
Dr. Rajesh Rolen Avatar asked Oct 29 '10 09:10

Dr. Rajesh Rolen


People also ask

Can stored procedure have multiple insert statements?

yes it would, and the question doesn't specify anything other than "a procedure to insert into 2 tables" - which is what this does.

How can we insert bulk data in a table in SQL?

The basic syntax for bulk importing data is: INSERT ... SELECT * FROM OPENROWSET(BULK...) When used in an INSERT statement, OPENROWSET(BULK...)

How do I insert 1500 records in SQL?

First query USE CustomerDB; IF OBJECT_ID('Customer', 'U') IS NOT NULL DROP TABLE Customer; CREATE TABLE Customer ( CustomerID int PRIMARY KEY IDENTITY, CustomerName nvarchar(16), ...about 130 more columns... ); INSERT INTO Customer VALUES ('FirstCustomerName', ...), ... 1500 more rows...


2 Answers

There's nothing wrong with your stored procedure code - the point is: the BULK INSERT command cannot accept a file name as a variable.

This does work:

BULK INSERT ZIPCodes  FROM  'e:\5-digit Commercial.csv'  WITH  

but this never works - within a stored proc or not:

DECLARE @filename VARCHAR(255) SET @filename = 'e:\5-digit Commercial.csv'   BULK INSERT ZIPCodes  FROM @filename WITH  

So you just cannot do it this way, unfortunately. You could consider building up your BULK INSERT statement as a string (with a fixed file name) and then execute it as dynamic SQL - but I don't really see any other solution.

DECLARE @filepath nvarchar(500) SET @filepath = N'e:\5-digit Commercial.csv'  DECLARE @bulkinsert NVARCHAR(2000)  SET @bulkinsert =         N'BULK INSERT ZIPCodes FROM ''' +         @filepath +         N''' WITH (FIRSTROW = 2, FIELDTERMINATOR = '','', ROWTERMINATOR = ''\n'')'  EXEC sp_executesql @bulkinsert 
like image 78
marc_s Avatar answered Sep 23 '22 03:09

marc_s


There is an alternative to dynamic SQL if you have access to the SQLCmd exe.

The SqlCmd utility allows you to pass string replacement variables using the -v argument.

You can use a template variable named "filepath" which will be replaced when you execute the script via the cmdline.

The SQL script would look like:

BULK INSERT ZIPCodes  FROM  '$(filepath)'  WITH  (       FIRSTROW = 2 ,     FIELDTERMINATOR = ',',      ROWTERMINATOR = '\n'  ) end 

You would then execute the script from a commandline using something like the following:

sqlcmd -b -S SERVER\INSTANCEHERE -E -i "PATH\FILENAMEHERE.Sql" -v FilePath = "e:\5-digit Commercial.csv" -s "|" 

The important part of the example is the -v argument:

-v FilePath = "e:\5-digit Commercial.csv" 
like image 41
Ed Ajaz Avatar answered Sep 19 '22 03:09

Ed Ajaz