Thursday, March 1, 2007

Stored Procedure-Importing CSV Files into Table -SQL Server

Here is the Stored Procedure code for importing comma seperated CSV files to table.


CREATE PROCEDURE [dbo].[sp_ImportScript]
@filepath varchar(2000)
AS
BEGIN TRY
declare @tmpsql varchar(3000)
set @tmpsql='BULK INSERT table'
set @tmpsql=@tmpsql + ' FROM ''' + @filepath + ''''
set @tmpsql=@tmpsql + ' WITH (FIRSTROW=2,FIELDTERMINATOR ='
set @tmpsql=@tmpsql + ''','''
set @tmpsql=@tmpsql + ',ROWTERMINATOR ='
set @tmpsql=@tmpsql + ''','''
set @tmpsql=@tmpsql + ')'
EXEC(@tmpsql)
select @@rowcount
END TRY
BEGIN CATCH
DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int
SELECT @ErrMsg = ERROR_MESSAGE(),
@ErrSeverity = ERROR_SEVERITY()
RAISERROR(@ErrMsg, @ErrSeverity, 1)
END CATCH
Share:

0 comments:

Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews