SQL中迴圈和條件語句

2021-09-22 02:30:59 字數 1140 閱讀 9780

1、if語句使用示例:

declare @a int

set @a=12

if @a>100

begin

print @a

end

else

begin

print 'no'

end2、while語句使用示例:

declare @i int

set @i=1

while @i<30

begin

insert into test (userid) values(@i)

set @i=@i+1

end設定重複執行sql語句或語句塊的條件。只要指定的條件為真,就重複執行語句。可以使用break   和continue關鍵字在迴圈內部控制while迴圈;

3、臨時表和try

--增加臨時表

select * into #csj_temp from csj

--刪除臨時表 用到try

begin try --檢測**開始

drop table #csj_temp

end try

begin catch --錯誤開始

end catch

4、游標迴圈記錄

declare @temp_temp int

--建立游標 --local(本地游標)

declare aaa cursor for select house_id from house_house where deleted=0 or deleted is null

--開啟游標

open aaa

--遍歷和獲取游標

fetch next from aaa into @temp_temp

--print @@fetch_status=0

begin

select * from house_monthend where house_id=@temp_temp

fetch next from aaa into @temp_temp --取值賦給變數

end--關閉游標

close aaa

--刪除游標

deallocate aaa

條件語句和迴圈語句

條件語句 if語句有三種用法 1.if 表示式 如果表示式的值為真,則執行括號內的復合語句 2.if 表示式 else 如果表示式的值為真,則執行語句1,否則執行語句2 3.if 表示式 else if else if else 如果表示式的值為真,則執行對應的語句然後跳出if語句執行後面的語句,若...

條件和迴圈語句

python條件語句是通過一條或多條語句的執行結果 true或者false 來決定執行的 塊。if 判斷條件 執行語句 else 執行語句 開始有縮排的概念 基本判斷語句 age 12 if age 18 print 18歲以下不宜 if語句後面必須有 自動縮排 if語句寫完後,要退回原有縮排繼續寫...

Python條件語句和迴圈語句

1 python條件語句 python條件語句是通過一條或多條語句的執行結果 true或者false 來決定執行的 塊。python程式語言指定任何非0和非空 null 值為true,0 或者 null為false。基本形式為 if 判斷條件 執行語句 else 執行語句 當判斷條件為多個值時,可以...