LibreOffice Base SQL: Query Parameters

Today, I will continue the discussion of SQL queries with queries that involve parameters in queries. The SQL in this post is based on the TeamMembers database. For instructions on setting up the database in LibreOffice and a short description of the database and tables, see the post at http://wp.me/pvwht-a4.

Parameters in Queries

Imagine that you need to query the database several times a day to know who is on what project in order to arrange meetings and disseminate information about the project. You could write a new query for each project, but this could become unmanageable over time. The SQL would look something like

SELECT “FirstName”, “LastName”
FROM “TeamMembers”
JOIN “TeamMember2Project”
ON “TeamMembers”.”ID” = “TeamMember2Project”.”TeamMemberID”
JOIN “Projects”
ON “TeamMember2Project”.”ProjectID” = “Projects”.”ID”
WHERE “Projects”.”ID” = 3;

where the ID queried in the where clause would change for each project. Repeat that a few hundred times, and you start to clog up you database with queries.

Lucky for us, Base gives us a way to reuse queries using parameters in our queries. A parameter is a placeholder for the value you will supply when you run the query. A parameter is a colon followed by a placeholder name, i.e. :myParameter.

Here is the same query written with a parameter in place of the value for the project ID in the WHERE clause.

SELECT “FirstName”, “LastName”
FROM “TeamMembers”
JOIN “TeamMember2Project”
ON “TeamMembers”.”ID” = “TeamMember2Project”.”TeamMemberID”
JOIN “Projects”
ON “TeamMember2Project”.”ProjectID” = “Projects”.”ID”
WHERE “Projects”.”ID” = :ProjectID;

When you run this query, you are prompted by a dialog box asking for a value for ProjectID. Enter the ID for one of the projects and get your results.

Using parameters in your queries allows you to reuse common queries by replacing values with prompted parameters.

Written with StackEdit.

5 comments on “LibreOffice Base SQL: Query Parameters

  1. Anonymous says:

    륤ȥTĥԩ`,ͥTĥԩ`,åTĥԩ`,֥T ԩ`ͨ؜,륤ȥT,ͥT,åT,Щ`Х`T, ީ`T, 륤ȥTļ,ͥTļ,륤ȥT,ץ`oإĥԩ`դsupremeԩ`ĥ`ѩ`ԩ` ԩ` Ʒ.
    ץ`Tĥԩ` http://www.vicpaynestudio.com

  2. Anonymous says:

    ˚ݤһ֥liveåԩ`Υ`ѩ`ԩ`å؜,˚ݥ֥һԩ`Tꥰåԩ`,˚ƷΌT؜ӵǤåԩ`,ԩ`å,åԩ`,åԩ` t,Gucciԩ`һʥ֥ɥԩ`ᡣձƷ|Υԩ`֥ɴͨ؜.
    ֥tĥԩ` http://forum.bikenet.nl

  3. Ade says:

    Please help: I have 2 dropdown fields on my form A & B. How can i use the value selected in field A as a parameter in my query for field B? Please help

  4. Ade says:

    Just to clarify: I want the values displayed/available in field B to be dependent on the value the user selects in field A. The rational is that field A gets its values using a Select Distinct query from a column titled Colours and the max returned values are 9 (colours). However the same data source has a field titled Variant which holds the various shades of the colours in column Colours. There are 215 values in column Variants so I want to show only Variants (in dropdown field B) that are related to the colour selected by the user in dropdown field A.

  5. Boyet says:

    Hi, how about when the parameter will be supplied by a value from a office base form? TIA.

Leave a comment