Link to home
Start Free TrialLog in
Avatar of Fay A
Fay A

asked on

trigger to create a view when table is created

Hi All, I am working on a trigger that works good on the same database. If I run these triggers it does create a view, alters and deletes on the same database, But I would like this trigger to run on SQL09 box when a new table is created  (pipelinetrueup database) and then create a view on SQL04 box under (ODS database). Is this possible?

here is my create trigger stmt:

USE [PipelineTrueUp]
GO

/****** Object:  DdlTrigger [trg_pipeline_orchestration_create]    Script Date: 4/9/2018 1:30:37 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





CREATE TRIGGER [trg_pipeline_orchestration_create]
            ON DATABASE
            FOR CREATE_TABLE
            AS
       -- SET XACT_ABORT OFF;
            SET NOCOUNT ON
            DECLARE @data XML
            SET @data = EVENTDATA()
            DECLARE @schemaName VARCHAR(250) =  @data.value('(/EVENT_INSTANCE/SchemaName)[1]', 'varchar(250)')
            DECLARE @tableName VARCHAR(250) =  @data.value('(/EVENT_INSTANCE/ObjectName)[1]', 'varchar(250)')
            DECLARE @SQL AS NVARCHAR(4000)
            IF(@schemaName != 'tmp' AND @tableName NOT LIKE 'voicerecognition%')
            BEGIN
                  SET @SQL = ' CREATE VIEW pipeline.vw_' + @tableName + ' as SELECT a.* From PipelineTrueup.' + @schemaName + '.' + @tableName + ' a WITH (NOLOCK)'
                  --EXEC ODS.dbo.sp_executesql @SQL
                  EXEC (@sql) AT ATLASBI
            END
        IF(@schemaName != 'tmp' AND @tableName LIKE 'voicerecognition%')
            BEGIN
                  SET @SQL = ' CREATE VIEW voicerecognition.vw_' + @tableName + ' as SELECT a.* From PipelineTrueup.' + @schemaName + '.' + @tableName + ' a WITH (NOLOCK);'
                  --EXEC ODS.dbo.sp_executesql @SQL
                  EXEC (@sql) AT ATLASBI
            END
GO

DISABLE TRIGGER [trg_pipeline_orchestration_create] ON DATABASE
GO
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

I'm wondering why do you need a View that's simple return all data from a table?
Avatar of Fay A
Fay A

ASKER

that's where we point our users to run reports, view on SQL04 box under ODS database. There are many tables created daily on SQL09 box which is the master in availability groups so we don't want users running or altering tables there for their needs.
Looks like you're not using the permissions correctly. Why are you giving users the permissions to perform changes?
By the way, if you're use SQL Server Availability Groups, you can point the reports to run on a secondary Replica. Secondary Replicas are read only so even they want, they can't perform changes on the database.
Avatar of Fay A

ASKER

Well, I know. There are other job duties where we need these views to be created. If there is table created on SQL09 box under pipelinetrueup database then we need a view to be created on a SQL04 box under ODS database. Is that doable under a trigger?
What's wrong with your code?
Avatar of Fay A

ASKER

it works on the same box , somehow I need to change the code so the view gets created on SQL04 box instead.
ASKER CERTIFIED SOLUTION
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Fay A

ASKER

It is pointing to some test server. Do you think if I change this to a server where I need the view to be created would that work?
Avatar of Fay A

ASKER

Also, these two servers where i want this are part of AG.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial