我有一个'N'行的表,我试图逐行获取并想要执行一些操作。
有人可以帮我解决这个存储过程吗?
有关详细信息:我正在尝试从' UserMaster'中检索邮件ID。表一个接一个,我想用这个邮件ID发送邮件给特定用户,我可以发邮件给用户,
一个接一个地检索它们
declare @count as int
declare @inti as int
select COUNT(UserId) from tblUserMaster where AccessLevelId = '1'
SET @inti = 1
while @inti <= @count
BEGIN
select @mail = Email_ID
from User_Master
where AccessLevelId = '1'
rownum(@inti) -- here I need to retrieve one bye one row
@inti ++;
EXEC sp_send_dbmail @profile_name = 'PinalProfile',
@recipients = @mail,
@subject = 'Test message',
@body = 'This is the body of the test message.
Congrates Database Mail Received By you Successfully.'
END
答案 0 :(得分:0)
有多种方法可以做到这一点,我认为最简单的方法是将结果集插入带有标识列的临时对象,然后遍历临时对象。这是一些伪代码:
DECLARE @t1 table (id int identity, email varchar(50)),
@count int,
@i int = 1,
@email varchar(50);
INSERT INTO @t1 (email)
select Email_ID
from User_Master
where AccessLevelId = '1';
select @count = count(*) from @t1;
while @i <= @count
BEGIN
select @email = email from @t1 where id = @i
EXEC sp_send_dbmail @profile_name = 'PinalProfile',
@recipients = @mail,
@subject = 'Test message',
@body = 'Test body';
set @i = @i + 1;
END