如何为SQL Server 2005实例中的所有数据库创建DDL触发器

发布于 2024-07-23 06:44:54 字数 748 浏览 8 评论 0原文

我将为 SQL Server 实例中的所有数据库创建一个 DDL 触发器。 我想在一次运行中完成此操作,而不是为每个数据库运行多次。

下面是我需要执行的两条 T-SQL 语句:

-- Create table

use <dbname>
GO
CREATE TABLE dbo.ChangeAttempt
(EventData xml NOT NULL,
AttemptDate datetime NOT NULL DEFAULT GETDATE(),
DBUser char(50) NOT NULL)
GO

-- Create DDL trigger 

use <dbname>
GO
CREATE TRIGGER db_trg_ObjectChanges
ON DATABASE
FOR ALTER_PROCEDURE, DROP_PROCEDURE,
 ALTER_INDEX, DROP_INDEX,
 ALTER_TABLE, DROP_TABLE, ALTER_TRIGGER, DROP_TRIGGER,
 ALTER_VIEW, DROP_VIEW, ALTER_SCHEMA, DROP_SCHEMA,
 ALTER_ROLE, DROP_ROLE, ALTER_USER, DROP_USER
AS
SET NOCOUNT ON
INSERT dbo.ChangeAttempt
(EventData, DBUser)
VALUES (EVENTDATA(), USER)
GO

我的问题是:如何以编程方式在一次运行中创建 DDL 触发器?

I am going to create a DDL trigger to all databases in SQL Server instance. I'd like to do it in one run instead of many runs for each database.

Below are the two T-SQL statements I need to execute:

-- Create table

use <dbname>
GO
CREATE TABLE dbo.ChangeAttempt
(EventData xml NOT NULL,
AttemptDate datetime NOT NULL DEFAULT GETDATE(),
DBUser char(50) NOT NULL)
GO

-- Create DDL trigger 

use <dbname>
GO
CREATE TRIGGER db_trg_ObjectChanges
ON DATABASE
FOR ALTER_PROCEDURE, DROP_PROCEDURE,
 ALTER_INDEX, DROP_INDEX,
 ALTER_TABLE, DROP_TABLE, ALTER_TRIGGER, DROP_TRIGGER,
 ALTER_VIEW, DROP_VIEW, ALTER_SCHEMA, DROP_SCHEMA,
 ALTER_ROLE, DROP_ROLE, ALTER_USER, DROP_USER
AS
SET NOCOUNT ON
INSERT dbo.ChangeAttempt
(EventData, DBUser)
VALUES (EVENTDATA(), USER)
GO

My question is: how can I programmaticaly create DDL trigger in one run?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

智商已欠费 2024-07-30 06:44:54

为什么你需要跑一趟? 这是唯一的方法。

Msg 111, Level 15, State 1, Line 2
'CREATE TRIGGER' must be the first statement in a query batch.

运行由此生成的输出:

DECLARE @DatabaseName varchar(500)
DECLARE @Database_id  int
DECLARE @Query        varchar(8000)
DECLARE @CRLF         char(2)

SET @CRLF=CHAR(13)+CHAR(10)
---MODIFY THIS TO INCLUDE THE DATABASES THAT YOU WANT TO WORk ON
---MODIFY THIS TO INCLUDE THE DATABASES THAT YOU WANT TO WORk ON
select @Database_id=MIN(database_id) from sys.databases where database_id IN (5,7,8,6)

WHILE @Database_id IS NOT NULL
BEGIN
    SELECT @DatabaseName=name from sys.databases where database_id=@Database_id
    SET @Query='-- Create table'+@CRLF+@CRLF
                +'use '+@DatabaseName+@CRLF
                +' GO'+@CRLF
                +' CREATE TABLE dbo.ChangeAttempt'+@CRLF
                +' (EventData xml NOT NULL,'+@CRLF
                +' AttemptDate datetime NOT NULL DEFAULT GETDATE(),'+@CRLF
                +' DBUser char(50) NOT NULL)'+@CRLF
                +'GO'+@CRLF+@CRLF
                +'-- Create DDL trigger '+@CRLF+@CRLF
                +'use '+@DatabaseName+@CRLF
                +'GO'+@CRLF
                +'CREATE TRIGGER db_trg_ObjectChanges'+@CRLF
                +'ON DATABASE'+@CRLF
                +'FOR ALTER_PROCEDURE, DROP_PROCEDURE,'+@CRLF
                +' ALTER_INDEX, DROP_INDEX,'+@CRLF
                +' ALTER_TABLE, DROP_TABLE, ALTER_TRIGGER, DROP_TRIGGER,'+@CRLF
                +' ALTER_VIEW, DROP_VIEW, ALTER_SCHEMA, DROP_SCHEMA,'+@CRLF
                +' ALTER_ROLE, DROP_ROLE, ALTER_USER, DROP_USER'+@CRLF
                +'AS'+@CRLF
                +'SET NOCOUNT ON'+@CRLF
                +'INSERT dbo.ChangeAttempt'+@CRLF
                +'(EventData, DBUser)'+@CRLF
                +'VALUES (EVENTDATA(), USER)'+@CRLF
                +'GO'+@CRLF
    PRINT @Query
    ---MODIFY THIS TO INCLUDE THE DATABASES THAT YOU WANT TO WORk ON
    ---MODIFY THIS TO INCLUDE THE DATABASES THAT YOU WANT TO WORk ON
    select @Database_id=MIN(database_id) from sys.databases WHERE database_id IN (5,7,8,6) AND database_id>@Database_id
END

编辑
要确定为其生成脚本的数据库,请执行以下操作:

  • 运行此查询:

    select database_id,name from sys.databases

  • 查找要为其运行脚本的所有数据库

  • 在两个地方更改我的上述脚本(循环之前和循环底部),因此您想要的所有database_id都在以下代码部分中:

    WHERE database_id IN (AAA,BBB,CCC,DDD,....)

why do you need one run? this is the only way to do it.

Msg 111, Level 15, State 1, Line 2
'CREATE TRIGGER' must be the first statement in a query batch.

run the output generated by this:

DECLARE @DatabaseName varchar(500)
DECLARE @Database_id  int
DECLARE @Query        varchar(8000)
DECLARE @CRLF         char(2)

SET @CRLF=CHAR(13)+CHAR(10)
---MODIFY THIS TO INCLUDE THE DATABASES THAT YOU WANT TO WORk ON
---MODIFY THIS TO INCLUDE THE DATABASES THAT YOU WANT TO WORk ON
select @Database_id=MIN(database_id) from sys.databases where database_id IN (5,7,8,6)

WHILE @Database_id IS NOT NULL
BEGIN
    SELECT @DatabaseName=name from sys.databases where database_id=@Database_id
    SET @Query='-- Create table'+@CRLF+@CRLF
                +'use '+@DatabaseName+@CRLF
                +' GO'+@CRLF
                +' CREATE TABLE dbo.ChangeAttempt'+@CRLF
                +' (EventData xml NOT NULL,'+@CRLF
                +' AttemptDate datetime NOT NULL DEFAULT GETDATE(),'+@CRLF
                +' DBUser char(50) NOT NULL)'+@CRLF
                +'GO'+@CRLF+@CRLF
                +'-- Create DDL trigger '+@CRLF+@CRLF
                +'use '+@DatabaseName+@CRLF
                +'GO'+@CRLF
                +'CREATE TRIGGER db_trg_ObjectChanges'+@CRLF
                +'ON DATABASE'+@CRLF
                +'FOR ALTER_PROCEDURE, DROP_PROCEDURE,'+@CRLF
                +' ALTER_INDEX, DROP_INDEX,'+@CRLF
                +' ALTER_TABLE, DROP_TABLE, ALTER_TRIGGER, DROP_TRIGGER,'+@CRLF
                +' ALTER_VIEW, DROP_VIEW, ALTER_SCHEMA, DROP_SCHEMA,'+@CRLF
                +' ALTER_ROLE, DROP_ROLE, ALTER_USER, DROP_USER'+@CRLF
                +'AS'+@CRLF
                +'SET NOCOUNT ON'+@CRLF
                +'INSERT dbo.ChangeAttempt'+@CRLF
                +'(EventData, DBUser)'+@CRLF
                +'VALUES (EVENTDATA(), USER)'+@CRLF
                +'GO'+@CRLF
    PRINT @Query
    ---MODIFY THIS TO INCLUDE THE DATABASES THAT YOU WANT TO WORk ON
    ---MODIFY THIS TO INCLUDE THE DATABASES THAT YOU WANT TO WORk ON
    select @Database_id=MIN(database_id) from sys.databases WHERE database_id IN (5,7,8,6) AND database_id>@Database_id
END

EDIT
to determine what databases to generate scripts for do the following:

  • run this query:

    select database_id,name from sys.databases

  • find all of the databases you want to run the scripts for

  • change my above script in two places (before loop & at bottom in loop) so all of the database_id that you want are in the following code section:

    WHERE database_id IN (AAA,BBB,CCC,DDD,....)

叹倦 2024-07-30 06:44:54

您可以使用 sp_MSforeachdb。

像这样的事情

sp_MSforeachdb
'
    CREATE TABLE ?.dbo.ChangeAttempt
        etc. etc. etc.
'
sp_MSforeachdb
'
    CREATE TRIGGER ?.dbo.db_trg_ObjectChanges
        etc. etc. etc.
'

我还没有测试过,理论上我希望它能工作。 不过,您要确保排除系统数据库。

You could use sp_MSforeachdb.

Something like this

sp_MSforeachdb
'
    CREATE TABLE ?.dbo.ChangeAttempt
        etc. etc. etc.
'
sp_MSforeachdb
'
    CREATE TRIGGER ?.dbo.db_trg_ObjectChanges
        etc. etc. etc.
'

I haven't tested this, in theory I'd expect it to work though. You want to make sure you exclude the system databases though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文