loop through sql statement

I am trying to loop through a sql select statement.
and on each row run a sendemail to the data from that row.

I copied this code from something else and I am not sure that I took too much and what can be removed.
I have something like this, but it's not 100% correct because the while loop is throwing an error.
declare csrIds cursor static for

open csrIds

while 1 = 1
begin
    fetch next from csrIds into @id
    if @@FETCH_STATUS <> 0
        break

 select @email = bd.Email, @name =  bd.FirstName
    from  TestEmail bd
    where
      where sentEmail<> 1

DECLARE @Sub nvarchar(MAX)
SET @Sub = 'this is a record that came through: ' + @email + ' and name of : ' + @name

EXEC msdb.dbo.sp_send_dbmail
  @body_format= 'HTML',@recipients= @email ,@subject = 'Cash Receipts' ,
   @body=@Sub ;

end --while

deallocate csrIds 

Open in new window

rivkamakAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leo TorresSQL DeveloperCommented:
you have to use a cursor

DECLARE  @MyVariable INT,	

DECLARE My_Cursor CURSOR FAST_FORWARD FOR
Select col1 from MyTable  

OPEN My_Cursor
	FETCH NEXT FROM My_Cursor INTO @MyVariable
	WHILE @@FETCH_STATUS =0
	BEGIN 
	
	--Your iteration Here
	
FETCH NEXT FROM My_Cursor  INTO @MyVariable 
END
CLOSE My_Cursor DEALLOCATE My_Cursor 	

Open in new window

0
rivkamakAuthor Commented:
I tried putting in my select statement.
Did I do it wrong?
DECLARE  @MyVariable INT,	

DECLARE My_Cursor CURSOR FAST_FORWARD FOR
 select @email = bd.Email, @name =  bd.FirstName
    from  TestEmail bd
    where
      where sentEmail<> 1

OPEN My_Cursor
	FETCH NEXT FROM My_Cursor INTO @MyVariable
	WHILE @@FETCH_STATUS =0
	BEGIN 
	
DECLARE @Sub nvarchar(MAX)
SET @Sub = 'this is a record that came through: ' + @email + ' and name of : ' + @name


			EXEC msdb.dbo.sp_send_dbmail

   @body_format= 'HTML',@recipients= @email ,@subject = 'Cash Receipts' ,
    @body=@Sub ;
	
FETCH NEXT FROM My_Cursor  INTO @MyVariable 
END
CLOSE My_Cursor DEALLOCATE My_Cursor 

Open in new window

0
Leo TorresSQL DeveloperCommented:
try

DECLARE  @MyVariable varchar(255),@MyVariable2 varchar(255)	

DECLARE My_Cursor CURSOR FAST_FORWARD FOR
 select bd.Email, bd.FirstName
    from  TestEmail bd
    where
      where sentEmail<> 1

OPEN My_Cursor
	FETCH NEXT FROM My_Cursor INTO @MyVariable, @MyVariable2
	WHILE @@FETCH_STATUS =0
	BEGIN 
	
DECLARE @Sub nvarchar(MAX)
SET @Sub = 'this is a record that came through: ' + @MyVariable + ' and name of : ' + @MyVariable2


			EXEC msdb.dbo.sp_send_dbmail

   @body_format= 'HTML',@recipients= @MyVariable ,@subject = 'Cash Receipts' ,
    @body=@Sub ;
	
FETCH NEXT FROM My_Cursor  INTO @MyVariable, @MyVariable2 
END
CLOSE My_Cursor DEALLOCATE My_Cursor 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.