游標那點事兒

2021-06-27 17:06:44 字數 1794 閱讀 8099

兩種迴圈跳出方法

1:稍顯複雜點:

create  procedure dbo.usp_cralltables

@client_id varchar(256)

asdeclare @table_name varchar(50)

set nocount on 

declare t_name_cur cursor local static forward_only for 

select name from 

(values ('jim'),('tom'),('anna')) as a (name)

open t_name_cur 

while 1=1

begin 

fetch next from t_name_cur into @table_name

if @@fetch_status<>0 break

print @table_name

end 

close t_name_cur

deallocate t_name_cur

重點在於break放置位置,避免重複取最後一行,這兩種方法的print那裡就是我們想要迴圈執行東東的地方。

2.直接用@@fetch_status 作為迴圈終止條件

set nocount on

declare t_name_cur cursor local static forward_only for

select name from

(values ('jim'),('tom'),('anna')) as a (name)

open t_name_cur

fetch next from t_name_cur into @table_name

while (@@fetch_status=0)

begin

print @table_name

fetch next from t_name_cur into @table_name

end

close t_name_cur

deallocate t_name_cur

需要記得想要在迴圈內執行的東西要放到兩個fetch next之間才好。

實踐,游標加上動態sql 實現對某錶的所有字段進行更新的操作

create procedure clean_up_raw_tables 

@table_name varchar(256) 

as declare @name varchar(256), 

@sql varchar(max) 

declare re_cur cursor local static forward_only for 

select name from syscolumns where id=object_id(@table_name) and name!='id' 

open re_cur 

while (1=1) 

begin 

fetch next from re_cur into @name 

if @@fetch_status<>0 break 

set @sql='update '

+@table_name+

' set '

+@name+

'=replace('

+@name+

',''"'','''')' 

print @sql 

exec(@sql) 

end 

close re_cur 

deallocate re_cur

imu那點事兒

一.對於bosch晶元的總結 offset 是指sensor的零偏。datasheet 裡邊描述的是在不同的情況下offset 的spec.offa,int 表示sensor 出廠時最初的offset spec,是component level offa,board 表示sensor 在貼到pcb ...

Spring MVC那點事兒

1 spring mvc的啟動原理?spring mvc是基於ioc容器的,因此需要先建立ioc容器,才能建立對應的spring mvc執行環境。ioc容器是通過contextloaderlistener建立的,這個類通過servletcontext建立。在springmvc中,最核心的思想其實就是...

sizeof 那點兒事兒

c 用了許久發現我對你的了解還真的不夠,至少可以這樣來評價自己的了解 剛剛接 觸皮毛 所以我打算把c 在系統的複習,或者說重新學習一下。一下我的測試 都是在vc 6.0和32位作業系統下做的測試,參考 c 國際標準1998 和 inside the c object model 一,由sizeof ...