Sobes.tech
Junior — Middle

Explain what stored procedures are in database management systems and how they are used.

sobes.tech AI

Answer from AI

Stored procedures are pre-written and saved sets of SQL commands in a database that can be invoked by name. They allow performing complex data operations on the database side, which enhances performance and security.

They are used for:

  • Automating repetitive operations.
  • Centralizing business logic within the database.
  • Reducing the amount of data transferred between the application and the database.

Example of calling a stored procedure in C# using ADO.NET:

using (var connection = new SqlConnection(connectionString))
{
    var command = new SqlCommand("StoredProcedureName", connection);
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.AddWithValue("@param1", value);
    connection.Open();
    command.ExecuteNonQuery();
}