I’d like to create a procedure that selects all records that have been assigned to any particular user, then send one personalized email to each assigned user that contains a list of the records that are assigned to them.
So if myTable looks like this:
ID Assigned
1 Joe
2 Joe
3 Shelly
Joe’s email would display a line-delimited list with records 1 and 2 in it, and Shelly’s would display record 3.
I started building a procedure with cursors at first, but 1) wasn’t sure if I could reference a cursor within another cursor, and 2) don’t know if a cursor is even the best approach for this.
My thought was that cursor 1 would get all unique Assigned values (Joe, Shelly) and cursor 2 would be executed inside the cursor 1 loop and get all records assigned to the current cursor 1 value.
Any insight or nudges in the appropriate direction would be greatly appreciated.
It is possible to reference another cursor within the first one:
But you can do better, deppends on your version.
You can aggregate the records so in one query you’ll get one record for each user with a column that contains the records concatenated with a
newline:here are some methods to this.
And you can also use xmlagg: