












·表:
create table New_Category
(
C_ID int identity(1,1)primary key,
C_Name varchar(100)
)
create table New_Sub_Category
(
S_ID int identity(1,1) primary key,
S_Name varchar(20),
C_ID int
)
create table News
(
N_ID int identity(1,1) primary key,
N_Title varchar(200),
N_Content ntext,
N_IssueTime Datetime DEFAULT (getdate()),
N_ArticleSource varchar(100),
N_ClickBate int,
N_Key varchar(50),
N_Image varchar(100),
NC_ID int,
N_TodyFocus bit
)
--·添加好分类后,按照分类在news中批量插入数据------------------------------
declare @str varchar(5)
set @str = '10'
insert into news(N_Title,N_Content,N_ArticleSource,N_Key,N_Image,NC_ID)
select C.C_Name + '/' + S.S_Name + @str
,C.C_Name + '/' + S.S_Name + @str
,C.C_Name + '/' + S.S_Name + @str
,C.C_Name + '/' + S.S_Name + @str
,'image',S.S_ID
from new_category C
right join new_sub_category S
on C.C_ID = S.C_ID
·存储过程如下
create procedure [dbo].[News_GetCateNews]
@cid int
as
declare @s int
declare t_cursor cursor
for
select S_ID from new_sub_category
where C_ID = @cid
open t_cursor
fetch next from t_cursor into @s
while @@fetch_status = 0
begin
select top 10 * from news where NC_ID = @s
fetch next from t_cursor into @s
end
close t_cursor
deallocate t_cursor
返回的数据是多个表,利用DataSet接收,我这样做的目的是为了避免多次存取数据库。
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。