Sqlserver儲存過程中經常使用的迴圈

2021-06-27 06:40:10 字數 3167 閱讀 9507

1:游標的使用:

sql server游標的使用***

declare @date2 datetime

declare tabb_cursor cursor for  

select date,weeknum,studytime  from @tabb where date > @date order by date

open tabb_cursor

fetch next from tabb_cursor into @date2,@weeknumb,@studytimeb

while @@fetch_status = 0

begin

if @weeknumb !=7

insert into @tabc values (@weeknum,@date2,@sequence,@courseid,@chapterid,@studytime)

else if @weeknumb=7

begin

set @weeknum=@weeknum+1

endfetch next from tabb_cursor into @date2,@weeknumb,@studytimeb

endclose tabb_cursor

deallocate tabb_cursor

2:使用表變數:

use [db]

go/****** object:  storedprocedure [dbo].[p_teacherstatis]    script date: 11/26/2014 09:07:01 ******/

set ansi_nulls on

goset quoted_identifier on

goalter proc [dbo].[p_teacherstatis]

asdeclare @teacheridset table (teacherid int,status int)

declare @teanum int,

@v_teacherid varchar(max),

@v_adviserclassnum int,

@v_advisermembertotal int,

@v_adminclassnum  int,

@v_adminmembertotal int,

@errorcode int

set    @errorcode=0

--開啟事物

begin           

set    nocount    on;           

set xact_abort on;           

begin try

begin transaction

-- 直接清空表

truncate table advc_teacher_statis

--建立 所有的老師的teacherid 集合

insert into @teacheridset (teacherid,status)  select  teacherid,1   from advc_teacher  with (nolock) where  status=1

select @teanum=count(*) from @teacheridset where status=1

while @teanum>0

begin

select top 1 @v_teacherid=teacherid from @teacheridset where status=1

-- 這個老師是班主任 管理的班級,管理的班級個數

select @v_adviserclassnum = count(case when act.teachertype =1 then 1 end),@v_adminclassnum = count(*)  

from advc_class_teacher  act with (nolock)   

inner join  advc_class ac  with (nolock)   on ac.classid = act.classid   

where   ac.status=1 and  act.teacherid =  @v_teacherid  

-- 作為班主任 管理學員總數

select @v_advisermembertotal = sum(case when ct.teachertype = 1 then c.currcount else 0 end)

,@v_adminmembertotal = sum(c.currcount)

from advc_class c with(nolock)

inner join  advc_class_teacher ct with (nolock) on ct.classid = c.classid  

inner join  advc_teacher tea with (nolock) on ct.teacherid = tea.teacherid    

where c.status=1  and  ct.teacherid = @v_teacherid

-- 直接清空了,下面就直接插入

insert into [advc_teacher_statis]

([teacherid],[adviserclassnum],[advisermembertotal],[adminclassnum],[adminmembertotal])

values

(@v_teacherid,@v_adviserclassnum,@v_advisermembertotal,@v_adminclassnum,@v_adminmembertotal)

update @teacheridset set status = 0 where teacherid=@v_teacherid

select @teanum=count(*) from @teacheridset where status=1

end--結束事物

commit transaction

end try

begin catch

rollback transaction

set @errorcode=-1 --失敗

end catch

end--print convert(varchar(20),@errorcode)

return @errorcode

SQLServer 儲存過程中各種判斷是否為空

1.判斷是否存在這樣的記錄 if not exists 和 if exists 相對應 例 使用者是否存在 if not exists if exists select 1 from user nolock where username abcd begin print 使用者存在 end else...

SqlServer儲存過程中迴圈的使用

1.while迴圈 格式示例如下 declare iint set i 1 while i 30begin insert into test userid values i set i i 1end2.游標迴圈 格式示例如下 declare a1varchar 10 a2varchar 10 a3i...

SQL SERVER儲存過程中使用事務

儲存過程格式 create procedure yourprocedure asbegin set nocount on begin try 開始捕捉異常 bein tran 開始事務 update a set a.names b.names from t1 as a inner join t2 a...