I have a table in database (let’s say ITEMTYPE) with column (let’s say INSTANCE_DATA) where the name of another table (no matter existing or not) in same database, stored.
According to business logic of my app when I create a new entity in the app following things happened:
- New row added to ITEMTYPE table with null in INSTANCE_DATA column
- This row updated with proper value of INSTANCE_DATA column
- Table with the name = value from INSTANCE_DATA created
All these things happen under the hood of the app and outside of my control.
I’m trying to create update trigger on ITEMTYPE table, which can wait until new table will be created and perform some actions with this table.
My code:
CREATE TRIGGER TriggerName ON ITEMTYPE
FOR UPDATE
AS
IF UPDATE (INSTANCE_DATA)
BEGIN
SET NOCOUNT ON
DECLARE @newInstanceData NVARCHAR(MAX)
SELECT @newInstanceData = i.INSTANCE_DATA FROM inserted i
DECLARE @TableExists BIT = 0
WHILE @TableExists = 0
BEGIN
IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @newInstanceData)
BEGIN
SET @TableExists = 1
END
ELSE
BEGIN
WAITFOR DELAY '00:00:01'
END
END
-- do something with newly created table
END
But when trying to add entity through app, getting an eternal loop and new table never created.
Important note: I cannot use DDL triggers (which would be an obvious an easiest way) for some reason.
Is there any solution for this ?
Thank you.
Pafnutiy Seledkin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.