テーブルを作成した後、while ループでテーブル情報を使用します。テーブルを作成するために「with」を使用していましたが、コードにエラーがあります。コードは以下のとおりです。
declare @i integer
set @i=0;
with cte as
(SELECT ROW_NUMBER()OVER(ORDER BY ItemID) as RowNumber,*,DATEDIFF(day,GETDATE(),AdDateTo)as DaysToExpire
FROM KenticoCMS1.dbo.AD_Advertise inner join KenticoCMS1.dbo.CMS_User
ON KenticoCMS1.dbo.AD_Advertise.ItemCreatedBy = KenticoCMS1.dbo.CMS_User.UserID
WHERE DATEDIFF(day,GETDATE(),AdDateTo) in (SELECT RemainDays FROM KenticoCMS1.dbo.AD_SendEmailForExpire)
AND AdShow=1)
--SELECT * FROM cte
while ( @i<=(select max(RowNumber) from cte))
BEGIN
@i=@i+1;
EXEC msdb.dbo.sp_send_dbmail @profile_name='ExpireAdvertiseEmail',
@recipients= select Email FROM cte where RowNumber=@i , --'mj.yazdani1988@gmail.com',
@subject='Test message',
@body='This is the body of the test message.Congrates Database Mail Received By you Successfully.'
END
GO
そして私のエラー:
Msg 156, Level 15, State 1, Line 13
Incorrect syntax near the keyword 'while'.
Msg 156, Level 15, State 1, Line 16
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Line 16
Incorrect syntax near ','.