列出一台服务器

时间:2016-12-20 09:57:42

标签: sql sql-server database triggers sql-server-2012

我在SO上经历了很多帖子。我发现我们可以在一个数据库中列出所有触发器。

我想要的是无论数据库我想要给定表accgrpportinfo

上的所有触发器

这是我试过的

SELECT 
    sysobjects.name AS trigger_name,
    USER_NAME(sysobjects.uid) AS trigger_owner,
    s.name AS table_schema,
    OBJECT_NAME(parent_obj) AS table_name,
    OBJECTPROPERTY( id, 'ExecIsUpdateTrigger') AS isupdate,
    OBJECTPROPERTY( id, 'ExecIsDeleteTrigger') AS isdelete,
    OBJECTPROPERTY( id, 'ExecIsInsertTrigger') AS isinsert,
    OBJECTPROPERTY( id, 'ExecIsAfterTrigger') AS isafter,
    OBJECTPROPERTY( id, 'ExecIsInsteadOfTrigger') AS isinsteadof,
    OBJECTPROPERTY(id, 'ExecIsTriggerDisabled') AS [disabled] 
FROM
    sysobjects 
INNER JOIN 
    sysusers ON sysobjects.uid = sysusers.uid 
INNER JOIN 
    sys.tables t ON sysobjects.parent_obj = t.object_id 
INNER JOIN 
    sys.schemas s ON t.schema_id = s.schema_id 
WHERE 
    sysobjects.type = 'TR' 
    AND OBJECT_NAME(parent_obj) IN ('accgrp', 'portinfo')

我无法遍历每个数据库并获取这两个表上的触发器列表。我有50到70个数据库,所以每次我必须手动完成它们。任何人都可以建议一些事情。

4 个答案:

答案 0 :(得分:2)

我的查询适用于diff db collat​​ions。如果需要,请添加更多列以供选择:

DECLARE @str        NVARCHAR(MAX)   = SPACE(0);
DECLARE @collation  NVARCHAR(MAX)   = (SELECT CONVERT(NVARCHAR(MAX), SERVERPROPERTY('COLLATION')));
DECLARE @rnd        NVARCHAR(MAX)   = NEWID();

SELECT 
     @str +=  CHAR(13) + 'SELECT [trg_name] = [name] COLLATE ' + @collation + ',  [db_name] = ' + CHAR(39) + [name] + CHAR(39) + ' FROM [' + [name] + '].[sys].[triggers] ' + CHAR(13) + ' UNION ALL'
FROM 
    [sys].[databases];

SET @str += @rnd;
SET @str =  REPLACE(@str, 'UNION ALL' + @rnd, SPACE(0));

EXEC(@str);

答案 1 :(得分:1)

Command sysusers table join and get result : 

 SELECT 
 sysobjects.name AS trigger_name 
,USER_NAME(sysobjects.uid) AS trigger_owner 
,s.name AS table_schema 
,OBJECT_NAME(parent_obj) AS table_name 
,OBJECTPROPERTY( id, 'ExecIsUpdateTrigger') AS isupdate 
,OBJECTPROPERTY( id, 'ExecIsDeleteTrigger') AS isdelete 
,OBJECTPROPERTY( id, 'ExecIsInsertTrigger') AS isinsert 
,OBJECTPROPERTY( id, 'ExecIsAfterTrigger') AS isafter 
,OBJECTPROPERTY( id, 'ExecIsInsteadOfTrigger') AS isinsteadof 
,OBJECTPROPERTY(id, 'ExecIsTriggerDisabled') AS [disabled] 
FROM sysobjects 
/*
 INNER JOIN sysusers ON sysobjects.uid = sysusers.uid 
*/  
 INNER JOIN sys.tables t ON sysobjects.parent_obj = t.object_id  
 INNER JOIN sys.schemas s ON t.schema_id = s.schema_id 
 WHERE sysobjects.type = 'TR' 

答案 2 :(得分:1)

这将给你想要的东西

DECLARE @QRY VARCHAR(MAX) ='';

SELECT @QRY =@QRY + ' select  s.name AS table_schema 
    ,OBJECT_NAME(TR.parent_id) AS table_name 
    ,OBJECTPROPERTY( SO.object_id, ''ExecIsUpdateTrigger'') AS isupdate 
    ,OBJECTPROPERTY( SO.object_id, ''ExecIsDeleteTrigger'') AS isdelete 
    ,OBJECTPROPERTY( SO.object_id, ''ExecIsInsertTrigger'') AS isinsert 
    ,OBJECTPROPERTY( SO.object_id, ''ExecIsAfterTrigger'') AS isafter 
    ,OBJECTPROPERTY( SO.object_id, ''ExecIsInsteadOfTrigger'') AS isinsteadof 
    ,OBJECTPROPERTY(SO.object_id, ''ExecIsTriggerDisabled'') AS [disabled]
FROM ['+name+'].SYS.TRIGGERS TR
INNER JOIN ['+name+'].sys.tables t     ON TR.parent_id = t.object_id 
INNER JOIN ['+name+'].sys.objects SO ON TR.object_id = SO.object_id
INNER JOIN ['+name+'].sys.schemas s     ON t.schema_id = s.schema_id 
WHERE OBJECT_NAME(parent_id) IN (''accgrp'',''portinfo'')
UNION ALL
' 
FROM SYS.DATABASES
WHERE name not IN ('master', 'model', 'msdb', 'tempdb', 'resource',
       'distribution' , 'reportserver', 'reportservertempdb','jiradb')

SELECT @QRY = SUBSTRING(@QRY,1,LEN(@QRY)-12)

EXEC( @QRY)

我做了什么?

  1. 查询sys.databases表以获取用户创建的数据库。
  2. 通过将name的数据库sys.databases列作为表的前缀来附加查询。和UNION ALL作为查询的后缀

  3. 通过内部循环使用@QRY

  4. 将其存储在变量SELECT @QRY =@QRY +
  5. 使用子字符串

  6. 删除了上一个UNION ALL
  7. 执行动态代码。

答案 3 :(得分:1)

这是一个可能的方法,使用游标 - 我已经逐字地将脚本添加到每次迭代游标时运行的动态sql。

SET NOCOUNT ON;
DECLARE @GET_Databases CURSOR;
DECLARE
     @Current_Database  NVARCHAR(250)
    ,@SQL               NVARCHAR(MAX) 
    ,@Param_Declare     NVARCHAR(500)
    ,@ErrorMsg          NVARCHAR(MAX)
    ,@LoopCount         INT
    ,@RowCount          NVARCHAR(5);

SET @SQL = ''
SET @Param_Declare = N'@Err NVARCHAR(MAX) OUTPUT,@Rows VARCHAR(5) OUTPUT'
SET @LoopCount = 1
IF OBJECT_ID('tempdb..#Results') IS NOT NULL DROP TABLE #Results;
CREATE TABLE #Results
( Id            INT IDENTITY(1,1)   NOT NULL
 ,DatabaseName  NVARCHAR(250)       NOT NULL
 ,trigger_name  NVARCHAR(250)       NOT NULL
 ,trigger_owner NVARCHAR(250)       NOT NULL
 ,table_schema  NVARCHAR(50)        NOT NULL
 ,table_name    NVARCHAR(250)       NOT NULL
 ,isupdate      INT                 NOT NULL
 ,isdelete      INT                 NOT NULL
 ,isinsert      INT                 NOT NULL
 ,isafter       INT                 NOT NULL
 ,isinsteadof   INT                 NOT NULL
 ,[disabled]    INT                 NOT NULL
 ,Error         NVARCHAR(MAX)           NULL
);

SET @GET_Databases = CURSOR FOR
SELECT name
FROM sys.databases
ORDER BY name;

OPEN @GET_Databases;

FETCH NEXT FROM @GET_Databases INTO @Current_Database;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQL = '';
SET @SQL = @SQL + 
----------------------------------------------------------------------------------------------------------------------------------------------  
----------------------------------------------------------------------------------------------------------------------------------------------  
'USE ' + QUOTENAME(@Current_Database) + ';
';

----------------------------------------------------------------------------------------------------------------------------------------------  
----------------------------------------------------------------------------------------------------------------------------------------------  
    SET @SQL = @SQL + '
BEGIN TRY
    ';
    SET @SQL = @SQL +

----------------------------------------------------------------------------------------------------------------------------------------------  
----------------------------------------------------------------------------------------------------------------------------------------------  
'INSERT #Results (DatabaseName, trigger_name, trigger_owner, table_schema, table_name, isupdate, isdelete, isinsert, isafter, isinsteadof, disabled)
SELECT
    ''' + @Current_Database + ''',
    sysobjects.name AS trigger_name,
    USER_NAME(sysobjects.uid) AS trigger_owner,
    s.name AS table_schema,
    OBJECT_NAME(parent_obj) AS table_name,
    OBJECTPROPERTY( id, ''ExecIsUpdateTrigger'') AS isupdate,
    OBJECTPROPERTY( id, ''ExecIsDeleteTrigger'') AS isdelete,
    OBJECTPROPERTY( id, ''ExecIsInsertTrigger'') AS isinsert,
    OBJECTPROPERTY( id, ''ExecIsAfterTrigger'') AS isafter,
    OBJECTPROPERTY( id, ''ExecIsInsteadOfTrigger'') AS isinsteadof,
    OBJECTPROPERTY(id, ''ExecIsTriggerDisabled'') AS [disabled] 
FROM
    sysobjects 
INNER JOIN 
    sysusers ON sysobjects.uid = sysusers.uid 
INNER JOIN 
    sys.tables t ON sysobjects.parent_obj = t.object_id 
INNER JOIN 
    sys.schemas s ON t.schema_id = s.schema_id 
WHERE 
    sysobjects.type = ''TR'' 
    AND OBJECT_NAME(parent_obj) IN (''accgrp'', ''portinfo'')'
----------------------------------------------------------------------------------------------------------------------------------------------  
----------------------------------------------------------------------------------------------------------------------------------------------  
    SET @SQL = @SQL + '
    SET @Rows = CONVERT(NVARCHAR(5),@@ROWCOUNT)
END TRY
BEGIN CATCH
    SET @Err = ERROR_MESSAGE();
END CATCH';

    IF @LoopCount = 1
    BEGIN
        PRINT 'Example SQL Script:';
        PRINT @SQL;
    END

    PRINT @Current_Database;

    EXECUTE sp_executesql
         @SQL
        ,@Param_Declare
        ,@Err = @ErrorMsg OUTPUT
        ,@Rows = @RowCount OUTPUT;

    IF @ErrorMsg IS NOT NULL
    BEGIN 
        PRINT @ErrorMsg + '
';
        SET @ErrorMsg = NULL;
    END
    ELSE PRINT '    ' + @Rowcount + ' Rows

';
    SET @LoopCount = @LoopCount + 1;
    FETCH NEXT FROM @GET_Databases INTO @Current_Database;
END

CLOSE @GET_Databases;
DEALLOCATE @GET_Databases;

SET NOCOUNT OFF;

SELECT * FROM #Results

DROP TABLE #Results