在学着写cursor:
declare @cname varchar(20)
declare cursor_1 cursor for
select cname from table_customer
where address like ‘%bj%’ and cname like ‘H%’
open cursor_1
fetch next from cursor_1 into @cname
while (@@fetch_status = 0)
begin
print ‘@cname’
fetch next from cursor_1 into @cname
end
close cursor_1
deallocate cursor_1
执行这段代码,消息提示 : 命令已成功执行
奇怪,怎么啥都没打印呢?
以前用print命令都是可以正常打印的呀。
为什么这次就不可以了呢?
聪明的你能知道为什么吗?
====>
当时我的想法是:也许print 有问题。print 在cursor中难道没法用?
然后百度啊百度,无解
最后找jerry,才知道问题所在
====>
想知道为什么吗?
啊,啊,啊
其实就是因为select cname from table_customer
where address like ‘%bj%’ and cname like ‘H%’
本身这个结果集就是空集啊。。
====>
关于这次事件给我的思考
遇到大而复杂的东西,一定要把最基本的,最基础的,最原始的,也是最简单的东西 搞清楚搞明白了。。
别的问题,才会迎刃而解
cursor,看上去是一个庞然大物,先要 declare,再 open,再close,再deallcoate
然而,它的根基就只有 一句select 语句。
如果先把这一句搞明白了,理清了
下面就是搭个框架而已!!!
====>
其实在工作中,这种问题也遇到过很多次
在我们的一个系统中,会存在各种调用关系,但是最根基,最核心的,其实也就是那么一段select 语句
先把这个select 语句搞定了
其他的就都是套模式,走套路了
====>
抓住问题的根本所在,才能事半功倍