適用於:SQL Server
Azure SQL 資料庫
Azure SQL 受控執行個體
Azure Synapse Analytics
分析平台系統(PDW)
Microsoft Fabric 中的 SQL 分析端點
Microsoft Fabric 中的倉儲
Microsoft Fabric 中的 SQL 資料庫
BREAK 會退出目前的 WHILE 迴圈。 若目前的 WHILE 迴圈位於另一個迴圈的巢狀結構內,則 BREAK 只會退出目前的迴圈,將控制權交給外部迴圈的下一個陳述式。
BREAK 通常位於 IF 陳述式內。
Examples
SQL Server 的範例
想像一下當另一個前項程式完成時,預期值的數據表:
WHILE (1=1)
BEGIN
IF EXISTS (SELECT * FROM ##MyTempTable WHERE EventCode = 'Done')
BEGIN
BREAK; -- 'Done' row has finally been inserted and detected, so end this loop.
END
PRINT N'The other process is not yet done.'; -- Re-confirm the non-done status to the console.
WAITFOR DELAY '00:01:30'; -- Sleep for 90 seconds.
END
Azure Synapse 專用 SQL 集區的範例
DECLARE @sleeptimesec int = 1;
DECLARE @startingtime datetime2(2) = getdate();
PRINT N'Sleeping for ' + CAST(@sleeptimesec as varchar(5)) + ' seconds'
WHILE (1=1)
BEGIN
PRINT N'Sleeping.';
PRINT datediff(s, getdate(), @startingtime)
IF datediff(s, getdate(), @startingtime) < -@sleeptimesec
BEGIN
PRINT 'We have finished waiting.';
BREAK;
END
END