在存储过程里边,这么把查询出来的表,一条条遍历,取出想要的数据

wrost 2012-02-28 01:49:44
CREATE PROCEDURE P_Search_ShitfList
@S_BeginTime datetime ,--开始时间 是由查询的医生提供
@S_EndTime datetime ,--结束时间
@DeptID NVarChar --部门编号

AS

declare @ShiftCount int
declare @ShiftID int
declare cursor iTouch for select * from shift where deptID= @DeptID


select @ShiftCount=count(*) from shift
where deptID= @DeptID


while @ShiftCount<>0 begin
--我想在这个地方遍历,取出ShiftID字段
--exec P_Search_Shitf '2012-02-01','2012-02-28','3','8'

set @ShiftCount=@ShiftCount-1
end

GO



他们说要用游标,我这样建立又出错
请问该怎么做,谢谢


...全文
794 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wrost 2012-02-28
  • 打赏
  • 举报
回复
朋友,不好意思,抽烟都把头抽晕了!

非常感谢!
  • 打赏
  • 举报
回复
go
if OBJECT_ID('shift')is not null
drop table shift
go
create table shift(
ID int,
deptid int,
riqi datetime
)
go
insert shift
select 1,1,'2011-12-31' union all
select 2,2,'2011-6-25' union all
select 3,1,'2011-9-30' union all
select 4,1,'2011-05-12' union all
select 5,2,'2011-12-24' union all
select 6,3,'2011-12-11'
select id from shift where deptid=1 and riqi between '2011-6-01'and'2011-10-31'

go
if OBJECT_ID('P_Search_ShitfList') is not null
drop proc P_Search_ShitfList
go
create proc P_Search_ShitfList
@S_BeginTime datetime ,--开始时间 是由查询的医生提供
@S_EndTime datetime ,--结束时间
@DeptID NVarChar --部门编号
as
declare Shitf_Cur cursor
for select id from shift
where deptID=@DeptID
and riqi between @S_BeginTime and @S_EndTime
declare @ShiftID int
open Shitf_Cur
fetch next from Shitf_Cur into @ShiftID
while @@fetch_status=0
begin
print ltrim(@ShiftID)
fetch next from Shitf_Cur into @ShiftID
end
close Shitf_Cur
deallocate Shitf_Cur

exec P_Search_ShitfList '2011-6-01','2011-10-31',1



楼主把游标名看仔细了,你的存储过程中游标名不一致,我已经改过来了
你看看我这个,没问题了
wrost 2012-02-28
  • 打赏
  • 举报
回复
好的,谢谢!
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 wrost 的回复:]

引用 7 楼 travylee 的回复:
引用 6 楼 wrost 的回复:

SQL code
declare Shitf_Cur cursor for select id from shift where deptID= @DeptID


有的啊?


没发现什么语法错误,你把你的表给我,我试试看


还在?
[/Quote]

我这也遇到这个问题了,正在在解决.....
wrost 2012-02-28
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 travylee 的回复:]
引用 6 楼 wrost 的回复:

SQL code
declare Shitf_Cur cursor for select id from shift where deptID= @DeptID


有的啊?


没发现什么语法错误,你把你的表给我,我试试看
[/Quote]

还在?
wrost 2012-02-28
  • 打赏
  • 举报
回复
好的! 谢谢
CREATE TABLE [dbo].[Shift] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[DeptID] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[BeginTime] [datetime] NOT NULL ,
[EndTime] [datetime] NOT NULL ,
[Descript] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Cancel] [bit] NULL ,
[ManCreate] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[ManModify] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[TimeCreate] [datetime] NULL ,
[TimeModify] [datetime] NULL
) ON [PRIMARY]



ID Deptid begintime endtime descript cancel
18 1 2012-01-30 2012-02-07 test 1 xie[谢] NULL 2012-02-24
19 3 2012-02-20 2012-02-29 班次2 1 xie[谢] NULL 2012-02-28
8 3 2012-01-09 2012-02-15 自行车 1 xie[谢] NULL 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wrost 的回复:]

SQL code
declare Shitf_Cur cursor for select id from shift where deptID= @DeptID


有的啊?
[/Quote]

没发现什么语法错误,你把你的表给我,我试试看
wrost 2012-02-28
  • 打赏
  • 举报
回复
declare Shitf_Cur cursor  for select id from shift where deptID= @DeptID

有的啊?
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wrost 的回复:]

引用 3 楼 travylee 的回复:
自定义一个学生表(学号、姓名和性别),利用游标
读取表中数据并输出。
*/
--创建一个学生表student:
go
IF OBJECT_ID('student') is not null
drop table student
go
create table student(
id int ,
name varchar(10),
……
[/Quote]

你都没有声明游标

declare 有标名 cursor for 查询语句(读取哪儿的数据)

wrost 2012-02-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 travylee 的回复:]
自定义一个学生表(学号、姓名和性别),利用游标
读取表中数据并输出。
*/
--创建一个学生表student:
go
IF OBJECT_ID('student') is not null
drop table student
go
create table student(
id int ,
name varchar(10),
gender int check(ge……
[/Quote]

我执行了你的代码,显示成功

我按照你的样式写出来,为什么提示错误?? 请指教


CREATE PROCEDURE P_Search_ShitfList
@S_BeginTime datetime ,--开始时间 是由查询的医生提供
@S_EndTime datetime ,--结束时间
@DeptID NVarChar --部门编号

AS


declare @ShiftID int
declare Shitf_Cur cursor for select id from shift where deptID= @DeptID


open Shift_Cur
fetch next from Shift_Cur into @ShiftID
while @@fetch_status=0
begin

--exec P_Search_Shitf '2012-02-01','2012-02-28','3','8'
print ltrim(str(@ShiftID))
fetch next from Shift_Cur into @ShiftID
end


close Shift_Cur
deallocate Shift_Cur

GO




exec P_Search_ShitfList '2012-02-01','2012-02-28','3'

出错:
服务器: 消息 16915,级别 16,状态 1,过程 P_Search_ShitfList,行 12
名为 'Shitf_Cur' 的游标已存在。
服务器: 消息 16916,级别 16,状态 1,过程 P_Search_ShitfList,行 15
名为 'Shift_Cur' 的游标不存在。
服务器: 消息 16916,级别 16,状态 1,过程 P_Search_ShitfList,行 16
名为 'Shift_Cur' 的游标不存在。
服务器: 消息 16916,级别 16,状态 1,过程 P_Search_ShitfList,行 26
名为 'Shift_Cur' 的游标不存在。
服务器: 消息 16916,级别 16,状态 1,过程 P_Search_ShitfList,行 27
名为 'Shift_Cur' 的游标不存在。
  • 打赏
  • 举报
回复
自定义一个学生表(学号、姓名和性别),利用游标
读取表中数据并输出。
*/
--创建一个学生表student:
go
IF OBJECT_ID('student') is not null
drop table student
go
create table student(
id int ,
name varchar(10),
gender int check(gender=1 or gender=2)
)
go
insert into student values(1001,'tracy',1)
insert into student values(1002,'lily',2)
insert into student values(1003,'kobe',1)
insert into student values(1004,'lucy',2)
insert into student values(1005,'nash',1)

declare cur cursor for select *from student
declare @id int,@name varchar(10),@gender int
open cur
fetch next from cur into @id,@name,@gender
while @@fetch_status=0
begin
print ltrim(str(@id))+','+@name+','+ltrim(str(@gender))
fetch next from cur into @id,@name,@gender
end
close cur
deallocate cur


----楼主参考一下游标如何逐行读取数据
水族杰纶 2012-02-28
  • 打赏
  • 举报
回复
去掉
select @ShiftCount=count(*) from shift
where deptID= @DeptID
判断游标状态用@@fetch_status
WHILE @@fetch_status = 0
begin
end

具体看下游标的用法吧
很基本的
叶子 2012-02-28
  • 打赏
  • 举报
回复

USE AdventureWorks2008R2;
GO
-- Declare the variables to store the values returned by FETCH.
DECLARE @LastName varchar(50), @FirstName varchar(50);

DECLARE contact_cursor CURSOR FOR
SELECT LastName, FirstName FROM Person.Person
WHERE LastName LIKE 'B%'
ORDER BY LastName, FirstName;

OPEN contact_cursor;

-- Perform the first fetch and store the values in variables.
-- Note: The variables are in the same order as the columns
-- in the SELECT statement.

FETCH NEXT FROM contact_cursor
INTO @LastName, @FirstName;

-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN

-- Concatenate and display the current values in the variables.
PRINT 'Contact Name: ' + @FirstName + ' ' + @LastName

-- This is executed as long as the previous fetch succeeds.
FETCH NEXT FROM contact_cursor
INTO @LastName, @FirstName;
END

CLOSE contact_cursor;
DEALLOCATE contact_cursor;
GO




http://msdn.microsoft.com/zh-cn/library/ms190028.aspx

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧