Link to home
Start Free TrialLog in
Avatar of Zahid Ahamed
Zahid AhamedFlag for United States of America

asked on

SSIS Package SQL Task failing

I have SSIS package and it runs every hour. This package import data from csv into database table. From march 2017 this package has been failing. What i see there is a SQL task step which is failing but before it was succeeded. Here is the below code:-- I put the four *** sign where it is failing.


declare @toparse nvarchar(max)
declare @strlength int
declare @separator_location int
declare @instance nchar(10)

declare @instance_url nvarchar(max)

declare @hostname nvarchar(50)
declare @strIn nvarchar(2000)
declare @separator nchar(2) = ' '
declare @inner_separator nchar(1) = '|'

DECLARE @item nvarchar(1000)  -- assume maximun item length 1000 char
DECLARE @len int, @sepPos1 int,  @sepPos2 int

-- truncate table staging_oracle_version
--declare @truncate varchar(200)
--set @truncate = 'truncate table staging_oracle_version'
--exec (@truncate)

-- get oracle instances to load
declare  oracleinstance_csr cursor for
select certname
      , value
from puppetdb_certname_facts
where fact like 'oracle_instance_url'
   and value IS NOT NULL
   and value !=''
  and value != '""'

open oracleinstance_csr

fetch next from oracleinstance_csr into @hostname, @strIn

--select @hostname, @strIn

while @@FETCH_STATUS = 0
begin

  SET @strIn = REPLACE(@strIn, char(10), @separator)  -- Replace all the LF (char(10)) from the string and replace with defined separator [ the code for CR is char(13)]
  Set @sepPos1 = 1
  set @sepPos2 = charindex(@separator, @strIn)

  SET @strin = @strIn + @separator

  IF @sepPos2 = 0  -- nothing to parse here
  BEGIN
     SET @item = @strIn
     SET @toparse =  ( RTRIM(LTRIM(@item)))
     
       select @strlength = LEN(@toparse)
       select @separator_location = charindex(@inner_separator, @toparse)

      
****       select @instance = LEFT(@toparse,(@separator_location-1))
       select @instance_url = RIGHT(@toparse, (@strlength-@separator_location))

        update oracleinstances
        set instance_url  = @instance_url
        where instanceName = @instance
          and hostname = @hostname
       -- select @instance, @hostname, @version
      
  END
ELSE
  BEGIN
    WHILE @sepPos2 > 0
    BEGIN
        -- get parse through the values
      SET @item = substring(@strIn, @sepPos1,@sepPos2-1)
      SET @toparse =  ( RTRIM(LTRIM(@item)))
     
        select @strlength = LEN(@toparse)
        select @separator_location = charindex(@inner_separator, @toparse)

        select @instance = LEFT(@toparse,(@separator_location-1))
        select @instance_url = RIGHT(@toparse, (@strlength-@separator_location))

        
        update oracleinstances
        set instance_url = @instance_url
        where instanceName = @instance
          and hostname = @hostname

      SET @sepPos1 = @sepPos1 + @sepPos2
      set @sepPos2 = charindex(@separator, substring(@strIn, @sepPos1,100))
    END
 

  END

  fetch next from oracleinstance_csr into @hostname, @strIn
           
END

close oracleinstance_csr
deallocate oracleinstance_csr

Error:

Msg 537, Level 16, State 3, Line 56
Invalid length parameter passed to the LEFT or SUBSTRING function.
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

I don't see any obvious errors.  Have you coped this code, pasted into SSMS, and executed there?
I suspect that your code is fine.  Try using another tool to validate the CSV file.

Possibilities:

  • If small enough, try the Excel Text Import wizard.
  • Add debug code to identify if a particular record in the CSV file is the culprit
Here is a tool that I looked at a long time ago that does validation on CSV files.

Flat File Checker
Avatar of Zahid Ahamed

ASKER

Hi Jeff,

I found that culprit data.


HostName     Instance_Url

ServerA              GISDEV|http://xxx.net:xxx
ServerB              DEMPROD|err:login-fail
ServerC              REVHUBDV|http://xxx.net:xxx PATEST|http://xxx.net:xxx

If you see very closely in the third record The ServerC has instance url where (REVHUBDV|http://xxx.net:xxx PATEST|http://xxx.net:xxx)

has space that's why i got the error because it is not working select @instance = LEFT(@toparse,(@separator_location-1)) . Now how could i avoid this error. or should i let my team know fix their csv file



But if put if @separator_location >0 then query works fine. Here i am confuse in order to avoid this error should i put if  if @separator_location >0 or let them know fix their value in the csv file. because this query generates reports.
ASKER CERTIFIED SOLUTION
Avatar of Jeff Darling
Jeff Darling
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Jeff