CREATE PROCEDURE P_Search_ShitfList
@S_BeginTime datetime ,--开始时间  是由查询的医生提供
@S_EndTime datetime ,--结束时间
@DeptID NVarChar --部门编号ASdeclare @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
endGO
他们说要用游标,我这样建立又出错
请问该怎么做,谢谢

解决方案 »

  1.   


    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;
    ENDCLOSE contact_cursor;
    DEALLOCATE contact_cursor;
    GO
    http://msdn.microsoft.com/zh-cn/library/ms190028.aspx
      

  2.   

    去掉
    select @ShiftCount=count(*)  from shift 
    where deptID= @DeptID 
    判断游标状态用@@fetch_status
    WHILE @@fetch_status = 0 
      begin
      end具体看下游标的用法吧
    很基本的
      

  3.   

    自定义一个学生表(学号、姓名和性别),利用游标
       读取表中数据并输出。
    */
    --创建一个学生表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
    ----楼主参考一下游标如何逐行读取数据
      

  4.   


    我执行了你的代码,显示成功我按照你的样式写出来,为什么提示错误??  请指教
    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_CurGO
    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' 的游标不存在。
      

  5.   


    你都没有声明游标declare 有标名 cursor for 查询语句(读取哪儿的数据)
      

  6.   

    declare Shitf_Cur cursor  for select id from shift where deptID= @DeptID有的啊?
      

  7.   

    好的! 谢谢
    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
      

  8.   

    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_Curexec P_Search_ShitfList '2011-6-01','2011-10-31',1楼主把游标名看仔细了,你的存储过程中游标名不一致,我已经改过来了
    你看看我这个,没问题了