使用T-SQL获取SSIS包名称,作业名称和描述

时间:2017-01-05 16:17:03

标签: sql-server tsql

我正在尝试从SQL Server中获取以下信息:

  • 所有SSIS职位代理名称。
  • SSIS包名称。
  • [可选但很高兴] SSIS工作代理说明。

使用下表,我可以毫无问题地提取所有这3条信息:

  • msdb.dbo.sysjobs为我提供了工作名称和工作描述。
  • msdb.dbo.sysssispackages为我提供了SSIS包名称。

不幸的是,我无法找到这两个表之间的相关性,以便将所有3条信息放在一起。

有什么想法吗?谢谢!

3 个答案:

答案 0 :(得分:4)

试试这个,

select 
    --Job Information
     a.job_id
    ,a.name
    ,a.description
    --SSIS package Information
    ,b.name
    ,b.id
    ,b.description
    --Job steps Information
    ,js.step_id
    ,js.step_name
    ,js.subsystem
    ,js.command

 from  msdb.dbo.sysjobs  a 
Left Join msdb.dbo.sysjobsteps js on js.job_id=a.job_id 
left join msdb.dbo.sysssispackages b
on a.name=b.name

答案 1 :(得分:3)

我还搜索了一个解决方案,这对我有用:

declare @MyJobTable table 
(
JobName nvarchar(255)
,StepName nvarchar(255)
,StepID int
,SSIDName nvarchar(255)
,StepCommand nvarchar(1024)
)

declare @MyCursor as cursor
declare @MyName as nvarchar(255)

set @MyCursor = CURSOR for select name from msdb.dbo.sysssispackages

open @mycursor
fetch next from @MyCursor into @MyName

while @@FETCH_STATUS = 0
begin 

insert into @MyJobTable
(JobName,StepName,StepID,SSIDName,StepCommand)
(select
jobs.name as JobName
,steps.step_name as StepName
,steps.step_id as StepID
,@MyName as SSIDName
,steps.command as StepCommand
from msdb.dbo.sysjobs as jobs
join msdb.dbo.sysjobsteps as steps
on jobs.job_id = steps.job_id
where steps.subsystem = 'SSIS'
and steps.command like '%'+@MyName+'%')
fetch next from @MyCursor into @MyName

end

select * from @MyJobTable
order by JobName,StepID

此致 斯蒂芬

答案 2 :(得分:3)

这似乎是Microsoft的一个明显漏洞-如果将PackageId存储在msdb.dbo.sysjobsteps表中,而不是必须解析Command列中的信息,那就更好了。正如OP正确指出的那样,当程序包名称相同时,这可能会出现问题。无论如何,我一直在致力于提供以下内容的查询:

  1. 职位名称
  2. 步骤名称
  3. 打包文件夹路径

-获取作业指向的程序包的文件夹路径。这把所有东西都聚集在一起。

;WITH CTE1 AS (SELECT J.job_id
                ,JobName = J.name
                ,JS.step_id, JS.step_name, JS.command
                ,StartIndex = CASE WHEN JS.command LIKE '/DTS%' OR JS.command LIKE '/SQL%' OR JS.command LIKE '/ISSERVER%' THEN CHARINDEX('\',JS.command, CHARINDEX('\',JS.command) + 1)
                                    WHEN JS.command LIKE '/SERVER%' THEN CHARINDEX('"', JS.Command, CHARINDEX(' ',command, CHARINDEX(' ',command) + 1) + 1) + 1
                                    ELSE 0
                                    END
                ,EndIndex = CASE WHEN JS.command LIKE '/DTS%' OR JS.command LIKE '/SQL%'  OR JS.command LIKE '/ISSERVER%' THEN CHARINDEX('"',JS.command, CHARINDEX('\',JS.command, CHARINDEX('\',JS.command) + 1)) 
                                                                                                - CHARINDEX('\',JS.command, CHARINDEX('\',JS.command) + 1) - 1
                                    WHEN JS.command LIKE '/SERVER%' THEN CHARINDEX('"',command, CHARINDEX('"', JS.Command, CHARINDEX(' ',command, CHARINDEX(' ',command) + 1) + 1) + 1)
                                                                - CHARINDEX('"', JS.Command, CHARINDEX(' ',command, CHARINDEX(' ',command) + 1) + 1) - 1
                                    ELSE 0
                                    END
            FROM msdb.dbo.sysjobsteps JS
            INNER JOIN msdb.dbo.sysjobs J
                ON JS.job_id = J.job_id
            WHERE JS.subsystem = 'SSIS'
            )

SELECT C1.job_id, C1.JobName, C1.step_id, C1.step_name
    ,PackageFolderPath = CASE WHEN C1.command LIKE '/DTS%' OR C1.command LIKE '/ISSERVER%' THEN SUBSTRING(C1.command, C1.StartIndex, C1.EndIndex)
                            WHEN C1.command LIKE '/SQL%' THEN '\MSDB' + SUBSTRING(C1.command, C1.StartIndex, C1.EndIndex)
                            WHEN C1.command LIKE '/SERVER%' THEN '\MSDB\' + SUBSTRING(C1.command, C1.StartIndex, C1.EndIndex)
                            ELSE NULL
                            END
    , C1.command
FROM CTE1 C1
ORDER BY C1.job_id, C1.step_id