ms sql 2015

I am bulk loading a file nightly and want to rename the file as part of the store procedure.
In stored procedure do bulk load and then rename the file name so that you can tell the file has been bulk loaded.
Bryon HuffmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ITSysTechSenior Systems AdministratorCommented:
Are you using MSSQL 2016 the heading says 2015?  thanks
0
Bryon HuffmanAuthor Commented:
Sorry ms sql 2008
0
Pawan KumarDatabase ExpertCommented:
Please find the complete solution below -

CREATE TABLE BulkExamples
(
	ID INT,
	Name VARCHAR(40)
)
GO

BULK
INSERT BulkExamples
FROM 'c:\pawan\tblName_March_2015.txt'
WITH
(
	FIELDTERMINATOR = '	',
	ROWTERMINATOR = '\n'
)
GO

SELECT * FROM BulkExamples

Open in new window


OUTPUT

/*------------------------
SELECT * FROM BulkExamples
------------------------*/
ID          Name
----------- ----------------------------------------
1           Pawan
2           Sharlee
3           Jason
1           Pawan
2           Sharlee
3           Jason

(6 row(s) affected)

Open in new window


For rename the file you can download the code from http://www.itprotoday.com/t-sql/rename-files-or-directories-fly-t-sql-scripts

Execution -
exec sp_ReplaceFileOrDirNames @pathToObject= 'c:\pawan',
                              @oldName='tblName_March_2015.txt',
                              @newName='tblName_March_2015_DONE_BULK.Txt'

Open in new window


For renaming if the above solution does not work we can also use xp_cmdshell.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Vitor MontalvãoMSSQL Senior EngineerCommented:
You shouldn't use SQL Server to perform operation system tasks as renaming a file.
I will recommend you a solution that runs from the Windows command prompt. You can use the Windows Task Scheduler to schedule the following script to run it daily:
bcp databaseName.dbo.FileName IN C:\importFiles\FileName.bcp -c -T
ren C:\importFiles\FileName.bcp C:\importFiles\NewFileName.bcp

Open in new window

BCP is a utility that performs the Bulk Insert and ren is the rename command.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Bryon, is your issue solved?
0
Pawan KumarDatabase ExpertCommented:
Hi Bryon,
Did my comment worked for you ?

Thanks
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
2 different solutions provided.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.