sql - How to create a trigger that uses the results from a UDF to insert into another table -


i'm using sql server r2.

two tables:

userentryaccess (entryid, userid) parententryrelation(id, parenttype, parentid, entryid) 

when record inserted parententryrelation, trigger needs list of user ids have access walking parententryrelation table, , insert entryid , userid userentryaccess. have function recursive common table expression retrieves these user ids. works great, yippee.

the question: what single sql query can use call function every record in inserted table, , insert results entryid userentryaccess in single sql statement.

here recursive cte:

create function [dbo].[getuseraccessids](@entryid uniqueidentifier) returns @tempuserids table (userid uniqueidentifier) begin  recursiveentry (parentid,entryid,parenttype) ( -- anchor member definition select parentid,entryid,parenttype parententryrelation parententryrelation.entryid = @entryid union -- recursive member definition select parententryrelation.parentid, parententryrelation.entryid, parententryrelation.parenttype parententryrelation inner join recursiveentry on recursiveentry.parentid = parententryrelation.entryid )  insert @tempuserids select distinct parentid recursiveentry ((parenttype & 32) = 32) --32 serveruser type option (maxrecursion 32767)  return end 

sounds you'd want use cross apply.

insert userentryaccess      (entryid, userid)     select i.entryid, f.userid         inserted             cross apply [dbo].[getuseraccessids](i.entryid) f 

Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -