在sql中同时使用“临时表”和“while循环”

在sql中同时使用“临时表”和“while循环”,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我想创建一个表,然后在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 Kent

我想创建一个表,然后在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

试试这个:把这个部分放进去

while (  @i<=(select max(RowNumber) from cte))
BEGIN   
        @i=@i+1;
    EXEC msdb.dbo.sp_send_dbmail @profile_name='ExpireAdvertiseEmail',
    @recipients=  (

之后

就我的理解而言,cte定义必须由select直接遵循,并且只能在后面的一个select中使用

希望对你有帮助


编辑:逗号显然是函数参数列表的一部分

while (  @i<=(select max(RowNumber) from cte))
BEGIN   
        @i=@i+1;
    EXEC msdb.dbo.sp_send_dbmail @profile_name='ExpireAdvertiseEmail',
    @recipients=  (

之后

就我的理解而言,cte定义必须由select直接遵循,并且只能在后面的一个select中使用

希望对你有帮助


编辑:逗号显然是函数参数列表的一部分

不能将CTE用作临时表。您可以创建表或声明表变量,将数据放在其中并执行以下操作:

create table #temp (RowNumber int, Email nvarchar(max)
但到目前为止,您的代码似乎可以这样更改:

declare @Email nvarchar(max)

declare cur cursor local fast_forward for
    select
        u.Email 
    from KenticoCMS1.dbo.AD_Advertise as a
        inner join KenticoCMS1.dbo.CMS_User as u on u.UserID = a.ItemCreatedBy 
    where
        a.AdShow = 1 and
        datediff(day, getdate(), a.AdDateTo) in 
        (
            select t.RemainDays
            from KenticoCMS1.dbo.AD_SendEmailForExpire as t
        )

open cur
while 1 = 1
begin
    fetch cur into @Email
    if @@fetch_status <> 0 break

    exec msdb.dbo.sp_send_dbmail
        @profile_name = 'ExpireAdvertiseEmail',
        @recipients = @Email,
        @subject = 'Test message',
        @body = 'This is the body of the test message.Congrates Database Mail Received By you Successfully.'      

end
close cur
deallocate cur

注意表格别名,删除多余的括号。我认为删除datediff也会有帮助,但必须先查看数据。

不能将CTE用作临时表。您可以创建表或声明表变量,将数据放在其中并执行以下操作:

create table #temp (RowNumber int, Email nvarchar(max)
但到目前为止,您的代码似乎可以这样更改:

declare @Email nvarchar(max)

declare cur cursor local fast_forward for
    select
        u.Email 
    from KenticoCMS1.dbo.AD_Advertise as a
        inner join KenticoCMS1.dbo.CMS_User as u on u.UserID = a.ItemCreatedBy 
    where
        a.AdShow = 1 and
        datediff(day, getdate(), a.AdDateTo) in 
        (
            select t.RemainDays
            from KenticoCMS1.dbo.AD_SendEmailForExpire as t
        )

open cur
while 1 = 1
begin
    fetch cur into @Email
    if @@fetch_status <> 0 break

    exec msdb.dbo.sp_send_dbmail
        @profile_name = 'ExpireAdvertiseEmail',
        @recipients = @Email,
        @subject = 'Test message',
        @body = 'This is the body of the test message.Congrates Database Mail Received By you Successfully.'      

end
close cur
deallocate cur

注意表格别名,删除多余的括号。我认为删除datediff也会有帮助,但必须先查看您的数据。

嗨,Marjan Yazdani!!你应该首先在谷歌上搜索关于临时表的信息first@AliSarshogh我找了。。但是我没有发现什么特别的。。。所以我和往常一样用。我不想声明一个表,因为我有太多的列,我和你一样懒惰!你写在你的个人资料里。所以我在这里问它,我学会了光标!我以前没用过…谢谢女士。。。所以你的网站是哇…我为你感到骄傲…嗨Marjan Yazdani!!你应该首先在谷歌上搜索关于临时表的信息first@AliSarshogh我找了。。但是我没有发现什么特别的。。。所以我和往常一样用。我不想声明一个表,因为我有太多的列,我和你一样懒惰!你写在你的个人资料里。所以我在这里问它,我学会了光标!我以前没用过…谢谢女士。。。所以你的网站是哇…我为你感到骄傲…如果你不介意向所有收件人发送一封信,我认为甚至可以不用光标,只需将电子邮件连接在一个长字符串中,每个收件人的主题和正文都不一样。我在顶部和游标中声明了一些其他变量…是否可以使用@TableVariable而不是诱惑变量来执行相同的操作?如果您不介意向所有收件人发送一封信,我认为甚至可以在没有游标的情况下执行此操作,只需将电子邮件连接在一个长字符串中,每个收件人的主题和正文都不同。我在顶部和游标中声明了一些其他变量……是否可以使用@TableVariable而不是诱惑变量来执行相同的操作?
declare @Email nvarchar(max)

declare cur cursor local fast_forward for
    select
        u.Email 
    from KenticoCMS1.dbo.AD_Advertise as a
        inner join KenticoCMS1.dbo.CMS_User as u on u.UserID = a.ItemCreatedBy 
    where
        a.AdShow = 1 and
        datediff(day, getdate(), a.AdDateTo) in 
        (
            select t.RemainDays
            from KenticoCMS1.dbo.AD_SendEmailForExpire as t
        )

open cur
while 1 = 1
begin
    fetch cur into @Email
    if @@fetch_status <> 0 break

    exec msdb.dbo.sp_send_dbmail
        @profile_name = 'ExpireAdvertiseEmail',
        @recipients = @Email,
        @subject = 'Test message',
        @body = 'This is the body of the test message.Congrates Database Mail Received By you Successfully.'      

end
close cur
deallocate cur