惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
The GitHub Blog
The GitHub Blog
小众软件
小众软件
美团技术团队
博客园 - 司徒正美
G
Google Developers Blog
Blog — PlanetScale
Blog — PlanetScale
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
大猫的无限游戏
大猫的无限游戏
罗磊的独立博客
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Docker
J
Java Code Geeks
Last Week in AI
Last Week in AI
V
Visual Studio Blog
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
V
V2EX
C
Check Point Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MyScale Blog
MyScale Blog

博客园 - Lee-hp

SQL:去掉重复行,只保留第一行 游标(转3) 游标(转2) 游标(转) Sql:取每个分类的前10条数据 sql:去掉重复行 水晶报表参考资料 xml操作 页面信息抓取(二) 页面信息抓取 在GridView中绑定Radio 引用js脚本的奇怪问题 读博 好好工作,好好学习 Url传递中文 SQL触发器--插入时判断数据是否已存在 程序开发和参考资料 SQLServer - 标识列自增 锻炼你的专注力
游标使用——获取每个分类的前10条数据
Lee-hp · 2007-02-02 · via 博客园 - Lee-hp

·表:
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接收,我这样做的目的是为了避免多次存取数据库。