diy*_*da_ 0 sql sql-server temp-tables sql-server-2008-r2
我正在创建一个电子邮件队列来处理电子邮件发送 从该队列中,我正在记录X个记录,并根据记录的类型字段发送电子邮件.
为此,我在存储过程中声明了一个表.当记录X个记录时,我将在EmailQ表中设置记录的状态 进行处理.但是在发送X号记录之后,现在在声明的表中必须删除.
为此我可以使用Delete但是有TRUNCATE删除表中的所有记录.但声明的表未标识为表.
WHILE EXISTS ( SELECT * FROM emailQ WHERE Status != 3)
BEGIN
CREATE PROCEDURE [dbo].[SendMails]
DECLARE @Temp TABLE (......)
--Declare all the necessary variables
INSERT INTO @Temp SELECT TOP 10
WITH (UPDLOCK, HOLDLOCK)
--Update the email queue table status of selected set of records in to the @Temp
DECLARE dataSet CURSOR FORWARD_ONLY FOR (SELECT....... FROM @Temp)
OPEN dataSet
FETCH NEXT FROM dataSet INTO...
WHILE @@FETCH_STATUS = 0
BEGIN
--send mails acordingly
END
CLOSE dataSet
DEALLOCATE dataSet
--Update the email queue table status of completed set of records in to the @Temp
WAITFOR DELAY...
TRUNCATE @Temp// This is where this Temp table is not identified as a table(It says "Incorrect sintax... Expecting a table")
END
Run Code Online (Sandbox Code Playgroud)
从此声明的表中删除记录的最合适方法是什么.我也很感谢我处理邮件发送方式的评论.
谢谢.
你应该用删除来做
DELETE FROM @Temp
Run Code Online (Sandbox Code Playgroud)
检查相关问题
有关截断和临时表的更多信息
更新:
截断表不适用于声明的表变量.您应该使用#Temp表或删除行而不是trancating.查看相关问题以获取更多信息.
更新2:
Martin Smith给出了很好的答案