Reputation: 93
I am developing an application where I need to send the Emails to all the users in the table. Since the number of users is in millions, so to select all the users at once won’t do any good. Is there any way I can send emails in a group of 20 or 30 users so that the next time it will select the next 20 users dynamically and send emails. Any help will be appreciated.
Upvotes: 0
Views: 404
Reputation: 208
If you are using SQL Server 2005 or higher you can use Row_Number() to get the desired number of users.
Example:
Select * from (Select *, Row_number()over (order by UserEmail) as Row from Userprofile) T
Where T.Row Between 30 and 40
How ever if you are using SQL Server 2000 there is nothing like Row_Number(). You will have to use IDENTITY() function and a temporary table.
Example:
SELECT IDENTITY(int, 1,1) AS RowNumber, UserEmail INTO #temp
FROM Userprofile ORDER BY UserEmail ASC
SELECT * FROM #Temp Where #Temp.RowNumber Between 10 and 20 ORDER BY RowNumber DROP TABLE #Temp
Make sure to drop the Temporary table.
Upvotes: 1
Reputation: 2466
Create a new table EmailReceivers that you fill with all the email addresses that you wish to send to. Then select top 30 from that table, and once you have done sending the email you delete that record from that table. When the table is empty you are done.
create table EmailReceivers
(
Id bigint identity(1, 1) primary key clustered,
EmailAddress varchar(5000)
)
-- Now fill this table with all the email addresses
insert EmailReceivers(EmailAddress)
select Email from users....
-- Select top 30 to send
select top 30 Id, EmailAddress from EmailReceivers order by Id
-- When done sending, delete the receiver from the table
delete EmailReceivers where id = @id
Doing this many deletes can be bit of a performance hog, so you might want to delete in chunks. Do so by delete all of the rows you previously selected. Just grab the highest ID from that batch and delete all the rows with IDs lower or equal to that ID.
delete EmailReceivers where id <= @maxIdFromLastBatch
Upvotes: 0