BREAK (Transact-SQL)

Berlaku untuk:yes SQL Server (semua versi yang didukung) YesAzure SQL Database YesAzure SQL Managed Instance yesAzure Synapse Analytics Analytics yesPlatform System (PDW)

BREAK keluar dari perulangan WHILE saat ini. Jika perulangan WHILE saat ini ditumpuk di dalam yang lain, BREAK hanya keluar dari perulangan saat ini, dan kontrol diberikan ke pernyataan berikutnya di perulangan luar.

BREAK biasanya berada di dalam pernyataan IF.

Contoh

Contoh untuk 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

Contoh untuk Kumpulan SQL Khusus Azure Synapse

declare @sleeptimesec int = 5;
declare @startingtime datetime2 = 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

Lihat juga