Skip to content

CREATE PROCEDURE

Creates a procedure: a stored query, select or action, that can declare parameters and is run by name.

Access SQL. ACE accepts this statement as written here.

CREATE PROCEDURE procedure_name
[ parameter_name data_type [ , … ] ]
AS procedure_body
CREATE PROCEDURE procedure_name
( parameter_name data_type [ , … ] )
AS procedure_body

procedure_body is one of:

select_statement -- SELECT, UNION, or SELECT … INTO (make-table)
TRANSFORM … PIVOT … -- a crosstab
INSERT INTO … -- VALUES or SELECT (append)
UPDATE …
DELETE …
CREATE TABLE …

The name of the procedure. No table, view or procedure in the database may already have it.

A parameter the body refers to. The list can be written bare or in parentheses. A name is one of:

  • an identifier, bracketed if it needs to be: pCountry, [first name];
  • an @ name: @from;
  • a chain that names an Access form control: [Forms]![frmMenu]![cboCategory].

In the body, refer to the parameter by the same name. A form control chain matches whether its parts are joined by ! or ..

As ACE does, LibRed stores an @ name without its @, and keeps the brackets of a bracketed name.

The parameter’s type, written as for a column in CREATE TABLE: TEXT(15), LONG, DATETIME, CURRENCY and so on. A BIGINT or DATETIME2 parameter does not raise the file’s format version, because a parameter stores no data. ACE does the same.

The statement the procedure stores:

  • SELECT: a select query. Unlike a view, it can have ORDER BY (with TOP too), and can be a UNION.
  • SELECT … INTO: a make-table query.
  • TRANSFORM … PIVOT: a crosstab query.
  • INSERT INTO: an append query, from VALUES or from a SELECT.
  • UPDATE, DELETE: update and delete queries.
  • CREATE TABLE: a data-definition query. It cannot declare parameters.

A FROM clause, a make-table’s INTO and an append’s target can name a table in another Access file with IN 'path'. See SELECT … INTO and INSERT for the forms it takes.

LibRed stores a procedure the way Access does, as an entry in MSysObjects, the query broken down into MSysQueries rows, and one row per parameter. Access opens the file and runs a procedure LibRed wrote.

Run a procedure with EXECUTE, passing a value for each parameter in the order they are declared. A select procedure returns its rows. An action procedure returns the number of rows it changed.

A select procedure can also be used in a FROM clause, like a table. Its parameters then take their values from the command’s parameters, by name.

A CREATE TABLE body is stored as its text, so a parameter declared for it could never bind. LibRed refuses one: “Parameters on a data-definition procedure are not supported”.

To change a procedure, delete it with DROP PROCEDURE and create it again.

Differences from ACE

  • ACE refuses an IN 'path' unless its AllowQueryRemoteTables setting allows it. LibRed has no such setting. LibRed opens only Access databases this way.

These run against the Northwind sample database, as in the playground.

A select query with one parameter:

CREATE PROCEDURE CustomersByCountry (pCountry TEXT(15)) AS
SELECT CustomerID, CompanyName FROM Customers WHERE Country = pCountry
EXECUTE CustomersByCountry 'Mexico'

A bare parameter list with @ names:

CREATE PROCEDURE OrdersBetween @from DATETIME, @to DATETIME AS
SELECT OrderID, OrderDate FROM Orders WHERE OrderDate BETWEEN @from AND @to
EXECUTE OrdersBetween #1996-07-01#, #1996-07-10#

A parameter named after a form control, as Access queries often declare them:

CREATE PROCEDURE ProductsInCategory [Forms]![frmMenu]![cboCategory] LONG AS
SELECT ProductName FROM Products WHERE CategoryID = [Forms]![frmMenu]![cboCategory]
EXECUTE ProductsInCategory 8

A sorted select query and a union, neither of which a view can store:

CREATE PROCEDURE TopProducts AS
SELECT TOP 5 ProductName, UnitPrice FROM Products ORDER BY UnitPrice DESC
CREATE PROCEDURE AllCities AS
SELECT City FROM Customers UNION SELECT City FROM Suppliers ORDER BY City

A crosstab, run by name or read as a table:

CREATE PROCEDURE OrdersByYear AS
TRANSFORM COUNT(OrderID) AS N
SELECT ShipCountry FROM Orders GROUP BY ShipCountry
PIVOT Year(OrderDate)
SELECT * FROM OrdersByYear WHERE ShipCountry = 'UK'

Action queries. These follow on from each other: the first creates the Notes table the others use.

CREATE PROCEDURE MakeNotes AS
CREATE TABLE Notes (Id COUNTER PRIMARY KEY, Body TEXT(255))
EXECUTE MakeNotes
CREATE PROCEDURE AddNote (pBody TEXT(255)) AS
INSERT INTO Notes (Body) VALUES (pBody)
CREATE PROCEDURE CopyUKNames AS
INSERT INTO Notes (Body) SELECT CompanyName FROM Customers WHERE Country = 'UK'
CREATE PROCEDURE EditNote (pId LONG, pBody TEXT(255)) AS
UPDATE Notes SET Body = pBody WHERE Id = pId
CREATE PROCEDURE DeleteNote (pId LONG) AS
DELETE FROM Notes WHERE Id = pId
CREATE PROCEDURE MakeUKList AS
SELECT CustomerID, CompanyName INTO UKList FROM Customers WHERE Country = 'UK'

A make-table query that creates its table in another database. Replace the path with an Access file of your own:

CREATE PROCEDURE ArchiveUK AS
SELECT CustomerID, CompanyName INTO UKArchive IN 'C:\Data\Archive.accdb'
FROM Customers WHERE Country = 'UK'