Execute an SQL statement or procedure

Microsoft Query

Show All

Execute an SQL statement or procedure

If you know Structured Query Language (SQL), you can use SQL to execute an SQL SELECT statement. For more information about SQL, see the documentation for your database.

  1. On the File menu, click Execute SQL.

    To execute an SQL statement on a table in another data source, click Data Sources, click the data source you want, and then click OK. You can specify a new data source.

    How?

  2. In the SQL statement box, type the SQL statement you want to run.

    If you have an existing SQL statement you want to run, click Open, click the SQL file, and then click Open.

    To run an SQL procedure stored in your external database, click Procedures, click the procedure you want in the Query box, and then click Paste to copy the procedure to the SQL statement box.

  3. Click Execute.

    Microsoft Query runs the SQL statement. If the query is taking a long time to return data, you can cancel the query by pressing ESC.

    To save the SQL statement, click Save.

Notes

  • To change or replace the SQL statement, use the same SQL syntax that Microsoft Query uses or the syntax your data source supports. Microsoft Query tries to interpret the syntax you use and displays both the query's design and its result set.
  • If Microsoft Query can't display the query's design because it can't interpret a portion of the syntax you used, the result set is still displayed. However, you can change only the result set's formatting, such as the display font or column width. You can't edit data, and you must make any change to the query definition, such as adding or removing fields, in the SQL dialog box.
  • If the query returns more than one result set, Microsoft Query displays the first one in the Data pane. Any other result sets are not displayed.
  • If the query runs successfully but does not return a result set, Microsoft Query displays a message that the statement was run successfully. If the query affected rows in the underlying tables, the message also specifies how many rows were affected.
  • If the query didn't run successfully, Microsoft Query displays the SQL error message that corresponds to the problem. To display the Execute SQL dialog box and correct the statement, click OK.