views:

182

answers:

1

I have to insert new records in a database every day from a text file ( tab delimited). I'm trying to make this into a stored procedure with a parameter for the file to read data from.

CREATE PROCEDURE dbo.UpdateTable

    @FilePath

    BULK INSERT TMP_UPTable 
    FROM @FilePath
    WITH 
    ( 
        FIRSTROW = 2, 
        MAXERRORS = 0, 
        FIELDTERMINATOR = '\t', 
        ROWTERMINATOR = '\n' 
    )

    RETURN

Then i would call this stored procedure from my code (C#) specifying the file to insert.

This is obviously not working, so how can i do it ?

Just to be clear the problem here is that i can't pass the parameter @FilePath to the FROM clause, or at least i don't know how.

+1  A: 

Sorry, I misunderstood. You need to create the SQL statement dynamically and then execute it:

    CREATE procedure dbo.UpdateTable

    @FilePath varchar(max)
AS

 declare @sql varchar(max)
 declare @parameters varchar(100)
 set @parameters = 'FIRSTROW = 2, MAXERRORS = 0, FIELDTERMINATOR = ''\\t'', ROWTERMINATOR = ''\\n'' '
 SET @SQL = 'BULK INSERT TMP_UPTable FROM ' + @FilePath + @parameters

EXEC (@SQL)

RETURN
simon831
this is exactly what i was looking for.i think i have some other kind of problem now : "The bulk load failed. The column is too long in the data file for row 1, column 1. Verify that the field terminator and row terminator are specified correctly."
Iulian
Maybe some adjustment of the "set @parameters =..." is needed.
simon831
i am looking at my text file - the data is delimited by tabs (Hex $9) but at the end of the line i have the same thing.Maybe the delimiters are not escaped properly, i've tried ''\t'' and i get the same error
Iulian
the problem is definitively in the txt file, thanks for your help
Iulian