當前位置:首頁 » 存儲配置 » 存儲過程while循環

存儲過程while循環

發布時間: 2023-06-01 17:44:39

㈠ mysql中 while循環只能在存儲過程中使用么

觸發器也可,其實存儲過程或觸發器都是SQL語句,只要是SQL語句就可以使用MYSQL

㈡ mysql 使用存儲過程 循環查找數據

delimiter
$$
mysql>
mysql>
CREATE
PROCEDURE
myProc()
//創建while循環的存儲過程
if分支語句示例
->
BEGIN
->
->
DECLARE
i
int;
->
SET
i=1;
->
loop1:
WHILE
i<=10
DO
->
IF
MOD(i,2)<>0
THEN
/*Even
number
-
try
again*/
->
SELECT
CONCAT(i,"
is
an
odd
number");
->
END
IF;
->
SET
i=i+1;
->
END
WHILE
loop1;
->
END$$
Query
OK,
0
rows
affected
(0.00
sec)
這種也可以

㈢ mysql存儲過程中iterate對while起不起效果

在MySQL存儲過程的語句中有三個標準的循環方式:WHILE循環,LOOP循環以及REPEAT循環。還有一種非標準的循環方式:GOTO,不過這種循環方式最好別用,很容易引起程序的混亂,在這里就不錯具體介紹了。
這幾個循環語句的格式如下:
WHILE……DO……END WHILE
REPEAT……UNTIL END REPEAT
LOOP……END LOOP
GOTO。

下面首先使用第一種循環編寫一個例子。
mysql> create procere pro10()
-> begin
-> declare i int;
-> set i=0;
-> while i<5 do
-> insert into t1(filed) values(i);
-> set i=i+1;
-> end while;
-> end;//
Query OK, 0 rows affected (0.00 sec)
在這個例子中,INSERT和SET語句在WHILE和END WHILE之間,當變數i大於等於5的時候就退出循環。使用set i=0;語句是為了防止一個常見的錯誤,如果沒有初始化,i默認變數值為NULL,而NULL和任何值操作的結果都是NULL。
執行一下這個存儲過程並產看一下執行結果:
mysql> delete from t1//
Query OK, 0 rows affected (0.00 sec)
mysql> call pro10()//
Query OK, 1 row affected (0.00 sec)
mysql> select * from t1//
+——-+
| filed |
+——-+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
+——-+
5 rows in set (0.00 sec)
以上就是執行結果,有5行數據插入到資料庫中,證明存儲過程編寫正確無誤^_^。
再來看一下第二個循環控制指令 REPEAT……END REPEAT。使用REPEAT循環控制語句編寫下面這個存儲過程:
mysql> create procere pro11()
-> begin
-> declare i int default 0;
-> repeat
-> insert into t1(filed) values(i);
-> set i=i+1;
-> until i>=5
-> end repeat;
-> end;//
Query OK, 0 rows affected (0.00 sec)
這個REPEAT循環的功能和前面WHILE循環一樣,區別在於它的執行後檢查是否滿足循環條件(until i>=5),而WHILE則是執行前檢查(while i<5 do)。
不過要注意until i>=5後面不要加分號,如果加分號,就是提示語法錯誤。
編寫完成後,調用一下這個存儲過程,並查看結果:
mysql> delete from t1//
Query OK, 5 rows affected (0.00 sec)
mysql> call pro11()//
Query OK, 1 row affected (0.00 sec) #雖然在這里顯示只有一行數據受到影響,但是下面選擇數據的話,還是插入了5行數據。
mysql> select * from t1//
+——-+
| filed |
+——-+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
+——-+
5 rows in set (0.00 sec)
一行就是執行結果,實際的作用和使用while編寫的存儲過程一樣,都是插入5行數據。
再來看一下第三個循環控制語句LOOP……END LOOP。編寫一個存儲過程程序如下:
mysql> create procere pro12()
-> begin
-> declare i int default 0;
-> loop_label: loop

-> insert into t1(filed) values(i);
-> set i=i+1;
-> if i>=5 then
-> leave loop_label;
-> end if;
-> end loop;
-> end;//
Query OK, 0 rows affected (0.00 sec)
從上面這個例子可以看出,使用LOOP編寫同樣的循環控制語句要比使用while和repeat編寫的要復雜一些:在循環內部加入了IF……END IF語句,在IF語句中又加入了LEAVE語句,LEAVE語句的意思是離開循環,LEAVE的格式是:LEAVE 循環標號。
編寫完存儲過程程序後,來執行並查看一下運行結果:
mysql> delete from t1//
Query OK, 5 rows affected (0.00 sec)

㈣ oracle存儲過程怎麼寫循環

寫循環的操作方法和步驟如下:

1、第一步,編寫存儲過程的整體結構,然後定義變數,見下圖。

㈤ SQL Server存儲過程里 select語句里能加入while循環查詢嗎或者說怎麼循環查詢

第一種方和友仿法,創建臨時表
create table #temptable()
WHILE @StartID < @EndID
BEGIN
insert into #temptable SELECT。。。
END
第二種方法,使用拼裝一喚纖個SQL
declare @sql nvarchar(2000)
WHILE @StartID < @EndID
BEGIN
組裝一個合適告悉的SQL,使用union all拼起來
set @sql=@sql+''
END
exec(@sql)

㈥ oracle存儲過程循環怎麼寫

Oracle中有三種循環(For、While、Loop):
1、loop循環:

createorreplaceprocerepro_test_loopis
inumber;
begin
i:=0;
loop
i:=i+1;
dbms_output.put_line(i);
ifi>5then
exit;
endif;
endloop;
endpro_test_loop;


2、while循環:

createorreplaceprocerepro_test_loopis
inumber;
begin
i:=0;
whilei<5loop
i:=i+1;
dbms_output.put_line(i);
endloop;
endpro_test_loop;


3、for循環1:

createorreplaceprocerepro_test_foris
inumber;
begin
i:=0;
foriin1..5loop
dbms_output.put_line(i);
endloop;
endpro_test_for;

4、for循環2:

createorreplaceprocerepro_test_cursoris
userRowt_user%rowtype;
cursoruserRowsis
select*fromt_user;
begin
foruserRowinuserRowsloop
dbms_output.put_line(userRow.Id||','||userRow.Name||','||userRows%rowcount);
endloop;
endpro_test_cursor;

㈦ 存儲過程一直循環怎麼解決

該怎麼吐槽呢,你要實現這功能用不著循環,循環寫的也不對,不管怎麼循環sal_custormer里的trackeman也不會減少,當然永遠exists,你要寫的這個邏輯類似sql游標循環,自己網上查吧。
這個功能你最好不使用循環,嘗試先把要插入的數據select出來,如果基礎不好不能一步到位,就多利用零時表做緩存
Select ecode , name from employees a where exists(select 1 from sal_custormer_1 where sal_custormer_1.tracke_man = a.encode)
一句話就完事兒了,再前面加個insert^_^

㈧ mssql 存儲過程中循環如何寫,在循環中用什麼語句跳出循環呢,在線等

給你個例子:

for loop是這樣的:
createprocere pro
begin
declare aintdefault1;
label1: loop
if a<6then
selecta;
seta=a+1;
iterate label1;
endif;
leave label1;跳出循環
endloop label1;
end

while是這樣的:
createprocere pro
begin
declare aintdefault4;
while a<10 do
selecta;
seta=a+1;
endwhile;
end

㈨ Sqlserver存儲過程如何寫循環

declare @i int

set @i = 0

while @i < 100

begin

print @i

set @i = @i + 1

end

-- 定義循環變數

declare @loopIndex int set @loopIndex = 0

--定義循環次數

declare @count int set @count=1

-- 取得循環次數

select @count=count(1) from sys_user

-- 開始循環

while @loopIndex <= @count

begin

  -- 定義接收參數

  declare @USER_NAME nvarchar(50)

-- 取得循環的數據

SELECT @USER_NAME = hh.USER_NAME

FROM (SELECT ROW_NUMBER() OVER (ORDER BY USER_NAME) 'rowindex',USER_NAME FROM sys_user)hh 

WHERE hh.rowindex = @loopIndex

-- 進行相關業務邏輯 例如輸出結果 

print @USER_NAME

-- 循環自動加一

set @loopIndex = @loopIndex + 1

end

begin

  -- 定義錯誤返回信息

  declare @error int 

-- 定義接收參數

  declare @User_Name varchar(50)

  declare @Address varchar(50)

  set @error=0

  --定義游標

  declare demo_cursor cursor

  for (select User_Name,Address from sys_user)

  --打開游標--

  open demo_cursor

  --開始循環游標變數--

  fetch next from demo_cursor into @User_Name,@Address

  while @@FETCH_STATUS = 0  --返回被 FETCH語句執行的最後游標的狀態--

    begin       

      print @User_Name+'____'+@Address

      set @error= @error + @@ERROR  --記錄每次運行sql後是否正確,0正確

      fetch next from demo_cursor into @User_Name,@Address  --轉到下一個游標,沒有會死循環

    end  

  close demo_cursor --關閉游標

  deallocate demo_cursor  --釋放游標

end

更多內容請訪問: https://mxdqh.top/

熱點內容
python處理excel文件 發布:2025-02-06 16:36:09 瀏覽:439
演算法相對定位 發布:2025-02-06 16:32:42 瀏覽:725
java程序的編譯和執行 發布:2025-02-06 16:21:45 瀏覽:416
什麼是淘寶帳號和密碼 發布:2025-02-06 16:21:36 瀏覽:495
解壓前面簽 發布:2025-02-06 16:02:00 瀏覽:324
華碩訪問點 發布:2025-02-06 15:56:57 瀏覽:331
excel拼接sql 發布:2025-02-06 15:50:10 瀏覽:501
加密手機直播 發布:2025-02-06 15:49:31 瀏覽:535
自帶ftp伺服器好用嗎 發布:2025-02-06 15:26:11 瀏覽:110
win7訪問xp區域網 發布:2025-02-06 15:17:07 瀏覽:525