2010-11-16 65 views
0

我做这样的事情:如果NOT EXISTS逻辑

exec up_sp1 -- executing this stored procedure which populates the table sqlcmds 

---Check the count of the table sqlcmds, 
---if the count is zero then execute up_sp2, 
----otherwise wait till the count becomes zero,then exec up_sp2 


IF NOT EXISTS (SELECT 1 FROM [YesMailReplication].[dbo].[SQLCmds]) 
BEGIN 

exec up_sp2 

END 

会是什么样的T-SQL的样子是否正确?

回答

1

服务代理的解决方案无疑是最好的 - 但有一个WAITFOR解决方案,以及:

exec up_sp1; 

while exists (select * from [YesMailReplication].[dbo].[SQLCmds]) begin 
    waitfor delay ('00:00:10'); -- wait for 10 seconds 
end; 

exec up_sp2; 
1

试试这个:

DECLARE @Count int 
SELECT @Count = COUNT(*) FROM [YesMailReplication].[dbo].[SQLCmds]) 

IF @Count > 0 BEGIN 

exec up_sp2 

END 
2

T-SQL没有WAITFOR语义除了Service Broker的队列。因此,您只需使用Service Broker即可定期轮询并查看该表是否已填充。对于小规模的企业来说,这样做很好,但是对于高规模而言,由于等待时间和轮询频率之间的平衡难以实现,因此更难以适应尖峰和低谷。

但如果你愿意使用服务代理,那么你可以通过利用Activation做的更优雅和可扩展的解决方案:up_sp1下降的消息到队列中,该消息激活启动,进而启动up_sp2排队过程中,在up_sp1提交之后。这是一种可靠的机制,可以处理服务器重新启动,镜像和集群故障切换,甚至可以通过备份重建服务器。请参阅Asynchronous procedure execution以获得非常相似的示例。

0

为什么不简单和自我记录?

DECLARE @Count int; 

SELECT @Count = Count(*) FROM [YesMailReplication].[dbo].[SQLCmds] 
If @Count = 0 exec up_sp2 
+2

我看到,安倍晋三发表他的反应,而我在写我的... – Flipster 2010-11-16 23:55:55