Showing posts with label Stored procedure with table name as perameter. Show all posts
Showing posts with label Stored procedure with table name as perameter. Show all posts

Thursday, September 10, 2009

Stored Procedure for Import a .csv file with filename and table name as perameter

Many times there is a problem to pass a table name as a store procedure perameter, so this
procedure could help,

Store Procedure:
........................................
Create PROCEDURE [dbo].[sp_ImportCsvFile]
(
@tableName nvarchar(200),
@from nvarchar(300) -- .csv file path
)
AS
DECLARE @SQLString nvarchar(1000)
DECLARE @S2 nvarchar(1000)
SET @SQLString = 'BULK INSERT ' + @tableName + ' FROM '''+ @from
Select @S2 = CAST(@SQLString as nvarchar(1000))
EXECUTE (@S2 + ''' WITH(FIELDTERMINATOR = '','',ROWTERMINATOR = ''\n'')')

..................................................................................................
Execute the procedure:
..........................................
exec sp_ImportCsvFile 'tbl_Forex','D:\20090908_forex.csv'