Skip to content

EXECUTE

Runs a stored procedure or other stored query by name, with a value for each of its parameters.

Access SQL. ACE accepts this statement as written here.

{ EXECUTE | EXEC } procedure_name [ argument [ , … ] ]

The stored query to run: a procedure made with CREATE PROCEDURE, a view, or a query Access saved. The statement fails if there is none by that name.

A value for one of the procedure’s parameters. The arguments are not in parentheses. They bind to the parameters in the order the procedure declares them, and there must be exactly one for each: “Procedure ‘CustomersByCountry’ declares 1 parameter(s) but was executed with 0 argument(s)”.

An argument is any expression: a literal such as 'Mexico' or #1996-07-01#, an expression such as 'No' & 'rway', or a command parameter such as @country.

LibRed also accepts each argument as parameter_name = value, which binds by name instead of by place. Name every argument or none.

EXEC is the same statement as EXECUTE.

A stored select query returns its rows. A stored action query (append, update, delete, make-table or data definition) runs and returns the number of rows it changed.

A select query can also be read as a table, SELECT * FROM CustomersByCountry, with its parameters taking their values from the command’s parameters by name.

A command’s parameters bind by name to the @name markers in its SQL. The @ is optional in ParameterName: @country and country both bind @country. A marker with no value fails with “No value was supplied for parameter ‘@country’”. LibRed supports named parameters only, so a positional ? marker is refused.

With CommandType.StoredProcedure, CommandText is the procedure’s name, and each parameter binds to the procedure’s parameter of the same name, in any order. See ADO.NET.

These run against the Northwind sample database, as in the playground. They follow on from the examples on the CREATE PROCEDURE page.

EXECUTE CustomersByCountry 'Mexico'
EXEC OrdersBetween #1996-07-01#, #1996-07-10#

Pass a command parameter, with @c set to Norway:

EXECUTE CustomersByCountry @c

Run the action procedures. Each returns the number of rows it changed:

EXECUTE MakeNotes
EXECUTE AddNote 'First note'
EXECUTE EditNote 1, 'Changed'

From C#, run a procedure by name:

using var command = connection.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "CustomersByCountry";
var country = command.CreateParameter();
country.ParameterName = "pCountry";
country.Value = "Norway";
command.Parameters.Add(country);
using var reader = command.ExecuteReader();