Sleep Command in T-SQL?

后端 未结 4 641
情话喂你
情话喂你 2020-12-04 05:44

Is there to way write a T-SQL command to just make it sleep for a period of time? I am writing a web service asynchronously and I want to be able to run some tests to see i

4条回答
  •  囚心锁ツ
    2020-12-04 06:02

    Here is a very simple piece of C# code to test the CommandTimeout with. It creates a new command which will wait for 2 seconds. Set the CommandTimeout to 1 second and you will see an exception when running it. Setting the CommandTimeout to either 0 or something higher than 2 will run fine. By the way, the default CommandTimeout is 30 seconds.

    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();
            }
          }
        }
      }
    }
    

提交回复
热议问题