SQL 存储过程更新表

USE [Reader]
GO
/****** Object:  StoredProcedure [dbo].[procedure_UpdateClassifyArticle]    Script Date: 2018/3/27 16:12:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[procedure_UpdateClassifyArticle]
as 
declare 
@newdate datetime=sysdatetime(),
@id int,
@cid int=0,
@nid int=9999
begin
update a
set a.Status=2,
	a.PostDateTime=a.TimingTime,
	a.TimeStamp=DATEDIFF(S,'1970-01-01 08:00:00',a.TimingTime), -- 新增时间戳排序功能
	@newdate=sysdatetime(),
	a.LastUpdateDateTime=@newdate,
	@id=a.Id,
	@cid=a.ArticleClassifyId,
	@nid=a.NewspaperGroupId
from A a 
where exists 
(select b.Id from A b where b.TimingTime<(select sysdatetime() as today) and b.TimingTime is not null  and  b.Status=4 and a.Id=b.Id)


if(exists(select * from B where B.NewsPaperGroupIdx=@nid and B.ClassifyId=@cid))
update B set LastUpdateDateTime =@newdate where B.NewsPaperGroupIdx = @nid  and B.ClassifyId=@cid
else
insert into  B values(@nid,@cid,@newdate) 
end


当A表更新时,记录下A表的 @Id,@nid,@newdate,@cid用变量存储,然后再去更新B表


猜你喜欢

转载自blog.csdn.net/fy809178958/article/details/79714853