I have a Table Reminder, Messaging and Users. I have to implement that whenever a row is created in the Reminder table then the Administrators from the Users table will get message in their Messaging table.
I am doing everything inside the sql query. I have reached till the inserton and selection of all admin users, also I am able to run a loop on the number of records. I want to insert one by one in a loop.
If @flag=1 // flag=1 means reminder is created successfully
BEGIN
Declare @BackUpMessage as varchar(200)
Set @BackupMessage = 'Hi Admin,\r\n
\t A backup has been scheduled by the system. Details are given below: \r\n
a) Bakcup Type : '+rem_type+' \r\n
b) Backup Date : '+ Convert(varchar,GETDATE()+@days)+''
Set @Count = (Select Count(*) From JFPUsers Where RoleId=1 and Active=1) // Total Numbers of Admin
IF @Count > 0
Declare @i int =0
Select Id From JFPUsers Where RoleId=1 and Active=1 // Getting Id of each admin to save
While @i < @Count
BEGIN
Insert Into JFMessaging (MessageBody, SenderId,RecieverId,IsRead,SentOn,IsActive)
Values(@BackUpMessage, -1, ,0,GETDATE(),1) // I am stucked here
END
END
I can do it easily using C# but I want to do these stuffs in a single connection and in a single hit @ backend .
Update : See Image and the summary
Summary : What I need is whenever a record is created in reminder. The query will pick all admin from JFPUsers {condition RoleId=1 is admin} and create that much row in JFMessaging where SenderId will be the user creating reminder or -1 (for system) and RecieverId will contain the admin id with a custom notification message.

Create an
INSERTtrigger on your Reminder table that does an INSERT into the messages table for administrators. Try to get out of the habit of using loops in SQL – it is usually bad practice.Something like this…