SQL EXEC Keyword

The EXEC (or EXECUTE) keyword in SQL is used to run a stored procedure. Stored procedures are pre-compiled SQL code blocks that you can save and reuse, making your database interactions more efficient and organized.



EXEC: Definition and Usage

Instead of writing and executing the same SQL query repeatedly, you can encapsulate it within a stored procedure and then simply execute the procedure using EXEC. This approach enhances code readability, maintainability, and often improves performance.

Syntax

Syntax

EXEC procedure_name;
--or
EXECUTE procedure_name;
      

Example

Executing a Stored Procedure

This example executes a stored procedure named 'SelectAllCustomers'. (This assumes a stored procedure with that name already exists in your database.)

Syntax

EXEC SelectAllCustomers;
      
Output

The result of the 'SelectAllCustomers' stored procedure will be displayed. This would typically be a table of data.


(The output will depend on what the SelectAllCustomers stored procedure does.  It might be a table of data, or it might be a single value.)