T-SQL의 sleep 명령어
T-SQL 명령어를 쓰는 방법으로는 일정 시간 동안만 sleeve 상태로 만들 수 있습니까?웹 서비스를 비동기적으로 작성하려고 하는데, 비동기 패턴으로 인해 확장성이 향상되는지 확인하기 위해 몇 가지 테스트를 수행할 수 있기를 원합니다.속도가 느린 외부 서비스를 "mock"하기 위해 실행 속도는 느리지만 실제로는 많은 작업을 처리하지 않는 스크립트를 사용하여 SQL 서버를 호출할 수 있어야 합니다.
WAITFOR 명령어를 확인합니다.
예.
-- wait for 1 minute
WAITFOR DELAY '00:01'
-- wait for 1 second
WAITFOR DELAY '00:00:01'
이 명령어는 높은 정밀도를 제공하지만 GetTickCount에 의존하기 때문에 일반 기계에서는 10ms~16ms 이내에서만 정확합니다.예를 들어, 콜은WAITFOR DELAY '00:00:00:001'
대기시간이 전혀 없을 것 같습니다.
WAITFOR DELAY 'HH:MM:SS'
최대 대기 시간은 23시간 59분 59초라고 생각합니다.
다음으로 스칼라 값 함수를 나타냅니다.다음 함수는 초의 정수 파라미터를 취득하여 HH로 변환합니다.MM: SS 및 를 사용하여 실행합니다.EXEC sp_executesql @sqlcode
명령어를 지정합니다.아래 함수는 시연용일 뿐 스칼라값 함수로서는 적합하지 않다는 것을 알고 있습니다! :-)
CREATE FUNCTION [dbo].[ufn_DelayFor_MaxTimeIs24Hours]
(
@sec int
)
RETURNS
nvarchar(4)
AS
BEGIN
declare @hours int = @sec / 60 / 60
declare @mins int = (@sec / 60) - (@hours * 60)
declare @secs int = (@sec - ((@hours * 60) * 60)) - (@mins * 60)
IF @hours > 23
BEGIN
select @hours = 23
select @mins = 59
select @secs = 59
-- 'maximum wait time is 23 hours, 59 minutes and 59 seconds.'
END
declare @sql nvarchar(24) = 'WAITFOR DELAY '+char(39)+cast(@hours as nvarchar(2))+':'+CAST(@mins as nvarchar(2))+':'+CAST(@secs as nvarchar(2))+char(39)
exec sp_executesql @sql
return ''
END
24시간 이상 지연시키고 싶은 경우 @Days 파라미터를 사용하여 며칠 동안 기능 실행 파일을 루프 내에 랩하는 것을 권장합니다.
Declare @Days int = 5
Declare @CurrentDay int = 1
WHILE @CurrentDay <= @Days
BEGIN
--24 hours, function will run for 23 hours, 59 minutes, 59 seconds per run.
[ufn_DelayFor_MaxTimeIs24Hours] 86400
SELECT @CurrentDay = @CurrentDay + 1
END
「WAITFOR」는 「TIME」도 가능합니다.
RAISERROR('Im about to wait for a certain time...', 0, 1) WITH NOWAIT
WAITFOR TIME '16:43:30.000'
RAISERROR('I waited!', 0, 1) WITH NOWAIT
Command Timeout을 테스트하기 위한 매우 간단한 C# 코드를 다음에 나타냅니다.그러면 2초 동안 대기하는 새 명령이 생성됩니다.Command Timeout을 1초로 설정하면 실행 시 예외가 표시됩니다.Command Timeout을 0 또는 2보다 큰 값으로 설정하면 정상적으로 실행됩니다.참고로 기본 Command Timeout은 30초입니다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var builder = new SqlConnectionStringBuilder();
builder.DataSource = "localhost";
builder.IntegratedSecurity = true;
builder.InitialCatalog = "master";
var connectionString = builder.ConnectionString;
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
using (var command = connection.CreateCommand())
{
command.CommandText = "WAITFOR DELAY '00:00:02'";
command.CommandTimeout = 1;
command.ExecuteNonQuery();
}
}
}
}
}
언급URL : https://stackoverflow.com/questions/664902/sleep-command-in-t-sql
'programing' 카테고리의 다른 글
sqlcmd를 사용하여 데이터를 SQL Server에서 CSV 형식으로 내보내려면 어떻게 해야 합니까? (0) | 2023.04.07 |
---|---|
SQL Server Management Studio에서 쿼리 기록을 보는 방법 (0) | 2023.04.07 |
SQL "select where not in subquery"에서 결과가 반환되지 않음 (0) | 2023.04.07 |
X-Requested-With 헤더의 요점은 무엇입니까? (0) | 2023.04.02 |
$.disc() jquery를 사용하여 여러 데이터를 전송하는 방법 (0) | 2023.04.02 |