How to pass String to Bulk instead of file?

I used the bulk insert command to convert the CSV file table. Currently, I saved the CSV file as VarBinary value in sql server. Now I can get the data from the Varbinary file by specifying it in Varchar using the CAST and CONVERT functions. But now I have a problem, I cannot convert this Varchar string containing csv content to a table using bulk insert. Can someone help me My sample code is below:

--@String contains varchar value of CSV file content.  
SET @sql = 'BULK INSERT TempCsv
FROM  ''' + @String  + '''
WITH
(
    FIRSTROW = 2,
    FIELDTERMINATOR = '','',  
    ROWTERMINATOR = ''\n'',   
    TABLOCK
)'

Please help me. Is there a way or alternative to insert data from a csv row into a table.

+1
source share
1 answer

EDIT: allow multiple char delimiters

This is how I solved it. It includes:

  • - (xftSplit) (char (10))
  • (fSubstrNth) n-
  • (fPatIndexMulti) n-
  • () Right
  • , - , SQL ( , SELECT )

, :

xftSplit

-- =============================================
-- Author:      Bernardo A. Dal Corno
-- Create date: 15/07/2014
-- Description: Quebra valores a partir de caracteres e retorna lista em tabela
-- =============================================
CREATE FUNCTION [dbo].[xftSplit] 
(
    @Texto varchar(max),
    @Splitter varchar(3)
)
RETURNS 
@Lista TABLE 
(
    ValoresQuebrados varchar(8000)
)
AS
BEGIN
DECLARE @Pos Smallint

  While len(@Texto)>0 BEGIN
    SET @Pos = Patindex('%'+@Splitter+'%',@Texto)

    IF @Pos > 0 BEGIN
      INSERT INTO @Lista
      SELECT left(@Texto, @Pos-1)

      SET @Texto = right(@Texto, len(@Texto)-@Pos)
    END
    ELSE BEGIN
      INSERT INTO @Lista
      SELECT @Texto

      SET @Texto = ''
    END
  End

  RETURN 
END

fSubstrNth

-- =============================================
-- Author:      Bernardo A. Dal Corno
-- Create date: 18/07/2017
-- Description: substring com 2 PatIndex limitando inicio e fim
-- =============================================
CREATE FUNCTION fSubstrNth
(
  @Text varchar(max),
  @Sep varchar(3),
  @N int --Nth campo
)
RETURNS varchar(max)
AS
BEGIN
  DECLARE @Result varchar(max)

  IF @N<1 RETURN ''
  IF @N=1
    SET @Result = substring(@Text, 1, dbo.fPatIndexMulti(@Sep,@Text,1)-1)
  ELSE
    SET @Result = substring(@Text, dbo.fPatIndexMulti(@Sep,@Text,@N-1)+LEN(@Sep), CASE WHEN dbo.fPatIndexMulti(@Sep,@Text,@N)>0 THEN dbo.fPatIndexMulti(@Sep,@Text,@N)-dbo.fPatIndexMulti(@Sep,@Text,@N-1)-LEN(@Sep) ELSE LEN(@Text)+1 END)

  RETURN @Result
END

fPatIndexMulti

-- =============================================
-- Author:      Bernardo A. Dal Corno
-- Create date: 17/07/2017
-- Description: recursive patIndex
-- =============================================
CREATE FUNCTION [dbo].[fPatIndexMulti]
(
  @Find varchar(max),
  @In varchar(max),
  @N tinyint
)
RETURNS int
AS
BEGIN
  DECLARE @lenFind int, @Result int, @Texto varchar(max), @index int
  DECLARE @i tinyint=1

  SET @lenFind = LEN(@Find)-1
  SET @Result = 0
  SET @Texto = @In
  WHILE (@i <= @N) BEGIN
    SET @index = patindex('%'+@Find+'%',@Texto)
      IF @index = 0 RETURN 0
    SET @Result = @Result + @index
    SET @Texto = dbo.xRight(@Texto, (@index + @lenFind)*-1)

    SET @i = @i + 1
  END
  SET @Result = @Result + @lenFind*(@i-2)

  RETURN @Result
END

xRight

-- =============================================
-- Author:      Bernardo A. Dal Corno
-- Create date: 06/01/2015
-- Description: Right inverso (para nros < 0)
-- =============================================
CREATE FUNCTION [dbo].[xRight] 
(
  @Texto varchar(8000),
  @Qntd int
)
RETURNS varchar(8000)
AS
BEGIN
  DECLARE @Result varchar(8000)

  IF (Len(@Texto) = 0) OR (@Qntd = 0)
    SET @Result = ''
  ELSE IF (@Qntd > 0) 
      SET @Result = Right(@Texto, @Qntd)
    ELSE IF (@Qntd < 0)
    SET @Result = Right(@Texto, Len(@Texto) + @Qntd)

  RETURN @Result
END

SELECT 
     acolumn = 'any value',
     field1 = dbo.fSubstrNth(line,',',1),
     field2 = dbo.fSubstrNth(line,',',2),
     anothercolumn = 'set your query as you would normally do',
     field3 = (CASE dbo.fSubstrNth(line,',',3) WHEN 'C' THEN 1 ELSE 0 END)
FROM (
  SELECT line = ValoresQuebrados FROM dbo.xftSplit(@StringVariable, char(10))
) lines

, :

  • fSubstrNth n-
  • xftSplit , , ( ) char(10) \n, -
  • . , , tabled, view .. ,
  • , temp, , , ( , ).
0

All Articles