Point is to make a trigger which will:
- Check the configuration table which contains a column
ConnectionField nvarchar(50)
- It should return the string value (
columnName
) which will be used as a key
So on insert/update on table Workers
, the code should set my Xfield
value to the value from column ConnectionField
, read from the Configuration
table.
In short since this is all messy. I want to be able to let my end user to write down in configuration which column he will use as unique (Worker ID, SNSID, Name etc... ) based on his pick trigger need to put that field value to my Xfield
Don't ask why. It's really confusing.
I've written a trigger which will do that but it just is stuck somewhere in an infinite loop
CREATE TRIGGER [dbo].Tr_ConnectionField
ON [dbo].Workers
FOR INSERT, UPDATE
AS
SET NOCOUNT ON;
DECLARE @ID BIGINT
DECLARE @tmpUpit CURSOR;
DECLARE @ConFieldSETUP NVARCHAR(50)
-- Here I will read the field from configuration which will be used as key
SET @ConFieldSETUP = (SELECT TOP 1 ISNULL(ConnectionField, 'SNSID')
FROM ConfigurationTable)
BEGIN
SET @tmpUpit = CURSOR LOCAL SCROLL FOR
SELECT i.id FROM inserted i
OPEN @tmpUpit
END
FETCH NEXT FROM @tmpUpit INTO @ID
WHILE @@fetch_status = 0
BEGIN
-- Here I will use the configuration columns value to my Xfield
UPDATE Workers
SET Xfield = (SELECT @ConFieldSETUP
FROM Workers cld
WHERE cld.Id = @ID)
WHERE Id = @ID
END
FETCH NEXT FROM @tmpUpit INTO @ID
DEALLOCATE @tmpUpit