2008-10-27 4 views
1

저는 SQL Server 2000을 실행 중이며 필요에 따라 구문 분석하여 위키 문서에 넣을 수 있도록 모든 DTS 개체에서 SQL 문을 내 보내야합니다.DTS 개체에서 SQL 문을 내보내려면 어떻게해야합니까?

할 방법이 있습니까?

어쩌면 각 DTS 개체를 프로세스 이름과 파일 헤더로 추출한 날짜가있는 파일 이름으로 개체 이름을 가진 텍스트 파일로 덤프 할 수 있습니다.

감사합니다.

답변

1

Visual Basic 코드로 저장된 DTS 패키지의 작업에서 SQL을 덤프하는 Python 2.6 스크립트 (쉽게 파이썬 2.5에 이식 가능)가 있습니다.

DTS 패키지를 VB 파일로 저장하는 방법은 ConcernedOfTunbridgeWells '게시물을 참조하십시오. VB 파일을 저장 한 후에이 함수를 실행하십시오. 패키지의 코드가 들어있는 VB 파일과 동일한 위치에 폴더를 만들고 찾은 SQL 코드를 덤프합니다. SQL의 출력이 CSV 파일 (outExt 참조) 또는 "Execute SQL Task"태스크에서 가져온 것으로 가정하고 출력 파일 또는 SQL 태스크 다음에 SQL 쿼리의 이름을 지정합니다. 패키지가 다른 작업을 수행하지 않으면 유용한 솔루션입니다.

마음에 들면이 코드를 깨끗하게 청소하십시오.

# from __future__ import with_statement # Version 2.5 requires this. 
import os, re 

def dump_sql(infile, outExt=r'csv'): 
    """Parse a DTS package saved as a .bas file, and dump the SQL code. 

    Pull out the SQL code and the filename for each task. This process 
    depends on the way that DTS saves packages as VB modules. 

    Keyword arguments: 
    infile - The .bas file defining a DTS package. 
    outExt - The extension (without a period) of the files exported by the 
      data pumps in the DTS package. These are used to rename the 
      extracted SQL scripts. If an extract file does not use this 
      extension, then the whole name of the extract file is used to 
      name the SQL script. (default: csv) 

    The function produces a folder in the same folder that contains the 
    .bas file. It's named like this: if the .bas file is "DTS package.bas", 
    then the directory will be named "DTS package_SQL". The SQL scripts are 
    stored in this folder. 

    """ 
    #Declare all of the RE's used in the script here. 
    basExtRE = re.compile(r'\.bas$', re.IGNORECASE) 
    outExtRE = re.compile(r'\.' + outExt + r'$', re.IGNORECASE) 
    startTaskRE = re.compile(r'Set oCustomTask(\d+) = oTask.CustomTask') 
    startSqlRE = re.compile(
     r'oCustomTask(\d+)\.(?:Source)?SQLStatement = "(.*)"(& vbCrLf)?') 
    nextSqlRE = re.compile(
     r'oCustomTask(\d+)\.(?:Source)?SQLStatement = oCustomTask\1\.' 
     r'(?:Source)?SQLStatement & "(.*)"(& vbCrLf)?') 
    filenameRE = re.compile(
     r'oCustomTask(\d+)\.DestinationObjectName = "(.*)"') 
    descripRE = re.compile(r'oCustomTask(\d+)\.Description = "(.*)"') 
    invalidCharsRE = re.compile(r'[][+/*?<>,.;:"=\\|]') 

    #Read the file 
    with open(infile, 'r') as f: 

     #Produce the directory for the SQL scripts. 
     outfolder = '%s_SQL\\' % basExtRE.sub('', infile) 
     if not os.path.exists(outfolder): 
      os.makedirs(outfolder) 

     taskNum = -1 
     outfile = '' 
     sql = [] 

     for line in f: 
      line = line.rstrip().lstrip() 

      if taskNum == -1: 
       #Seek the beginning of a task. 
       m = startTaskRE.match(line) 
       if m is not None: 
        taskNum = int(m.group(1)) 
      elif line == '' and outfile != '': 
       #Save the SQL code to a file. 
       if sql: 
        if os.path.exists(outfile): 
         os.unlink(outfile) 
        with open(outfile, 'w') as fw: 
         fw.writelines(["%s" % sqlQ for sqlQ in sql]) 
        print "%2d - %s" % (taskNum, outfile) 
       else: 
        print "%2d > No SQL (%s)" % (
         taskNum, os.path.basename(outfile)) 
       sql = [] 
       outfile = '' 
       taskNum = -1 
      else: 
       #Acquire SQL code and filename 
       m = startSqlRE.match(line) 
       if m: 
        #Start assembling the SQL query. 
        tnum, sqlQ, lf = m.groups() 
        assert int(tnum) == taskNum 
        sql = [sqlQ.replace('""', '"') 
          + ('\n' if lf is not None else '')] 
        continue 
       m = nextSqlRE.match(line) 
       if m: 
        #Continue assembling the SQL query 
        tnum, sqlQ, lf = m.groups() 
        assert int(tnum) == taskNum 
        sql.append(sqlQ.replace('""', '"') 
           + ('\n' if lf is not None else '')) 
        continue 
       m = descripRE.match(line) 
       if m: 
        # Get a SQL output filename from the task's 
        # description. This always appears near the top of the 
        # task's definition. 
        tnum, outfile = m.groups() 
        assert int(tnum) == taskNum 
        outfile = invalidCharsRE.sub('_', outfile) 
        outfile = "%s%s.sql" % (outfolder, outfile) 
        continue 
       m = filenameRE.match(line) 
       if m: 
        # Get a SQL output filename from the task's output 
        # filename. This always appears near the bottom of the 
        # task's definition, so we overwrite the description if 
        # one was found earlier. 
        tnum, outfile = m.groups() 
        assert int(tnum) == taskNum 
        outfile = os.path.basename(outfile) 
        outfile = outExtRE.sub('', outfile) 
        outfile = "%s%s.sql" % (outfolder, outfile) 
        continue 
    print 'Done.' 
2

DTS 패키지에 대한 개체 모델이있는 API가 있습니다. 이것을 통해 SQL 텍스트를 얻을 수 있습니다. Books on Line docs는 어느 정도 이것을 설명합니다 Here. 개체 모델 사용에 대한 예제를 Saving the DTS package to a Visual BASIC file으로 가져올 수 있으며 VB 파일이 개체 모델에 수행하는 내용을 볼 수 있습니다.

1

일부 작업을 저장하고 몇 달러를 지불해도 괜찮 으면 DTS 패키지를 완전히 문서화하는 tool이 있습니다. XML로 출력되기 때문에 SQL 문을 얻는 것이 상대적으로 쉽습니다.

+0

확실히 합리적인 가격 .... 너무 나쁘다. 시장은 이제 2008 년과 함께 빠르게 줄어들고 있습니다. – Keng

관련 문제