2017-12-13 17 views
1

스크립트 및 SQL 작업이 포함 된 내 ssis 패키지 내에 for 루프 컨테이너가 있습니다.SSIS For Loop 작업 중지

enter image description here

나는 3 개 변수를 가지고있다.

source.string = this is folder location 
file.string = i have used wildcard = *.csv 
exist.int = defaulted to 0 

내가 @로 설정 innitexpression 값이 = 1 을 존재 @로 설정 evalexpression 값이 나는 소스 변수에서와 file.string 경우보고를 설정 한 스크립트에서 = 1

을 존재 변수가 존재하면 변수가 1로 설정됩니다.

문제는 단지 루프입니다. 파일이 없으면 반복해야합니다. 위선자가 변수를 와일드 카드 *로 변경하기 전에 어떻게 잘못했는지 확인하십시오.

와일드 카드가 아닌 파일 이름이 포함 된 다른 변수를 사용하여 테스트 한 결과 올바르게 작동합니다. 파일 이름과 확장자 뒤에 와일드 카드를 찾을 때. 왜 이런거야? 와일드 카드 변수를 통과 할 수 있습니까? 내가이 개 다른 솔루션을 만들어 위의

내 스크립트 작업은 의견을

 public void Main() 
     { 
      // TODO: Add your code here 
      string Filepath = Dts.Variables["User::Source"].Value.ToString() 
+ Dts.Variables["User::file"].Value.ToString(); 
      if (
       File.Exists(Filepath)) 
      { 
       Dts.Variables["User::Exists"].Value = 1; 
      } 

      /// MessageBox.Show (Filepath); 
      /// MessageBox.Show(Dts.Variables["Exists"].Value.ToString()); 
      Dts.TaskResult = (int)ScriptResults.Success; 
     } 

     #region ScriptResults declaration 
     /// <summary> 
     /// This enum provides a convenient shorthand within the scope of this class for setting the 
     /// result of the script. 
     /// 
     /// This code was generated automatically. 
     /// </summary> 
     enum ScriptResults 
     { 
      Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success, 
      Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure 
     }; 
     #endregion 

    } 
} 
+0

는 당신이 우리 스크립트 작업 스크립트를 게재 할 수 있습니까? – plaidDK

+0

@plaidDK가 질문에 스크립트 작업을 추가했습니다 – sql2015

+0

경로가 어떻게 생겼습니까? – plaidDK

답변

3

을 기반으로합니다. 지금 당신을위한 해결책은 아니오 일 것입니다. 2

  1. 이 사람은 당신의 경로에 여러 파일을 기반으로 특정 파일을 검색 할 수 있습니다. 몇 가지 조정이 필요하지만 특정 파일이 와일드 카드로 있는지 확인하려는 경우 사용할 수 있습니다.

  2. 와일드 카드 파일이 있으면이 항목은 true로 평가됩니다.

C# 코드 1

Using System.IO: 

string Filepath = Dts.Variables["User::Source"].Value.ToString(); 
      string WildCard = Dts.Variables["User::file"].Value.ToString(); // In Text form @"*.txt"; 
      string fullpath = Filepath + WildCard; 

      //With for loop 
      string txtFile = null; 
      // Gets all files with wildcard 
      string[] allfiles = Directory.GetFiles(Filepath, WildCard); 

      //Loop through all files and set the filename in txtFile. Do whatever you want here 
      foreach(string fileName in allfiles) 
      { 
       //Check if a file contains something, it could be a prefixed name you only want 
       if(fileName.Contains("txt")) 
       { 
        txtFile = fileName; 
        if(File.Exists(txtFile)) 
        { 
         Dts.Variables["User::Exists"].Value = 1; 
        } 
       } 
      } 

C# 코드 2

Using System.IO; 
Using System.Linq; 

string Filepath = Dts.Variables["User::Source"].Value.ToString(); 
      string WildCard = Dts.Variables["User::file"].Value.ToString(); //In text form "*.txt"; 
      string fullpath = Filepath + WildCard; 

      //With bool 
      bool exists = Directory.EnumerateFiles(Filepath, WildCard).Any(); 

      if(exists == true) 
      { 
       Dts.Variables["User::Exists"].Value = 1; 
      } 


      MessageBox.Show (Filepath); 
      MessageBox.Show(Dts.Variables["Exists"].Value.ToString()); 
+0

대단히 감사합니다. 파일 유형 와일드 카드를 선언했습니다. 코드를 호출하는 대신 변수를 호출 하시겠습니까? SSIS 스크립트 작업이 와일드 카드 변수를 좋아하지 않으므로 광산이 작동하지 않습니까? – sql2015

+0

@ sql2015 네, 변수로 작업하고있었습니다. 난 그냥 내 테스트 케이스에서 그들을 만들고 싶지 않아. 그것을 하드 코딩하는 것이 더 빠릅니다. 당신은 단지 당신의 변수로 그들을 바꿀 수 있습니다 :)하지만 내가 그랬듯이 그것들을 분리하여 보관하는 것을 잊지 마십시오. 내 대답을 변수 값으로 업데이트하십시오. – plaidDK