I am creating a trigger on a table in a SQL Server database.
But what I am unable to do is this: I have variables @pageid and @templateid populated, and I need to read all template column id’s from one table – basically get a list of them (they depend on the template id and can vary in number) so I need some way of making an array or something and go through it and add it to a table.
So…
-
how do I read all template id’s (image below: from 1st table (available template fields) –
where availabletemplateid is @templateid) into an array? -
how do I loop through this array and add them one by one it to another table (image below: 2nd table – (public page to field mapping) with
publicpageid = @pageidand fieldid is one by one from the previous step, and field content is “-“)

So far I have
ALTER TRIGGER CreatePageFields
ON dbo.PublicPages
FOR INSERT
AS
BEGIN TRANSACTION
/* variables */
DECLARE
@pageid bigint,
@templateid bigint
/* insert template fields for this page */
SELECT @pageid = id, @templateid = templateid FROM inserted
/* TODO 1 get all template field id's */
/* TODO 2 insert each templatefieldid / page combination */
/* execute */
COMMIT TRANSACTION
GO
Thanks a lot
I don’t think you need an array (table variable) or any looping (cursor). Something like this will be much, much faster (guessing at your table and field names):