SQL DDL触发器

--同一个数据库服务器上 不同数据库 表结构同步 
CREATE PROCEDURE PRO_DB_DDL
(@sql nvarchar(max))
as
begin
 set nocount on
 declare @str nvarchar(max)
 set @str=' use mytest '
 set @str=@str+' '+@sql
 execute (@str)
 
end

GO

CREATE Trigger db_triGG on DATABASE for create_table
as
begin
set nocount on
DECLARE @SQL NVARCHAR(MAX)
SET @SQL='';
SELECT @SQL=EVENTDATA().value
        ('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]','nvarchar(max)')
EXEC PRO_DB_DDL @SQL
        
 end

 

CREATE TABLE aa(ID INT)
GO
DROP TABLE aa

 

或者

Create Trigger db_triGG on DATABASE for create_table
as
begin
set nocount on
DECLARE @SQL NVARCHAR(MAX)
SET @SQL=' USE MYTEST  ';
SELECT @SQL=@SQL+EVENTDATA().value
        ('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]','nvarchar(max)')
EXEC (@SQL)
        
 end


 

你可能感兴趣的:(sql,数据库,table,database,数据库服务器)