sql 中延迟执行
USE [CNPC_GasDataReport]
GO
/****** Object: StoredProcedure [dbo].[db_News_Info] Script Date: 04/23/2021 10:06:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--创建存储过程
ALTER procedure [dbo].[db_News_Info]
@CategoryID varchar(20), --定义参数
@NewsTitle varchar(255),
@NewsSource varchar(255),
@NewsDate datetime,
@UpdateTime datetime,
@NewsBody varchar(max)
as
declare @NewsID varchar(255)
if exists (select * from News_Info where CategoryID =@CategoryID and NewsTitle =@NewsTitle and NewsSource =@NewsSource and NewsDate =@NewsDate )
begin
select '1' ID
end
else begin
select '3' ID
INSERT INTO News_Info (CategoryID,NewsTitle,NewsSource,NewsDate,NewsState,UpdateTime,IsArticle,NewsType)
VALUES (@CategoryID,@NewsTitle,@NewsSource,@NewsDate,1,@UpdateTime,1,2)
Select @NewsID = @@IDENTITY
Select @NewsID
waitfor delay '0:0:0.1'
INSERT INTO News_Article (NewsID,NewsBody) VALUES (@NewsID,@NewsBody)
end
主、子表一起插入信息后 ,未等主表ID返回子表就执行出入啦 解决办法加延迟时间
学习:
SQL有定时执行的语句WaitFor。
语法格式:waitfor {delay 'time'|time 'time'}
delay后面的时间是需要延迟多长时间后执行。
time后面的时间是指定何时执行,格式为'HH:MM:SS',不支持日期
例:
--指定10:00执行
Begin
waitfor time '10:00'
select * from tablename
end
--指定等待1小时后执行
begin
waitfor delay '1:00'
select * from tablename
end
--指定等待毫秒级别后执行
waitfor delay '0:0:0.01'