Udostępnij za pośrednictwem


How to: Add Tables to Queries

When you create a query, you are retrieving data from a table or other objects structured like tables — views and certain user-defined functions. To work with any of these objects in your query, you add them to the Diagram Pane.

Note

Depending on the database management system you're connected to you may also be able to use functions, synonyms, and local tables.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

To add a table or table-valued object to a query

  • Drag the table from Server Explorer onto the Diagram pane.

    -or-

  • In the Diagram pane of the Query and View Designer, right-click the background and choose Add Table from the shortcut menu.

  • In the Add Table dialog box, select the tab for the type of object you want to add to the query.

  • In the list of items, double-click each item you want to add.

  • When you finish adding items, click Close.

    The Query and View Designer updates the Diagram Pane, Criteria Pane, and SQL Pane accordingly.

Alternatively, you can drag objects onto the Diagram Pane. You can drag a table, column, view, synonym, or table-valued function from Server Explorer. If you drag an individual column into the Diagram Pane, the column is added to the query for output. If the table containing that column has not already been added, the Query Designer adds it to the query.

Tables and views are automatically added to the query when you reference them in the statement in the SQL pane.

The Query and View Designer will not display data columns for a table or table-valued object if you do not have sufficient access rights to it or if the provider cannot return information about it. In such cases, only a title bar and the * (All Columns) check box are displayed for the table or table-valued object.

To add an existing query to a new query

  1. Make sure the SQL Pane is displayed in the new query you are creating.

  2. In the SQL Pane, type a right and left parentheses () after the word FROM.

  3. Open the Query Designer for the existing query. (You now have two Query Designers open.)

  4. Display the SQL Pane for the inner query – the existing query you are including in the new, outer query.

  5. Select all the text in the SQL Pane, and copy it to the Clipboard.

  6. Click in the SQL Pane of the new query, situate the cursor between the parentheses you added, and paste the contents of the Clipboard.

  7. Still in the SQL Pane, add an alias after the right parenthesis.

See Also

Tasks

How to: Create Table Aliases

How to: Remove Tables from Queries

Other Resources

Specifying Search Criteria

Summarizing Query Results

Performing Basic Operations with Queries