Sql server 如果不存在逻辑
我正在做这样的事情:Sql server 如果不存在逻辑,sql-server,sql-server-2005,Sql Server,Sql Server 2005,我正在做这样的事情: 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
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是什么样子的?试试以下方法:
DECLARE @Count int
SELECT @Count = COUNT(*) FROM [YesMailReplication].[dbo].[SQLCmds])
IF @Count > 0 BEGIN
exec up_sp2
END
试试这个:
DECLARE @Count int
SELECT @Count = COUNT(*) FROM [YesMailReplication].[dbo].[SQLCmds])
IF @Count > 0 BEGIN
exec up_sp2
END
除了ServiceBroker队列之外,T-SQL没有WAITFOR语义。因此,除了使用ServiceBroker之外,您所能做的就是定期轮询并查看表是否已填充。对于小规模而言,这很好,但对于高规模而言,它会中断,因为等待时间和轮询频率之间的正确平衡很难实现,而且更难使其适应峰值和低点
但是,如果您愿意使用ServiceBroker,那么您可以通过利用:
up\u sp1
将消息放入队列,此消息激活队列过程,在up\u sp1提交后依次启动up\u sp2
。这是一种可靠的机制,用于处理服务器重启、镜像和群集故障切换,甚至从备份中重建服务器。有关实现非常类似的功能的示例,请参阅。T-SQL除了Service Broker队列之外没有WAITFOR语义。因此,除了使用ServiceBroker之外,您所能做的就是定期轮询并查看表是否已填充。对于小规模而言,这很好,但对于高规模而言,它会中断,因为等待时间和轮询频率之间的正确平衡很难实现,而且更难使其适应峰值和低点
但是,如果您愿意使用ServiceBroker,那么您可以通过利用:
up\u sp1
将消息放入队列,此消息激活队列过程,在up\u sp1提交后依次启动up\u sp2
。这是一种可靠的机制,用于处理服务器重启、镜像和群集故障切换,甚至从备份中重建服务器。有关实现类似目标的示例,请参见。为什么不保持简单和自我记录
DECLARE @Count int;
SELECT @Count = Count(*) FROM [YesMailReplication].[dbo].[SQLCmds]
If @Count = 0 exec up_sp2
为什么不保持简单和自我记录呢
DECLARE @Count int;
SELECT @Count = Count(*) FROM [YesMailReplication].[dbo].[SQLCmds]
If @Count = 0 exec up_sp2
Service Broker解决方案当然是最好的,但也有一个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;
Service Broker解决方案当然是最好的,但也有一个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;