Solved

Import csv file, trouble with the file name when given as parameter.

Posted on 2015-02-02
2
112 Views
Last Modified: 2015-02-02
Hello,

I have this Stored Procedure:

USE [MyDB]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[spImport852Daily]
	@fName nvarchar(100)
AS
BEGIN
	SET NOCOUNT ON;

BULK INSERT dbo.tbl852Daily
FROM ''' + @fName + '''
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)

END
GO

Open in new window


Above doesn't work, I get this error:

Msg 4860, Level 16, State 1, Procedure spImport852Daily, Line 10
Cannot bulk load. The file "' + @fName + '" does not exist.

If I replace the @fName with a path to the file (c:\import.csv) then it works perfectly.

What am I doing wrong?

tnx
~j
0
Comment
Question by:prosit
2 Comments
 
LVL 13

Accepted Solution

by:
LIONKING earned 500 total points
ID: 40584534
Since the filename is a parameter, you would have to use dynamic SQL to execute this.
A quick and dirty way would be something like this:

ALTER PROCEDURE [dbo].[spImport852Daily]
	@fName nvarchar(100)
AS
BEGIN
	SET NOCOUNT ON;

DECLARE @sql VARCHAR(4000)

SET @sql = 'BULK INSERT dbo.tbl852Daily FROM ''' + @fName + ''' WITH ( FIELDTERMINATOR = '','', ROWTERMINATOR = ''\n'' )'

EXEC(@sql)
END

Open in new window

0
 
LVL 2

Author Closing Comment

by:prosit
ID: 40584602
Quick and dirty works, thank you so much...
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

744 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

11 Experts available now in Live!

Get 1:1 Help Now