有关游标的问题

leong_dongzi 2012-05-02 03:22:19
声明游标变量@stu_c,使之关联stu_cursor游标,利用@stu_c查询年龄在6~9月份出生的学生信息。怎样利用游标变量查询?
stu_cursor是之前声明过的游标
我使用set @stu_c=stu_cursor时,提示stu_cursor列名无效,怎么会这样啊?
...全文
98 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
leong_dongzi 2012-05-08
  • 打赏
  • 举报
回复
declare @stu_c cursor
set @stu_c=stu_cursor
declare @stu_id char(4),@stu_name char(8),@stu_sex char(2),@stu_bdate datetime,
@address varchar(50),@remark varchar(50),@stu_depa nchar(1)
open @stu_c
fetch next from @stu_c into @stu_id,@stu_name,@stu_sex,@stu_bdate,@address,@remark,@stu_depa
while @@FETCH_STATUS=0
begin
select @stu_id as 学号,@stu_name as 姓名,@stu_sex as 性别,@stu_bdate as 出生日期,
@address as 家庭地址,@remark as 备注,@stu_depa as 院系
where month(@stu_bdate)>=6 and month(@stu_bdate)<=9
fetch next from @stu_c into @stu_id,@stu_name,@stu_sex,@stu_bdate,@address,@remark,@stu_depa
end
close @stu_c
deallocate @stu_c
go
我知道了,谢谢哈
一克代码 2012-05-02
  • 打赏
  • 举报
回复
红色的就是 要声明一下!
一克代码 2012-05-02
  • 打赏
  • 举报
回复

Declare @CustCode nvarchar(36)
Declare @Number nvarchar(36)
Declare @BeginTime nvarchar(100)
Declare @FundPrice nvarchar(36)
Declare @InterestPrice nvarchar(36)
Declare @SumPrice nvarchar(36)
Declare @RemainPrice nvarchar(36)
Declare @FundType int
select @Numday=RefundDay,
@SendNumber=RefundMsgCount,
@Time=(select CONVERT(varchar(10) , getdate(), 20)),
@NewTime=(dateadd(day,@Numday,@Time)),
@BgTime=dateadd(hour,BgHour,@Time),
@BgTime=dateadd(minute,BgMinute,@BgTime),
@EdTime=dateadd(hour,EndHour,@Time),
@EdTime=dateadd(minute,EndMinute,@EdTime),
@CorpName = CorpName,
@DiffTime=(EndHour-BgHour)*60 + (EndMinute-BgMinute)
from Bas_Config where Id=57

DECLARE MY_CUR CURSOR FOR --声明游标

select CustCode,Number,CONVERT(varchar(100),BeginTime,23),FundPrice,InterestPrice,RemainPrice,SumPrice,FundType from Pact_Plan_List
where BeginTime between convert(varchar(100), @Time,25) and
convert(varchar(100), @NewTime,25) and PlanId not in (select PlanId from Pact_Plan_Pay) and
IsShouldPay = 1 or (IsShouldPay =0 and FundType<>0)

OPEN MY_CUR --打开游标
FETCH NEXT FROM MY_CUR --获取游标的下一行
into @CustCode,@Number,@BeginTime,@FundPrice,@InterestPrice,@RemainPrice,@SumPrice,@FundType
--@CustCode,客户编码 @Number,还款期次 @BeginTime,还款时间 @FundPrice,还款本金 @InterestPrice,还款利息 @RemainPrice,剩余本金
WHILE @@FETCH_STATUS = 0 --FETCH语句执行成功

BEGIN

declare @CustName nvarchar(16) --客户名称
declare @Mobile1 nvarchar(18)--发送手机1
declare @Mobile2 nvarchar(18)--发送手机2
declare @Mobile3 nvarchar(18)--发送手机3
declare @NoteTemCenter nvarchar(1000)--短信模板
declare @SendNoteText nvarchar(1000)--短信内容
declare @SendTime nvarchar(20)--发送时间
declare @i int

select @Mobile1=Mobile1,
@Mobile2=Mobile2,
@Mobile3=Mobile3,
@CustName=CustName
from Custom where CustCode=@CustCode

select @NoteTemCenter = NoteTemCenter from Bas_NoteTem where NoteCode='WHK'

set @SendNoteText=REPLACE(@NoteTemCenter,'###客户###',@CustName)
set @SendNoteText=REPLACE(@SendNoteText,'###公司名称###',@CorpName)
set @SendNoteText=REPLACE(@SendNoteText,'###还款日期###',@BeginTime)
set @SendNoteText=REPLACE(@SendNoteText,'###还款利息###',@InterestPrice)
set @SendNoteText=REPLACE(@SendNoteText,'###还款本金###',@FundPrice)
set @SendNoteText=REPLACE(@SendNoteText,'###本期还款###',@SumPrice)
-- 0 分期付款 1一次性付款 2按揭首付 3分期首付 4部分还款 5按揭首付分期付款 6贷垫款 7按揭首付分期付款首付款
if @FundType = 0 set @SendNoteText=REPLACE(@SendNoteText,'###还款类型###','分期付款')
if @FundType = 1 set @SendNoteText=REPLACE(@SendNoteText,'###还款类型###','次性付款')
if @FundType = 2 set @SendNoteText=REPLACE(@SendNoteText,'###还款类型###','按揭首付')
if @FundType = 3 set @SendNoteText=REPLACE(@SendNoteText,'###还款类型###','分期首付')
if @FundType = 5 set @SendNoteText=REPLACE(@SendNoteText,'###还款类型###','首付欠款')
if @FundType = 6 set @SendNoteText=REPLACE(@SendNoteText,'###还款类型###','贷垫款')
if @FundType = 7 set @SendNoteText=REPLACE(@SendNoteText,'###还款类型###','首付欠款首付款')
if @FundType = 8 set @SendNoteText=REPLACE(@SendNoteText,'###还款类型###','首付欠款利息')
set @SendTime=@BgTime
set @i=0
WHILE (@i) < @SendNumber
BEGIN
set @SendTime=(dateadd(minute,@DiffTime/@SendNumber,@SendTime))
if len(@Mobile1)=11
begin
insert into Bas_Note_Send (SendNoteMobile,SendNoteText,SendTime,IsSend)VALUES (@Mobile1,@SendNoteText,@SendTime,0)
end

if len(@Mobile2)=11
begin
insert into Bas_Note_Send (SendNoteMobile,SendNoteText,SendTime,IsSend)VALUES (@Mobile2,@SendNoteText,@SendTime,0)
end

if len(@Mobile3)=11
begin
insert into Bas_Note_Send (SendNoteMobile,SendNoteText,SendTime,IsSend)VALUES (@Mobile3,@SendNoteText,@SendTime,0)
end

set @i=@i+1
END

FETCH NEXT FROM MY_CUR --获取游标的下一行

--/*@CustCode,客户编码@Number,还款期次@BeginTime,还款时间@FundPrice,还款本金@InterestPrice,还款利息@RemainPrice*/剩余本金

into @CustCode,@Number,@BeginTime,@FundPrice,@InterestPrice,@RemainPrice,@SumPrice,@FundType
END
CLOSE MY_CUR --关闭游标
DEALLOCATE MY_CUR --释放游标

一克代码 2012-05-02
  • 打赏
  • 举报
回复
游标里面的字段变量 也需要声明

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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