By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,288 Members | 1,695 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,288 IT Pros & Developers. It's quick & easy.

Bulk Insert - Dynamic Errorfile Filename

P: 2
I am trying to dynamically create the errorfile name with the date the error occured. The error occurs at ERRORFILE = @errorfilename

Is this possible?

Here is the script I have so far...

-------------------------------------------------------------------------------------------------------------------

USE Temp

GO

DECLARE @errorfilename text

SET @errorfilename = 'c:\rx_data_repos_ERROR_' + CONVERT(text, GETDATE())

BULK INSERT downloaded
FROM 'c:\rx_data_repos.txt'
WITH
(
FIELDTERMINATOR = '|',
ROWTERMINATOR = '\n',
CHECK_CONSTRAINTS,
FIRE_TRIGGERS,
TABLOCK,
ERRORFILE = @errorfilename)
)
Mar 1 '08 #1
Share this Question
Share on Google+
2 Replies


ck9663
Expert 2.5K+
P: 2,878
I am trying to dynamically create the errorfile name with the date the error occured. The error occurs at ERRORFILE = @errorfilename

Is this possible?

Here is the script I have so far...

-------------------------------------------------------------------------------------------------------------------

USE Temp

GO

DECLARE @errorfilename text

SET @errorfilename = 'c:\rx_data_repos_ERROR_' + CONVERT(text, GETDATE())

BULK INSERT downloaded
FROM 'c:\rx_data_repos.txt'
WITH
(
FIELDTERMINATOR = '|',
ROWTERMINATOR = '\n',
CHECK_CONSTRAINTS,
FIRE_TRIGGERS,
TABLOCK,
ERRORFILE = @errorfilename)
)

You might need to create a dynamic t-sql for this one

declare @sqlstmt varchar(150)

set @sqlstmt = 'BULK INSERT downloaded
FROM ''c:\rx_data_repos.txt''
WITH
(
FIELDTERMINATOR = ''|'',
ROWTERMINATOR = ''\n'',
CHECK_CONSTRAINTS,
FIRE_TRIGGERS,
TABLOCK,
ERRORFILE = ' + @errorfilename + ')'

exec @sqlstmt

I did not check if you BULK INSERT syntax is correct. But I hope you got the idea of what am trying to suggest.

-- CK
Mar 2 '08 #2

P: 2
Thanks CK,

This is what I ended up with.

DECLARE @sqlstmt varchar(500),
@errorfilename varchar(150),
@TodayDate as varchar(40),
@TodayHour as varchar(40),
@TodayMinu as varchar(40)

SELECT @TodayDate = CONVERT(varchar(10), GETDATE(), 112)
SELECT @TodayHour = DATEPART(hh,GETDATE())
SELECT @TodayMinu = DATEPART(mi,GETDATE())

SET @errorfilename = 'c:\rx_data_repos_Error_' +
@TodayDate +
'_' +
@TodayHour +
@TodayMinu +
'.txt'

SET @sqlstmt = 'BULK INSERT tmp_Rx_Data_Repos
FROM ''c:\rx_data_repos.txt''
WITH (FIELDTERMINATOR = ''|'',
ROWTERMINATOR = ''\n'',
ERRORFILE = ' + '''' +@errorfilename + '''' +')'

EXECUTE (@sqlstmt)
Mar 2 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.