【问题标题】:How do I export the SQL statement from a DTS object?如何从 DTS 对象导出 SQL 语句?
【发布时间】:2008-10-27 17:25:15
【问题描述】:

我正在运行 SQL Server 2000,我需要从所有 DTS 对象中导出 SQL 语句,以便在需要时对其进行解析并放入 wiki 文档中。

有没有办法做到这一点?

可能会将每个 DTS 对象转储到一个文本文件中,其中对象名称作为文件名,进程名称和提取日期作为文件头。

谢谢。

【问题讨论】:

    标签: sql sql-server dts


    【解决方案1】:

    有一个用于 DTS 包的带有对象模型的 API。您可以通过此获取 SQL 文本。 Books on Line 文档在一定程度上描述了这一点Here. 您可以通过Saving the DTS package to a Visual BASIC file 获取对象模型使用示例,并查看 VB 文件对对象模型的作用。

    【讨论】:

      【解决方案2】:

      我有一个Python 2.6 脚本(很容易移植到 Python 2.5),它从已保存为 Visual Basic 代码的 DTS 包中的任务转储 SQL。

      请参阅 ConcernedOfTunbridgeWells 的帖子,了解如何将 DTS 包保存到 VB 文件。保存 VB 文件后,在其上运行此函数。它将在与包含包中代码的 VB 文件相同的位置创建一个文件夹,并将转储它找到的 SQL 代码。它假定 SQL 的输出进入 CSV 文件(请参阅outExt 参数)或来自“执行 SQL 任务”任务,并在输出文件或 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.'
      

      【讨论】:

        【解决方案3】:

        如果您想节省一些工作并且不介意花几块钱,那么有一个tool 可以完整记录您的 DTS 包。它也输出为XML,因此获得这些SQL语句应该相对容易。

        【讨论】:

        • 当然是一个合理的价格......太糟糕了,他们的市场现在正在迅速萎缩,因为 2008 年结束了。
        猜你喜欢
        • 2015-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-20
        相关资源
        最近更新 更多