0

I made this script to process batch .sql source files to define SP (as part of migration). It works fine but I can' catch any exception, when any source file is bad and given sp can not be created. I tried touse try/catch and looks like cmdshell doesn't care about result, even output for this file is different then for good files. do you know how else I can catch bad files ??

Thanks M

While @cc > @ccRun
Begin
set @ccRun = @ccRun + 1;
set @shellArg = (select 'sqlcmd -S '+ etc...+ @file....);
begin try
  EXEC xp_cmdshell @shellArg  -- to create sp
end try
begin catch                   -- try to catch bad files,??
  select 'Error_____ for ' + @file
end catch

End

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mario Trento
  • 513
  • 1
  • 4
  • 14

1 Answers1

1

xp_cmdshell is not very "try-catch friendly". You should try checking return value like below.

WHILE @cc > @ccRun
BEGIN
    SET @ccRun = @ccRun + 1;
    SET @shellArg = (select 'sqlcmd -S '+ etc...+ @file....);

    DECLARE @returnValue INT

    EXEC @returnValue = xp_cmdshell @shellArg

    IF @returnValue <> 0 
    BEGIN
        SELECT 'Error_____ for ' + @file
    END

END
pkmiec
  • 2,594
  • 18
  • 16
  • Thansk, Pk! @ReturnValue is not system one ? If so I'll check if sp exists in catalog, as I don't want to touch this code. – Mario Trento Jul 19 '15 at 23:14