新闻表存贮过程

USE [newssystem]
GO
/****** Object:  StoredProcedure [dbo].[news_delete]    Script Date: 2013/12/10 22:18:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:  张良伟
-- Create date: 2013-12-10 19:30
-- Description: 删除新闻
-- =============================================
ALTER PROCEDURE [dbo].[news_delete]
@id int
AS
BEGIN
--先删除该新闻下的评论
delete comment where newsId=@id
--再删除新闻本身
delete news where id=@id
END

USE [newssystem]
GO
/****** Object:  StoredProcedure [dbo].[news_selectByCaId]    Script Date: 2013/12/10 22:18:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:  张良伟
-- Create date: 2013-12-10 19:30 
-- Description: 根据类别ID取出该类别下的所有新闻
-- =============================================
ALTER PROCEDURE [dbo].[news_selectByCaId]
@caid int 
AS
BEGIN
    select  n.id,n.title,n.createTime,c.[name] from news n
 inner join category c on n.caId=c.id and n.caId=@caid
 order by n.createtime desc
END

USE [newssystem]
GO
/****** Object:  StoredProcedure [dbo].[news_selectByCaId]    Script Date: 2013/12/10 22:19:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:  张良伟
-- Create date: 2013-12-10 19:30 
-- Description: 根据类别ID取出该类别下的所有新闻
-- =============================================
ALTER PROCEDURE [dbo].[news_selectByCaId]
@caid int 
AS
BEGIN
    select  n.id,n.title,n.createTime,c.[name] from news n
 inner join category c on n.caId=c.id and n.caId=@caid
 order by n.createtime desc
END

USE [newssystem]
GO
/****** Object:  StoredProcedure [dbo].[news_selectByContent]    Script Date: 2013/12/10 22:19:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:  张良伟 
-- Create date: 2013-12-10 7:00
-- Description: 根据内容搜素新闻
-- =============================================
ALTER PROCEDURE  [dbo].[news_selectByContent]
@content varchar(1000)
AS
BEGIN
 select top 10 n.id,n.title,n.createTime,c.[name] from news n
 inner join category c on n.caId=c.id
 where n.content like'%'+@content+'%'
 order by n.createtime desc
END

USE [newssystem]
GO
/****** Object:  StoredProcedure [dbo].[news_selectById]    Script Date: 2013/12/10 22:19:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:  张良伟
-- Create date: 2013-12-10 19:30 
-- Description: 根据类别ID取出该条新闻主体内容
-- =============================================
ALTER PROCEDURE [dbo].[news_selectById]
@id int
AS
BEGIN
select title,[content],createTime,caId from news where id=@id
END

USE [newssystem]
GO
/****** Object:  StoredProcedure [dbo].[news_selectByTitie]    Script Date: 2013/12/10 22:20:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:  张良伟
-- Create date: 2013-12-10 19:30 
-- Description: 根据标题搜索新闻
-- =============================================
ALTER PROCEDURE [dbo].[news_selectByTitie]
@title varchar(100)
AS
BEGIN
 select top 10 n.id,n.title,n.createTime,c.[name] from news n
 inner join category c on n.caId=c.id
 where n.title like '%'+title+'%'
 order by n.createtime desc
END

USE [newssystem]
GO
/****** Object:  StoredProcedure [dbo].[news_selectHotNews]    Script Date: 2013/12/10 22:20:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:  张良伟
-- Create date:2013-12-10 19;00
-- Description: 取出10条热点新闻(评论回复最多的十条新闻)
--=============================================
ALTER PROCEDURE [dbo].[news_selectHotNews]

AS
BEGIN
    select top 10 n.id,n.title,n.createTime,c.[name] from news n
    inner join category c on n.caId=c.id
 inner join comment com on com.newsId=n.id
    group by n.id,n.title,n.createTime,c.[name]
    order by n.createTime desc
END

USE [newssystem]
GO
/****** Object:  StoredProcedure [dbo].[news_selectNewNews]    Script Date: 2013/12/10 22:20:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:  张良伟 
-- Create date: 2013-12-10 7:00
-- Description: 选择最新十条新闻
-- =============================================
ALTER PROCEDURE [dbo].[news_selectNewNews]
AS
BEGIN
 select top 10 n.id,n.title,n.createTime,c.[name] from news n
 inner join category c on n.caId=c.id
 order by n.createtime desc

END

USE [newssystem]
GO
/****** Object:  StoredProcedure [dbo].[news_update]    Script Date: 2013/12/10 22:20:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:  张良伟
-- Create date: 2013-12-10 19:30
-- Description: 修改新闻
-- =============================================
ALTER PROCEDURE [dbo].[news_update]
@id int,
@title varchar(100),
@content text,
@caid int
AS
BEGIN
UPDATE news
set title=@title,[content]=@content,caId=@caid
where id=@id
END

原文地址:https://www.cnblogs.com/liangwei/p/3468335.html