bulk insert error

  • hi, iam trying to impor a csv file but i get this err:

    requirement '@statement' type 'ntext/nchar/nvarchar'.

    any help plz?

    this is the code:

    CREATE PROCEDURE [wr_importar_detalles] AS

    declare @cad as varchar(8000)

    declare @fichero as varchar(200)

    set @Fichero='prueba.csv'

    set @cad='BULK INSERT webmisco.dbo.wr_primero FROM ''d:\transferencia$\web\reports\'+@Fichero+' with (DATAFILETYPE = ''char'', FIELDTERMINATOR = '','',CODEPAGE=''850'')'

    print @cad

    execute sp_executesql @cad

    Go

  • sp_executesql requires that the sql statement to be executed is of datatype 'ntext/nchar/nvarchar'

    Changes in Bold the Capitol 'N' tells sql to convert string to binary, but the conversion is implicit if you do not put the N there

    CREATE PROCEDURE [wr_importar_detalles] AS

    declare @cad as nvarchar(4000)

    declare @fichero as varchar(200)

    set @Fichero='prueba.csv'

    set @cad=N'BULK INSERT webmisco.dbo.wr_primero FROM ''d:\transferencia$\web\reports\'+@Fichero+' with (DATAFILETYPE = ''char'', FIELDTERMINATOR = '','',CODEPAGE=''850'')'

    print @cad

    execute sp_executesql @cad

     

  • The BULK INSERT statement will implicitly convert varchar to nvarchar if necessary.

    The problem is you're missing a single quote after the filename.

    Change this:

    set @cad='BULK INSERT webmisco.dbo.wr_primero FROM ''d:\transferencia$\web\reports\'+@Fichero+' with (DATAFILETYPE = ''char'', FIELDTERMINATOR = '','',CODEPAGE=''850'')'

    To this:

    set @cad='BULK INSERT webmisco.dbo.wr_primero FROM ''d:\transferencia$\web\reports\' + @Fichero + ''' with (DATAFILETYPE = ''char'', FIELDTERMINATOR = '','',CODEPAGE=''850'')'

  • I think it's actually a double quote that's missing. Try this:-

    set @cad='BULK INSERT webmisco.dbo.wr_primero FROM ''d:\transferencia$\web\reports\'+@Fichero+' with (DATAFILETYPE = ''char'', FIELDTERMINATOR = '','',CODEPAGE=''850'')'

    should be

    set @cad='BULK INSERT webmisco.dbo.wr_primero FROM ''d:\transferencia$\web\reports\'+@Fichero+'" with (DATAFILETYPE = ''char'', FIELDTERMINATOR = '','',CODEPAGE=''850'')'

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply